Skip to content

Arcade Scores - array methods

webprogramming language-features arrays unit-test

In this task, you will process high scores collected by an arcade system. Because the scores were read from a text input field, the input array may contain values of different types, including numbers, numeric strings, booleans, null, undefined, and NaN. A score is considered valid only if its type is number and its value is not NaN.

Implement the functions listed below in index.js as global function declarations. Do not use modules, imports, or exports.

Related concepts: the typeof operator, strict equality, the remainder operator, exponentiation, filter, map, reduce, sort, slice, template literals, spread syntax, and objects.

Starter package

The task description and starter files, ready to work on locally.

Included files: index.js

Tasks

isValidScore(s)

Determines whether a single value is a valid score.

  • Parameter: s: a value of any type.
  • Returns: true if s is of type number and is not NaN; otherwise false.

Examples:

isValidScore(42)

Expected return value: true

isValidScore("42")

Expected return value: false

cleanScores(scores)

Filters an array, keeping only valid scores.

  • Parameter: scores: an array containing values of any type.
  • Returns: a new array containing only the elements for which isValidScore returns true. The input array must not be modified.

Example:

cleanScores([42, "42", NaN, 17, null, 0])

Expected return value: [42, 17, 0]

evenScores(scores)

Selects the even scores from an array of numbers.

  • Parameter: scores: an array of numbers. It may be assumed that all elements are valid scores.
  • Returns: a new array containing only the even numbers.

Example:

evenScores([2, 3, 4])

Expected return value: [2, 4]

doubleScores(scores)

Doubles each score in the array.

  • Parameter: scores: an array of numbers.
  • Returns: a new array in which each element is twice the corresponding input value.

Example:

doubleScores([2, 3])

Expected return value: [4, 6]

formatScores(scores)

Formats scores for display.

  • Parameter: scores: an array of numbers.
  • Returns: a new array of strings, where each number s is converted to the form "s pts".

Example:

formatScores([42, 7])

Expected return value: ["42 pts", "7 pts"]

totalOddBonus(scores)

Computes the sum of the squares of all odd scores.

  • Parameter: scores: an array of numbers.
  • Returns: the sum of the squared values of the odd elements. Returns 0 if the array is empty or contains no odd numbers.

Example:

totalOddBonus([1, 2, 3, 4, 5])

Expected return value: 35, since 1 + 9 + 25 = 35.

combineBoards(board1, board2)

Merges two scoreboards.

  • Parameters: board1 and board2: two arrays.
  • Returns: a new array containing all elements of board1 followed by all elements of board2. Neither input array may be modified.

Example:

combineBoards([1, 2], [3, 4])

Expected return value: [1, 2, 3, 4]

topScores(scores, n)

Returns the highest n valid scores in decreasing order.

  • Parameters: scores: an array containing values of any type; n: a non-negative number indicating how many top scores to return.
  • Returns: a new array of length at most n, containing the largest valid scores sorted from highest to lowest. Invalid values are ignored. If fewer than n valid scores exist, all of them are returned. If n is 0 or no valid scores exist, an empty array is returned. The input array must not be modified.

Example:

topScores([5, "7", 20, null, 3, 12], 2)

Expected return value: [20, 12]

averageValidScore(scores)

Computes the arithmetic mean of the valid scores.

  • Parameter: scores: an array containing values of any type.
  • Returns: the average of the valid scores. Returns 0 if the array contains no valid scores.

Example:

averageValidScore([10, "20", 30, null])

Expected return value: 20, since (10 + 30) / 2 = 20.

parityGroups(scores)

Groups the valid scores by parity.

  • Parameter: scores: an array containing values of any type.
  • Returns: an object with two properties: even and odd. Each property holds an array with the corresponding valid scores, preserving their original relative order.

Example:

parityGroups([1, 2, "3", 4, null, 5])

Expected return value: { even: [2, 4], odd: [1, 5] }

Requirements

  • Define each function with a function declaration in the form function name(...) { ... }.
  • Do not use import, export, or type="module".
  • Declare variables with const or let. The use of var is not permitted.
  • Functions returning arrays must return new arrays and must not modify their inputs.
  • Use strict equality (=== and !==) for all comparisons.