Puzzle : The descent (Javascript)

Hello mates !

-Puzzle : The descent-

I can’t get why my code is not working if anyone can take a look ? :slight_smile:

// game loop
while (true) {
    var mountains = [];
    var max = Math.max(null, mountains);
    for (var i = 0; i < 8; i++) {
        var mountainH = parseInt(readline()); 

this code outputs me -1 for the tests 1 and 2, then 0 for tests 3,4,5

according to the indexOf docs, -1 means that the value is not found.

Maybe a scope problem ?

Anyway thanks for your time !


I can see 2 problems here:

  • You’re trying to find the max of an empty array, then populate it. Maybe put your
    var max = Math.max(null, mountains);

after the for-loop?

  • You may use Math.max.apply instead.

For your initial question, max(null, []) return 0, and if there is not mountains with 0 height, your indexOf will return -1.

Ps: Btw, you should use the Decent puzzle dedicated thread for this kind of question.

hence closing the topic.