The variable const total
is serving only to temporarily store the value of each array position to be compared to the value 8
, therefore this variable is not storing all values that are greater than 8
. So she can’t count how many grades are bigger than 8
, because its final value will be the last iteration of the loop for
.
What you need to do is create another variable with value 0
:
let bateram_o_recorde = 0;
And increase each time the condition total > 8
be attended to:
bateram_o_recorde++;
At the end this variable will have the value of how many numbers were greater than 8
:
function notas() {
const pontos = ["4", "8", "6.3", "9", "9.5", "8.5"];
const quanti = pontos.length;
console.log("Foram obtidas " + quanti + " notas no total!");
console.log("Apenas as notas abaixo superaram o recorde:");
let bateram_o_recorde = 0;
for (let i = 0; i < pontos.length; i++) {
const total = pontos[i];
if (total > 8) {
console.log(total);
bateram_o_recorde++;
}
}
console.log("Abaixo a nota mais ruim: ")
var min = pontos.map(Number).reduce(function (a, b) {
return Math.min(a, b);
});
console.log(min);
console.log("Quantidade de notas que bateram o record: ", bateram_o_recorde);
return notas;
}
notas();
I just didn’t understand the final line return notas;
, since notas
is the very
function.
If you are wanting to store the larger notes than 8
, would have to create an array, make an .push(total)
and then make a .length
in that array:
function notas() {
const pontos = ["4", "8", "6.3", "9", "9.5", "8.5"];
const quanti = pontos.length;
console.log("Foram obtidas " + quanti + " notas no total!");
console.log("Apenas as notas abaixo superaram o recorde:");
let bateram_o_recorde = []; // cria a array
for (let i = 0; i < pontos.length; i++) {
const total = pontos[i];
if (total > 8) {
console.log(total);
bateram_o_recorde.push(total); // insere na array
}
}
console.log("Abaixo a nota mais ruim: ")
var min = pontos.map(Number).reduce(function (a, b) {
return Math.min(a, b);
});
console.log(min);
console.log("Quantidade que bateram o record: ", bateram_o_recorde.length); // conta os itens da array
return notas;
}
notas();
Seria
total.length
? Since they’re all string.– Guilherme Nascimento
you can put a counter inside the
if (total > 8)
that counts how many times you went in there– Gabriel Oliveira
length think does not work pq is not a list, Voce can create a list of notsa larger than the record and use list.push(points[i]), and then see the length of that list
– Gabriel Oliveira