TryBugHunt
All collections
⚡Type Coercion ~20 mins

JavaScript Type Coercion Traps

When numbers become strings and values vanish into NaN

Accidental string concatenation, array sort ordering numbers alphabetically, and zero being falsely treated as missing.

JavaScriptMedium5 Challenges

Your Progress

0 of 5 completed

Start Collection

Challenges in this collection

  1. 1

    Number sort puts 10 before 2

    sortNumbers(arr) sorts numbers from smallest to largest. Example: sortNumbers([10, 2, 30]) → [2, 10, 30].

    easy
  2. 2

    Cart total shows "01020" instead of 30

    calculateTotal(items) adds up item.price for every item. The prices arrive as text, like "10". Example: prices "10" and "20" → 30.

    easy
  3. 3

    Zero counter also counts "" and false

    countZeros(values) counts the entries that are the number 0. Example: countZeros([0, "", false, 0]) → 2.

    medium
  4. 4

    Adding up [1, 2, 3] gives NaN

    sumAll(nums) adds up every number in the array. Example: sumAll([1, 2, 3]) → 6.

    easy
  5. 5

    map(parseInt) turns "10" into NaN and 2

    parseAll(strings) turns a list of number strings into numbers. Example: parseAll(["10", "10", "10"]) → [10, 10, 10].

    medium

More collections to explore