[{"id":"javascriptadvanced-001","courseId":"javascript-advanced","domain":"values-and-types","topic":"Symbol.toPrimitive precedence","prompt":"The engine must turn an object into a primitive for an expression like obj + ''. Put these steps of that conversion in order.","hint":"Think about which hook an object can define itself, and what that hook must return.","explain":"An object can take full control of its own conversion to a primitive. Each conversion also passes a hint naming the type the caller prefers, and that hint is only a weak signal.","source":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toPrimitive","interaction":{"type":"sort","items":[{"text":"Look up the object's Symbol.toPrimitive method.","why":"Coercion checks for this symbol method before trying any other conversion route."},{"text":"Call that method with a hint of \"number\", \"string\" or \"default\".","why":"The hint names the preferred type, but the method is free to ignore it."},{"text":"Check that the method returned a primitive value.","why":"Returning an object instead makes the conversion throw a TypeError."},{"text":"Use that primitive in place of the object in the operation.","why":"In the MDN example, the method returns true for the default hint, so obj + \"\" works on that primitive and gives \"true\"."}]},"options":[],"answer":"o:abcd","explanation":"Coercion checks an object's Symbol.toPrimitive method first; only when there is none does it fall back to valueOf() and toString().","concept":"Coercion checks an object's Symbol.toPrimitive method first; only when there is none does it fall back to valueOf() and toString().","rev":1,"kind":"scenario","takeaways":["A Symbol.toPrimitive method overrides valueOf() and toString() for every conversion of that object."]},{"id":"javascriptadvanced-002","courseId":"javascript-advanced","domain":"values-and-types","topic":"String length in code units","prompt":"A teammate runs the code below and is surprised by the first number. Which TWO statements about it are correct?","hint":"Think about the unit a string's length counts, and how iterating a string differs from that.","explain":"The length property counts a fixed low-level unit of a string, not the characters a reader sees. Iterating a string follows a different rule, so the two counts can disagree for some text.","source":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"s.length prints 2, because length counts UTF-16 code units.","why":"The emoji is stored as a surrogate pair, two 16-bit code units, and length counts those units."},{"id":"b","text":"[...s].length prints 1, because the iterator yields whole characters.","why":"The string iterator goes by characters, so the surrogate pair is kept together as one item."},{"id":"c","text":"s.length prints 1, because the emoji is one visible character.","whyNot":"length does not count visible characters or grapheme clusters; it counts code units."},{"id":"d","text":"s.length prints 1, because length counts Unicode code points.","whyNot":"length counts UTF-16 code units, so a single emoji outside the BMP gives 2."},{"id":"e","text":"[...s].length prints 2, because the iterator splits surrogate pairs.","whyNot":"The string iterator yields characters, so the pair is not split."},{"id":"f","text":"Setting s.length = 1 shortens the string to one character, since length is writable.","whyNot":"String length is not writable; assigning to it has no effect and throws in strict mode."}],"answer":"ab","explanation":"String length counts UTF-16 code units, so the emoji 😄 has length 2, while the string iterator yields it as one character.","concept":"String length counts UTF-16 code units, so the emoji 😄 has length 2, while the string iterator yields it as one character.","rev":1,"kind":"scenario","code":"const s = \"😄\";\nconsole.log(s.length);\nconsole.log([...s].length);","takeaways":["Count characters with [...str].length or Intl.Segmenter, not with length."]},{"id":"javascriptadvanced-003","courseId":"javascript-advanced","domain":"values-and-types","topic":"String.raw","prompt":"Tagged with String.raw, the template literal String.raw`Total: ${2 + 3}\\n` returns text where the substitution is filled in as 5, while the escape stays as a backslash followed by n.","hint":"Think about which parts of a tagged template the tag receives in raw form.","explain":"A tag function receives a template literal's pieces in two forms, cooked and raw, along with the values of its substitutions. The tag decides how to combine them into its result.","source":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","why":"Substitutions are still evaluated and inserted, but escape sequences are kept as typed, so the \\n stays as two characters."},{"id":"b","text":"False","whyNot":"String.raw does not skip substitutions; only escape sequences are left unprocessed."}],"answer":"a","explanation":"String.raw returns the raw form of a template literal: substitutions are processed, but escape sequences such as \\n are kept as typed.","concept":"String.raw returns the raw form of a template literal: substitutions are processed, but escape sequences such as \\n are kept as typed.","rev":1,"kind":"scenario","takeaways":["String.raw keeps backslashes as typed, which suits Windows paths and patterns; substitutions still work."]},{"id":"javascriptadvanced-004","courseId":"javascript-advanced","domain":"values-and-types","topic":"Date-only ISO parsing","prompt":"A server stores the date-only string '2019-01-01', and a client stores '2019-01-01T00:00:00' with no zone. Both are passed to Date.parse on a machine set to UTC-5. Which statement is true?","hint":"Compare how the time zone is decided when a string has a date only versus a date and a time.","explain":"Date strings in the same family can be read with different time zone rules. One shape assumes a fixed zone, while another uses whatever zone the machine running the code is set to.","source":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse","interaction":{"type":"single"},"options":[{"id":"a","text":"The date-only value is midnight UTC, while the date-time value is local midnight, so the two timestamps differ.","why":"A date-only string is read as UTC, and a date-time string with no zone is read as local time."},{"id":"b","text":"Both timestamps are equal, because Date.parse ignores the time part of a string.","whyNot":"The time part is used; without a zone it is read as local time, not dropped."},{"id":"c","text":"The date-time string returns NaN, because a time without a time zone is invalid.","whyNot":"It is a valid date-time string; without a zone it is read as local time."},{"id":"d","text":"The date-only string is read as local midnight, the same as the date-time string.","whyNot":"A date-only string is read as UTC, so the two strings can produce different values."}],"answer":"a","explanation":"A date-only ISO string is parsed as UTC, while a date-time string without a zone designator is parsed in the local time zone.","concept":"A date-only ISO string is parsed as UTC, while a date-time string without a zone designator is parsed in the local time zone.","rev":1,"kind":"scenario","takeaways":["Put an explicit zone such as Z in date-time strings, or the result depends on the machine."]},{"id":"javascriptadvanced-005","courseId":"javascript-advanced","domain":"values-and-types","topic":"Math.min with NaN","prompt":"A checkout reads prices from a form, and one entry is the text 'n/a'. The code below runs with that array. What does cheapest hold?","hint":"Think about what Math.min does when one argument cannot be read as a number.","explain":"Math.min accepts any number of arguments and reports the smallest. Its arguments are converted to numbers before comparing, so a value that cannot become a number affects the result.","source":"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/min","interaction":{"type":"single"},"options":[{"id":"a","text":"NaN, because any argument that converts to NaN makes Math.min return NaN.","why":"Math.min converts each argument to a number, and a NaN in the list makes the whole result NaN."},{"id":"b","text":"2.5, because Math.min skips values that are not numbers.","whyNot":"Math.min does not skip anything; the text converts to NaN, which wins over every number."},{"id":"c","text":"'n/a', because Math.min returns the smallest value as it was given.","whyNot":"Math.min always returns a number, so the text is converted before any comparison."},{"id":"d","text":"Infinity, because the invalid entry is treated as if it were missing.","whyNot":"Infinity is returned only when Math.min gets no parameters at all."}],"answer":"a","explanation":"Math.min converts each argument to a number; if any converts to NaN, the result is NaN rather than the smallest valid value.","concept":"Math.min converts each argument to a number; if any converts to NaN, the result is NaN rather than the smallest valid value.","rev":1,"kind":"scenario","code":"const prices = [4, 2.5, \"n/a\", 7];\nconst cheapest = Math.min(...prices);","takeaways":["Validate inputs before Math.min, since one bad value turns the whole result into NaN."]}]