CRT-600 practice question 29 of 30
Refer to code below: Let productSKU = ‘8675309’ ; A developer has a requirement to generate SKU numbers that are always 19 characters lon, starting with ‘sku’, and padded with zeros. Which statement assigns the values sku0000000008675309 ?
Full set: 223 questionsThe goal is to transform '8675309' into 'sku0000000008675309'. This is a two-step process using chained string methods.
1. productSKU.padStart(16, '0'): The original 7-character string is padded with zeros at the start until it reaches a length of 16. This results in '0000000008675309'. The target length is 16 because the final length is 19, and the 'sku' prefix is 3 characters (19 - 3 = 16).
2. .padStart(19, 'sku'): The 16-character result from the first step is then padded at the start with 'sku' until it reaches the final target length of 19, yielding the correct value.
A. The second padStart call is invalid; its first argument must be a target length (a number), not the string 'sku'.
B. padEnd incorrectly adds padding to the right of the number. The second padStart call is also syntactically invalid.
C. padEnd incorrectly adds padding to the right of the number, which does not meet the requirement of placing zeros between the prefix and the SKU.
1. MDN Web Docs (Mozilla): String.prototype.padStart(). The documentation specifies the syntax padStart(targetLength, padString). It states, "The padStart() method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length." This supports the two-step padding logic in option D.
2. ECMAScript® 2023 Language Specification (ECMA-262, 14th edition): Section 22.1.3.18 String.prototype.padStart ( maxLength [, fillString ] ). This official specification defines the abstract operation StringPad, which is invoked by padStart. It confirms that the first argument, maxLength, is coerced to an integer length, and the padding is applied to the start of the string. This validates the method's usage in option D and invalidates the syntax in option A.
3. MDN Web Docs (Mozilla): String.prototype.padEnd(). This document explains that padEnd adds padding to the end (right side) of a string, which is contrary to the requirement of adding a prefix and zero-padding on the left. This confirms why options B and C are incorrect.
Keep practicing
Take the free CRT-600 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other CRT-600 practice questions
- Question 2A developer has the function, shown below, that is called when a page loads. function onl…
- Question 3A developer needs to test this function: 01 const sum3 = (arr) => ( 02 if (!arr.length) r…
- Question 4Refer to the code below: Const resolveAfterMilliseconds = (ms) => Promise.resolve ( setTi…
- Question 10Refer to the following code block: class Animal{ constructor(name){ this.name = name; } m…
- Question 11A developer needs to debug a Node.js web server because a runtime error keeps occurring a…
- Question 12Refer to the string below: const str = 'Salesforce'; Which two statements result in the w…
