0
Object with pattern using for loop
I wonder how can i make this pattern with for loop. I want to make rock-paper-scissor game with more complex rules certainly. Can anyone solve this using for loop? rules.rocks[0].status = "draw" rules.rocks[1].status = "lose" rules.rocks[2].status = "win" rules.papers[0].status = "win" rules.papers[1].status = "draw" rules.papers[2].status = "lose" rules.scissors[0].status = "lose" rules.scissors[1].status = "win" rules.scissors[0].status = "draw" https://code.sololearn.com/W0g62UkWNPKN/?ref=app
3 Answers
+ 6
Tri Satria [NEW] Are you looking for sometime like this:
----
const statuses = ["draw", "lose", "win"]
const items = ["rocks", "papers", "scissors"]
items.forEach(rule => {
rules[rule].forEach((item, index) => {
item.status = statuses[index]
})
//Rotate the order of statuses
statuses.unshift(statuses.pop())
})
----
https://code.sololearn.com/WrYTH9z1cFp5/?ref=app
+ 2
Woah i have seen this somewhere but couldnt realize this was what i needed. Thanks for answering.
+ 2
Tri Satria [NEW] If you saw _this_ code, which I quickly wrote from scratch - for your code, somewhere else... that would be a mind blowing coincidence. đđ€