# JavaScript Coding Challenge #9

JavaScript Coding Challenge #9
0

#1

Taking one step further with a medium dificulty challenge. Hope you like it!

#2
``````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)

#3

Very nice @Aloap ! You have a different solution. I hope you enjoyed it!

#4

Nice challenge and nice idea!

#5

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.

#6

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!).