Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

89
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!