Taking one step further with a medium dificulty challenge. Hope you like it!
function play(arr){
var mosse=0;
while(arr.length>0){
arr=arr.slice(0,arr.indexOf(Math.max.apply(null, arr)));
mosse++;
}
return mosse % 2 ? "Bob" : "Andy";}
Just found this challenge! Hope my solution is fine (and hope the contest is to write down solutions as well )
EDIT: I’ve seen your solution and they are pretty similar, almost equal
The only relevant change:
return (turns % 2 === 0) ? ‘Andy’ : ‘Bob’;
turn%2 is itself a condition -> it return 0 (false) for odd and !=0(true) for even; so you can avoid to repeat a test [===0] and just change places in order to have the right answer: Bob->odd; Andy->even)
Nice challenge and nice idea!
We also use a different array method.
You’re using slice
and I’m using splice
Also for the while loop, I haven’t used the > 0
condition because if the array is empty. the length is 0 and it evaluates to false.
Nice one, guys! I had totally forgotten about both the spread operator and splice
method. I totally over-killed this one:
function playGame(arr) {
let sorted_a
let max
let moves = 0
while (arr.length) {
sorted_a = Array.from(arr).sort( function (a, b) {
return b<=a
})
moves++
max = sorted_a.pop()
arr = arr.slice(0, arr.indexOf(max))
}
return (moves % 2 == 0 ? 'ANDY' : 'BOB')
}
It just proves I should RTFM a bit more (last example mentions the spread operator!).