Home > database >  I need to see how many times the next number is greater than the before one
I need to see how many times the next number is greater than the before one

Time:07-24

function gameHistory(gamePoints){

    let points = gamePoints.split(", ")
    let recordBreak = 0;
    let bestPoint = points[0];

    for(let i = 1; i<points.length; i  ){
        
        if(points[i]>bestPoint){

            bestPoint = points[i]
            recordBreak = recordBreak   1;
            
        }

    }
    return console.log(recordBreak) 

     /*
      the console shows 2, was spected three, because we 
    have three times that the player beated the records:
    10 to 12,
    12 to 15,
    15 to 20. 
     */

}

gameHistory("10, 12, 8, 15, 20")  //I used this game results for example

CodePudding user response:

The code is comparing strings after the split. Change them to numbers with a map...

let points = gamePoints.split(", ").map(str =>  str);

function gameHistory(gamePoints) {
  let points = gamePoints.split(", ").map(str =>  str);
  let recordBreak = 0;
  let bestPoint = points[0];

  for (let i = 1; i < points.length; i  ) {
    if (points[i] > bestPoint) {
      bestPoint = points[i]
      recordBreak = recordBreak   1;
    }
  }
  return recordBreak

  /*
      the console shows 2, was spected three, because we 
    have three times that the player beated the records:
    10 to 12,
    12 to 15,
    15 to 20. 
     */

}

console.log(gameHistory("10, 12, 8, 15, 20"))
//I used this game results for example

  • Related