Skip to content

Instantly share code, notes, and snippets.

@jx13xx
Created April 1, 2022 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jx13xx/a560c4c39c9cdf174d3206cd55465d8c to your computer and use it in GitHub Desktop.
Save jx13xx/a560c4c39c9cdf174d3206cd55465d8c to your computer and use it in GitHub Desktop.
reduce, map, filter example javascript
const scores = [50, 6, 100, 0, 10, 75, 8, 60, 90, 80, 0, 30, 110];
//Any scores that are below 10 needs to be multiplied by 10 and the new value included.
const boostSingleScores = scores.map(function (val) {
return (val < 10) ? val *10 : val;
})
//Remove any scores that are over 100.
const removeOverScores = boostSingleScores.filter(function (value) {
return value <= 100;
})
//Remove any scores that are 0 or below.
const belowZero = removeOverScores.filter(function (value) {
return value > 0;
})
//Sum the scores.
const total = belowZero.reduce(function (accumalor, nextValue){
return accumalor + nextValue;
},0);
//Provide a count for the number of scores still remaining.
const scoreCount = belowZero.reduce(function (cnt, value){
console.log(value);
return cnt + 1;
},0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment