How do I print the amount of times an array item appears

Asked

Viewed 135 times

0

function must be written to receive only one parameter: an array of notes. It must also return an array with three elements:

The first, with the number of banknotes equal to 0 or 1. Would be the ones who did not like

The second, with the number of notes equal to 2 or 3. Would be those who found average

The third, with the number of banknotes equal to 4 or 5. Would be the ones who liked

notas = [1,1,3,3,5,5]

function calculaGostos(notas){
   for(i =0; i <notas.length; i++){
       if(notas[i] >=4){
            var nGostaram = [notas[i] >=4]
        }if (notas <=1) {
            var nNaoGostaram = [notas[i] <=1]
        } else {
            var nMediano = [notas[i] ==2||notas[i] ==3] 
        }
for each()

        return [nGostaram,nNaoGostaram,nMediano]
    }
}
  • Hello Bianca, your question is not clear, and the code you put as example, for being wrong does not help clarify that is the problem, you could add details to your question so that we can help you

  • I’m sorry, Leo, but even I don’t understand the logic of the exercise. But what I wanted to know is how do I retouch how many times an item appears within an array. It’s very compelling by the attention.

  • You can add details such as, for example, what this array looks like, whether it’s numbers or string, how do you know which item you’re looking for, if the item is passed as parameter... Things like.

  • I reorganized the code to make it easier to read and tried to use for each but I don’t know the syntax, I tried to multiply the if variables by the amount of elements in the array as well.

1 answer

0


Your code does one thing but your question says something else, anyway I will demonstrate some ways to solve what I believe to be your problem, this is our array of notes notas = [1,1,3,3,5,5], its function can be done in the following mode:

const notas = [1,1,3,3,5,5];

function calculaGostos(notas){
  // Declare as 3 notas necessárias com valor inicial 0
  let nGostaram = 0;
  let nNaoGostaram = 0;
  let nMediano = 0;

  // Dentro ao for você não declara nenhum variável, mas usa as que acabamos de criar for do for, do contrário, variáveis criadas dentro do for, existem somente dentro do for 
  for(i =0; i < notas.length; i++){
     if(notas[i] >= 4){
          // O operador ++ equivale a nGostaram = nGostaram + 1;
          nGostaram++;
          
     } else if (notas[i] <= 1) {
          nNaoGostaram++;
     } else {
          nMediano++;
      }
  }
    
  // Return é última instrução dentro a uma função, está a indicar que a função terminou a sua execução e deve retornar um valor
  return [nGostaram,nNaoGostaram,nMediano]
}

console.log(calculaGostos(notas)); // [2, 2, 2]

The above example is a way that can be used to do what you need, but you can make your life easier, for example the function ends by returning an array of numbers, you will not always remember what each number means and every time you will look for the function to know what it returns you? Let’s make it better!

const notas = [1,1,3,3,5,5];

function calculaGostos(notas){
  // Declare as 3 notas necessárias com valor inicial 0
  let gostaram = 0;
  let naoGostaram = 0;
  let mediano = 0;

  // Dentro ao for você não declara nenhum variável, mas usa as que acabamos de criar for do for, do contrário, variáveis criadas dentro do for, existem somente dentro do for 
  for(i =0; i < notas.length; i++){
     if(notas[i] >= 4){
          // O operador ++ equivale a nGostaram = nGostaram + 1;
          gostaram++;

     } else if (notas[i] <= 1) {
          naoGostaram++;
     } else {
          mediano++;
      }
  }

  // Mudamos os nomes das variáveis só para serem mais legiveis 
  // Retornamos um objeto e não um array
  return {gostaram, naoGostaram, mediano}
}
    
// Bem mais fácil de ler
console.log(calculaGostos(notas)); // {gostaram: 2, naoGostaram: 2, mediano: 2}

Take some time to read a little about these methods that can and will be useful during your life as a Javascript programmer:

Array filter

Array find

Javascript Operators

Javascript Objects

Stack Overflow Survival Guide in English

  • Thank you so much! The first way worked perfectly.

Browser other questions tagged

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