Arcade Scores - array methods
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
Determines whether a single value is a valid score.
- Parameter:
s: a value of any type. - Returns:
trueifsis of typenumberand is notNaN; otherwisefalse.
Examples:
isValidScore(42)
Expected return value: true
isValidScore("42")
Expected return value: false
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
isValidScorereturnstrue. The input array must not be modified.
Example:
cleanScores([42, "42", NaN, 17, null, 0])
Expected return value: [42, 17, 0]
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]
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]
Formats scores for display.
- Parameter:
scores: an array of numbers. - Returns: a new array of strings, where each number
sis converted to the form"s pts".
Example:
formatScores([42, 7])
Expected return value: ["42 pts", "7 pts"]
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
0if 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.
Merges two scoreboards.
- Parameters:
board1andboard2: two arrays. - Returns: a new array containing all elements of
board1followed by all elements ofboard2. Neither input array may be modified.
Example:
combineBoards([1, 2], [3, 4])
Expected return value: [1, 2, 3, 4]
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 thannvalid scores exist, all of them are returned. Ifnis0or 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]
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
0if the array contains no valid scores.
Example:
averageValidScore([10, "20", 30, null])
Expected return value: 20, since (10 + 30) / 2 = 20.
Groups the valid scores by parity.
- Parameter:
scores: an array containing values of any type. - Returns: an object with two properties:
evenandodd. 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, ortype="module". - Declare variables with
constorlet. The use ofvaris not permitted. - Functions returning arrays must return new arrays and must not modify their inputs.
- Use strict equality (
===and!==) for all comparisons.