Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

82
Visualizações
How can I get results from a given array with following conditions?

I'm a beginner in JavaScript and doing an online test to improve my skills. I came across to this question:

  1. Add 1 point for each even number in the array
  2. Add 3 points for each odd number in the array
  3. Add 5 points every time the number 5 appears in the array

So if I am given this array as an example: ([1,2,3,4,5]) the output should be 13

This is what I have got so far:

export function find_total( my_numbers ) {
  
  for(let i = 0; i < my_numbers.length; i++){
  if(my_numbers[i] % 2 === 0) {
    total = total + 1
  }
  if(my_numbers[i] % 2 === 1) {
    total = total + 3
  }
  if(my_numbers[i] === 5) {
    total = total + 5
  }
  return total
  }
  console.log(total)
}

But it is giving me errors. I get the logic in English but couldn't put it in JS. What would be the right syntax here?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The problem is with your if statements.

function find_total( my_numbers ) {
  let total = 0;

  for(let i = 0; i < my_numbers.length; i++){
    if(my_numbers[i] === 5) {
      total = total + 5
    }
    else if(my_numbers[i] % 2 === 0) {
      total = total + 1
    }
    else if(my_numbers[i] % 2 === 1) {
      total = total + 3
    }
  }
  
  return total;
}

console.log(find_total([1,2,3,4,5]))

This code should print the correct result of 13. You were not getting an answer of 13 because 5 is odd so has 3 point and 5 point exclusive to 5. The key is not to treat 5 as having point of odd number but having it's own point irrespective of either it is odd or even.

about 4 years ago · Juan Pablo Isaza Relatório

0

There are following issues in your code:

  1. total in the function find_total is never initialized
  2. return statement is misplaced inside the for loop, it must be outside, signifying return value of the function find_total
  3. Better to console.log the value of the function, instead of logging it inside. eg. console.log(find_total(...))

Fixing these, you will indeed get the return value as 16 for input [1, 2, 3, 4, 5] as pointed out in the comments.

function find_total(my_numbers) {

  let total = 0 // Initialize total with a value of 0

  for (let i = 0; i < my_numbers.length; i++) {
    if (my_numbers[i] % 2 === 0) {
      total = total + 1
    }
    if (my_numbers[i] % 2 === 1) {
      total = total + 3
    }
    if (my_numbers[i] === 5) {
      total = total + 5
    }
  }

  return total // return outside the loop
}

console.log(find_total([1, 2, 3, 4, 5])) // console.log outside

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda