forked from joinpursuit/8-0-accumulator-pattern-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (65 loc) · 2.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* Returns a boolean representing whether or not all values are numbers.
* @param {number[]} rolls - An array of numbers representing rolls on a die.
* @returns {boolean} Returns `true` if all values in the array are numbers. Otherwise, return `false`.
*/
function isValid(rolls) {
let result = true;
for (i = 0; i < rolls.length; i++) {
if (typeof rolls[i] !== 'number') {
result = false
}
} return result;
}
/**
* Finds a value in an array. If that value is in the array, returns it. Otherwise, returns `null`.
* @param {number[]} rolls - An array of numbers representing rolls on a die.
* @param {number} value - A specific value to find.
* @returns {*} - The found value or `null`.
*/
function findValue(rolls, value) {
for (i = 0; i < rolls.length; i++) {
if (rolls[i] === value) {
return value;
}
}
return null
}
/**
* Returns a new array from the `rolls` array with only values equal to or greater than the `lowest` value.
* @param {number[]} rolls - An array of numbers representing rolls on a die.
* @param {number} lowest - A number that represents the lowest allowed value in the new array.
* @returns {number[]} An array of all numbers that are equal to or higher than the `lowest` value.
*/
function filterOutLowValues(rolls, lowest) {
let newArray = [];
for (i = 0; i < rolls.length; i++) {
if (rolls[i] >= lowest) {
newArray.push(rolls[i]);
}
}
return newArray;
}
/**
* Returns an object which has rolls as keys and counts as values.
* @param {number[]} rolls - An array of numbers representing rolls on a die.
* @returns {object} An object where the keys are numbers rolled and the values are the number of times that roll appears in the `rolls` array.
*/
function getRollCounts(rolls) {
let newObject = {};
for (i = 0; i < rolls.length; i++) {
if (newObject[rolls[i]] === undefined) {
newObject[rolls[i]] = 1;
} else {
newObject[rolls[i]] += 1
}
}
return newObject
}
// Do not change the code below here.
module.exports = {
isValid,
findValue,
filterOutLowValues,
getRollCounts,
};