How to return the size of a variable result?

Asked

Viewed 51 times

0

Personally I created a function to which with it I am returning the amount of notes obtained (in case 6 notes), I also return a list of the notes that broke the record (the record was the note 8) and I am also returning the worst note (the note 4). So I would like to know how to return the amount of notes that broke the record, as you can see in the code below I just manage to list these notes, so I don’t know how to catch the length of the constant total.

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:");

for (let i = 0; i < pontos.length; i++) {
    const total = pontos[i];
    if (total > 8) {
        console.log(total);
    }
}
console.log("Abaixo a nota mais ruim: ")
var min = pontos.map(Number).reduce(function (a, b) {
    return Math.min(a, b);
});

console.log(min);
return notas;
}

    notas();
  • Seria total.length? Since they’re all string.

  • you can put a counter inside the if (total > 8) that counts how many times you went in there

  • 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

2 answers

2


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();

  • 1

    Thank you very much, man as I didn’t think of it! Thank you very much!

2

For those who like solutions in one line


I wonder how I return the amount of notes that hit the record [...]

You can use the filter function to return the notes records that correspond to the filter and then use length to return the amount of these records, so basically this would be your method

pontos.filter(x => parseFloat(x) > 8).length;

Your code would look like this:

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:");

for (let i = 0; i < pontos.length; i++) {
    const total = pontos[i];
    if (total > 8) {
        console.log(total);
    }
}

console.log('Abaixo o número de notas que superaram o recorde');
console.log(pontos.filter(x => parseFloat(x) > 8).length);

console.log("Abaixo a menor nota: ")
var min = pontos.map(Number).reduce(function (a, b) {
    return Math.min(a, b);
});

console.log(min);
return notas;
}

    notas();

  • Nice, that way I’ve never done it, thank you!

Browser other questions tagged

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