JavaScript Coding Challenge #9
For today I prepared a HackerRank challenge. This one is rated as a Medium difficulty challenge, which is good, we’re advancing :D. Let’s see what we need to do!
Gaming Array
Andy loves playing games. He wants to play a game with his little brother, Bob, using an array,
Aof distinct integers. The rules are as follows:1. Bob always plays first and the two players move in alternating turns.
2. In a single move, a player chooses the maximum element currently present in the array and removes it as well as all the other elements to its right. For example, if
A = [2, 3, 5, 4, 1], then it becomesA = [2, 3]after the first move because we remove the maximum element (i.e.,5) and all elements to its right (i.e.,4and1).3. The modifications made to the array during each turn are permanent, so the next player continues the game with the remaining array. The first player who is unable to make a move loses the game.
Given the initial array can you find and return the name of the winner? If Andy wins, return
ANDY; if Bob wins, returnBOB.
Before reading forward I highly recommend that you try to solve it on your own, this is how you’ll learn the most out of it.
Please note that I don’t claim to have the best solution for this challenge, so, if you find a better one I’d love to see how you did it! :)
OK… here we go!
First I’m going to write a function which will have as it’s parameter an array A with distinct integers, and will return the winner as a string. We initially set winner to be an empty string.
function gamingArray(A){
let winner = '';
return winner;
}
Good! Now, let’s see… The game plays until the array remains empty, right? This is when one of the two players losses the game. So we need to run a loop as long as long there are elements in the array:
function gamingArray(A){
let winner = ''; while(A.length){ } return winner;
}
We could also write: while(A.length > 0), but in JavaScript 0 is evaluated to false so we save some characters there xD.
Awesome! Now that we have the game loop, for each iteration we need to actually play the game, right? So… we need to find the max value in the array and remove it along with all the elements to its right.
We’re going to go a little “crazy” and do all these steps in one line. This is something I love in JavaScript a lot!
For that we need four things:
Array.splice()=> removes items from an array. We usespliceinstead ofslicebecause it changes the original array.Array.indexOf()=> searches the array for the specified item, and returns its position.Math.max()=> returns the number with the highest value.
4. The spread syntax => allows an expression to be expanded in places where multiple arguments (for function calls) or multiple elements (for array literals) or multiple variables (for destructuring assignment) are expected. Read more about it on MDN.
By using the spread syntax in the Math.max() method we get the maximum value from the array very easily without needing to loop through all the numbers. This will do that for us.
Let’s see how all these pieces fit together in perfect harmony:
function gamingArray(A){
let winner = ''; while(A.length){
A.splice(A.indexOf(Math.max(...A)));
} return winner;
}
So elegant! I love it! ❤
Get Florin Pop’s stories in your inbox
Join Medium for free to get updates from this writer.
Let’s see now how many turns they played the game until the array emptied out. For this I’m going to add a variable turns initialized with 0 which will increase by 1 in every loop. You’ll understand in a moment why we need to do this.
function gamingArray(A){
let winner = '';
let turns = 0; while(A.length){
A.splice(A.indexOf(Math.max(...A)));
turns++;
} return winner;
}
OK, but… how do we know who won?
It’s very easy. I’ll explain:
Let’s consider we have one number in the array. We know that Bob always plays first so he removes this number and therefore Andy loses because there are no more moves left so Bob wins…
For two numbers in ascending order like [1, 2] we’ll have:
- Bob removes number
2; - Andy removes number
1; - The array is empty… Andy wins.
For three numbers in ascending order like [1, 2, 3]…
- Bob removes
3; - Andy removes
2; - Bob removes
1; - The array is empty… Bob wins.
As you can see, if we have an even number of turns Bob wins, otherwise Andy wins. This is where we’ll decide the winner by using the turns variable:
function gamingArray(A){
let winner = '';
let turns = 0; while(A.length){
A.splice(A.indexOf(Math.max(...A)));
turns++;
} winner = (turns % 2 === 0) ? 'Andy' : 'Bob'; return winner;
}
We’re using the ternary operator to set the winner.
Let’s also return directly the result of this operation and get rid of the winner variable:
function gamingArray(A){
let turns = 0; while(A.length){
A.splice(A.indexOf(Math.max(...A)));
turns++;
} return (turns % 2 === 0) ? 'Andy' : 'Bob';
}
Yey! We solved yet another fun coding challenge! :D
Conclusion
For me this challenge was a lot of fun because I got to mix up all those beautiful JavaScript methods in a very elegant way, I’d say. ^_^
Do you have a better solution to it? Please don’t hesitate to let me know about it in the comments sections bellow. I’ve learned a lot from people who submitted their own solution on the previous coding challenges we had in the past. Find out more about them here.
I hope you enjoyed this, I would sincerely appreciate a click on the Recommend button 💚. Thank you!







