Created
March 17, 2021 11:38
-
-
Save cmarchena/6c8e2aae28b813b1c4042ec206b9d94b to your computer and use it in GitHub Desktop.
Array Challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const RefArray = ['RED', 'GREEN', 'BLUE', 'ORANGE', 'YELLOW', 'PINK', 'CYAN', 'BLACK', 'VIOLET'] | |
// RefArray is an array of strings | |
// RefArray length must adapt to user input | |
// Range depends on user input | |
// Function output must be an array of objects | |
// Function output must be printed as a string to user | |
function reducer(input){ | |
const result = [] | |
RefArray.slice(0,input).reduce((accum, curr, i)=>{ | |
const range = (100/input); | |
const output = { | |
'color': curr, | |
'rangeFrom': Number(accum.toFixed(2)), | |
'rangeTo': Number((accum+range).toFixed(2)) | |
} | |
result.push(output) | |
return accum += range | |
},0) | |
return console.log(JSON.stringify(result)); | |
} | |
reducer(6); | |
/* output | |
[ | |
{"color":"RED","rangeFrom":0,"rangeTo":16.67}, | |
{"color":"GREEN","rangeFrom":16.67,"rangeTo":33.33}, | |
{"color":"BLUE","rangeFrom":33.33,"rangeTo":50}, | |
{"color":"ORANGE","rangeFrom":50,"rangeTo":66.67}, | |
{"color":"YELLOW","rangeFrom":66.67,"rangeTo":83.33}, | |
{"color":"PINK","rangeFrom":83.33,"rangeTo":100} | |
] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment