Why are numbers over 100 not used in counting?

Asked

Viewed 42 times

-1

I have this exercise:

  • Pass a list with basketball games score
  • From that list, every time a score was higher than the previous score, it would add a point
  • In addition to the scoring of how many times the player surpassed his own scoring, in the end it will also show in which game he made the lowest score

The code I made was this::

let stringPontuacoes = "30 40 20 4 51 25 42 38 56 0"
let teste1 = '10 20 20 30 20 50 0'
let teste2 = '20 30 10 50 90 10 100 150 20 10 30 56 0'
let teste3 = '10 20'

let desempenho = (string) => {
    let melhorDesempenho = 0;
    let piorJogo = 0;
    let arrayConverter = string.split(' ');
    let firstScore = arrayConverter[0];
    let subsequente = firstScore;
    for (i = 0; i < arrayConverter.length; i++) {
        if (arrayConverter[i] > subsequente) {
            subsequente = arrayConverter[i];
            melhorDesempenho++;
        }
    }
    let convertNumber = arrayConverter.map(Number);
    let min = Math.min(...convertNumber)
    piorJogo = convertNumber.indexOf(min) + 1;
    console.log([melhorDesempenho, piorJogo]);
} 

desempenho(stringPontuacoes); //resultado = [3, 10]
desempenho(teste1); //resultado = [3, 7]
desempenho(teste2); //resultado = [3, 13]; era pra ser [5, 13]
desempenho(teste3);//resultado [1, 1]

The problem is with lists that have numbers greater than 99. Could anyone tell me why this happens? In this case, it does not count any score above 99, not adding a point in the list to those numbers. The code makes sense in my head, the only problem is being with those numbers from 100. Lists that do not have them give the correct result.

  • let teste2 = '20 30 10 50 90 10 100 150 20 10 30 56 0' for this string, the result should not be [7, 13] and not [5, 13]?

  • No, because it only adds up to one point if the next number is higher than the previous highest score. Then I would start with 30 > 20 [1], 50 > 30 [2], 90 > 50 [3], 100 > 90 [4] and 150 > 100 [5].

  • anyway, to find out if the number is bigger, should not convert to number? so for example if (Number(arrayConverter[i]) > Number(subsequente)) see here: https://jsfiddle.net/svd05rak/

  • Wow, now it was. Apparently that was the problem. In this case he wasn’t taking the numbers over 99 because he was comparing between strings? I thought it would work because with numbers smaller than 100 was working normally. Thank you very much!!!

  • It worked by coincidence, since '10' > '90' is false, despite comparing strings (texts) instead of numbers. The problem is that '100' > '90' also gives false, for the same reason. Understand better by reading here: https://answall.com/q/440496/112052

  • Thank you very much!!! Now it’s clear.

Show 1 more comment

1 answer

-2


The problem is that you are making comparisons between string and non-number values.

Following the way you developed, after you converted the string to array with this code let arrayConverter = string.split(' ');, you can convert all values to number in this way const arrayConvertedNumber = arrayConverter.map(value => Number(value));.

Following your logic I did this exercise in a more simplified way that can help you in your studies.

const scores1 = [30, 40, 20, 4, 51, 25, 42, 38, 56, 0];
const scores2 = [10, 20, 20, 30, 20, 50, 0];
const scores3 = [20, 30, 10, 50, 90, 10, 100, 150, 20, 10, 30, 56, 0];
const scores4 = [10, 20];

function desempenhoDoJogador(scores) {
  const lowerValueFromScore = Math.min(...scores);
  const lowestScoreGame = scores.indexOf(lowerValueFromScore) + 1;
  let counterBestScore = 0;
  let nextScore = scores[0];

  for (let score of scores) {
    if (score > nextScore) {
      nextScore = score;
      counterBestScore++;
    }
  }

  const Message = `O jogador superou a sua pontuação ${counterBestScore} vezes, e a menor pontuação foi no jogo ${lowestScoreGame}`;

  console.log(Message);
}

desempenhoDoJogador(scores1); //3, 10
desempenhoDoJogador(scores2); //3, 7
desempenhoDoJogador(scores3); //5, 13 
desempenhoDoJogador(scores4); //1, 1

Click here to view the code in the stackblitz
Documentation of the For of

Browser other questions tagged

You are not signed in. Login or sign up in order to post.