code-gladiator1

by bhupendra negi

JavaScript

function GetJumpCount(input1, input2, input3) {
  // input validations
  if (!(typeof input1 === "number" && 1 < input1 < Math.pow(10, 19))) {
    return false;
  }

  if (!(typeof input2 === "number" && 1 < input2 < Math.pow(10, 5))) {
    return false;
  }

  //input3 [wallheight1,wallheight2]

  var count = 0; // # of attempts

  input3.forEach(function(val) {
    // console.log(val);
    var wallCount = 0; // # for crossing each wall
    if (val <= input1) {
      wallCount = 1;
    } else {
      var netjump = input1 - input2;

      for (var i = val; i >= 0; i = i - netjump) {

        wallCount++;
        if (i <= input1) {
          break;
        }
      }

      console.log("wall : " + val + "  attempts :" + wallCount);

    }
    count += wallCount
  });

  return count;

}


// test wall heights;
var  t0 = window.performance.now();
var wallCounter = GetJumpCount(5, 1, [9,10]);
console.log(" Total # of attempts :" + wallCounter);
var t1 = window.performance.now();
console.log("Time taken :"+ (t1-t0).toFixed(4));