I have an array of values and I am looking to create a new array where specific values are assigned based on the values in the original array. For instance:
My initial array:
var values = [1, 2, 3, 4]
The new array I want:
[red, green, blue, black]
I attempted to achieve this using a for loop like so:
var values = [1, 2, 3, 4]
var colors = [];
for (var i = 0; i < values.length; i++) {
if (values < 200) {
colors += "green";
} else if (values.value > '200') {
colors += "blue";
} else {
colors += "grey"
}
}
console.log(colors);
However, currently, it only returns grey. It seems like I might not be accessing the correct value of the array items
EDIT: I received a lot of comments quickly! I attempted to illustrate my problem, but I used placeholder content and ended up confusing my example a bit, I apologize.
Adam, thank you for your response. I didn't realize I missed the [i] within the loop. I updated my loop to the following, and now it works:
for (var i = 0; i < values.length; i++){
if (values[i] < 200) {
colors+="green";
}else if (values[i] < 300){
colors+="blue";
}else{
colors+="grey"
}
}
EDIT 2, it functions to some extent. However, to store the values in an array, I utilized the push method as explained by 31piy.