+ 4

Can someone SOLVE THIS!

else if(balls[0].color == "red" && balls[1].color == "red" && balls[2].color == "red" && balls[3].color == "red" && balls[4].color == "red" && balls[5].color == "red" && balls[6].color == "red" && balls[7].color == "red" && balls[8].color == "red" && balls[9].color == "red" && balls[10].color == "red" && balls[11].color == "red" && balls[12].color == "red" && balls[13].color == "red" && balls[14].color == "red"... etc Can someone simplify the conditional, if value in property in an object are whether blue or red (true). Im using this method but still struggling on it //method function Color(color){ this.color = color } let colors = [] for(var j = 0; j < ballNum; j++){ let color = "yellow" colors.push(new Color(color)) colorObj = colors[j] } var allBlue = Object.keys(colorObj).every(function(k){ if(colorObj[k] === "blue" ){ console.log("blue win") } if(colorObj[k] === "red"){ console.log("red win") }}); https://code.sololearn.com/WvDDRkLGPRV9/?ref=app

8th Aug 2020, 5:20 AM
Tri Satria [FIRST]
Tri Satria [FIRST] - avatar
7 Respostas
+ 4
balls.every((ball) => ball.color === "red" https://code.sololearn.com/W8F22YAZ3TOT/#js btw- My initial reply is a direct quote from mdn.
8th Aug 2020, 5:51 AM
ODLNT
ODLNT - avatar
+ 4
I would like to give u 1 million likes just to solve this thank u so much for solving the problem! I will insert your name in the game!
8th Aug 2020, 5:55 AM
Tri Satria [FIRST]
Tri Satria [FIRST] - avatar
+ 3
"The every() method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value." source: https://developer.mozilla.org/ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
8th Aug 2020, 5:42 AM
ODLNT
ODLNT - avatar
+ 3
You answering pretty well but i dont know how to implement, can u give an ex?
8th Aug 2020, 5:46 AM
Tri Satria [FIRST]
Tri Satria [FIRST] - avatar
+ 2
You're welcome, I'm glad I could help.👍
8th Aug 2020, 6:00 AM
ODLNT
ODLNT - avatar
+ 2
You should use var allBlue=arrayName.every(function(k){return k=='blue'}); if(allBlue) console.log('blue'); else console.log('as you wish');
8th Aug 2020, 6:09 AM
ƬIMΛ
ƬIMΛ - avatar
+ 1
Thanks carlo, you both do a great job. Its now working and ill be die in peace
8th Aug 2020, 6:13 AM
Tri Satria [FIRST]
Tri Satria [FIRST] - avatar