-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]
?– Ricardo Pontual
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].
– flaviojoni
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/– Ricardo Pontual
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!!!
– flaviojoni
It worked by coincidence, since
'10' > '90'
isfalse
, despite comparing strings (texts) instead of numbers. The problem is that'100' > '90'
also givesfalse
, for the same reason. Understand better by reading here: https://answall.com/q/440496/112052– hkotsubo
Thank you very much!!! Now it’s clear.
– flaviojoni