0

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?

Anurag Srivastava
  • 14,077
  • 4
  • 33
  • 43
EsinYil
  • 49
  • 1
  • 8
  • 4
    _“the output should be 13”_ — Why? You have two even numbers, three odd numbers, and one five. That’s 1 + 1 + 3 + 3 + 3 + 5 = 16. – Sebastian Simon Mar 06 '22 at 15:10
  • 2
    The error I get is _“Uncaught ReferenceError: `total` is not defined”_. [_“In strict mode, assignment to an undeclared identifier is a **ReferenceError**.”_](/a/1471738/4642212). _“The [`export`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/export) statement is used when creating JavaScript modules […] Exported modules are in strict mode […]”_. Put `let total = 0;` inside the function, at the top. `return` exits a function. You unconditionally exit the function _inside_ the loop. Obviously, it should be after the loop. – Sebastian Simon Mar 06 '22 at 15:17
  • And of course, if you get _“Uncaught SyntaxError: `export` declarations may only appear at top level of a module”_, then see [ES2015 import doesn't work (even at top-level) in Firefox](/q/37624819/4642212) and read the [documentation](//developer.mozilla.org/docs/Web/JavaScript/Guide/Modules). – Sebastian Simon Mar 06 '22 at 15:25

2 Answers2

1

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.

codedump
  • 347
  • 1
  • 4
  • 13
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
Anurag Srivastava
  • 14,077
  • 4
  • 33
  • 43
  • 1
    If you properly fix your logic as per my answer you'll get an answer of 13. – codedump Mar 06 '22 at 15:18
  • 2
    Is 5 not an odd number? – Anurag Srivastava Mar 06 '22 at 15:20
  • It is. But as per the question if the answer needs to be 13(as stated by op) then the number 5 should not contribute to odd point(3) but it's own point(5), which yields the correct answer as required by OP. – codedump Mar 06 '22 at 15:23
  • 1
    Thank you @AnuragSrivastava this is very helpful! I knew I was somehow close, thanks for explaining it step by step. – EsinYil Mar 06 '22 at 15:28
  • 1
    In this case, it's asking for number 5 not being counted as a regular odd number. So I had to change it slightly to this: if (my_numbers[i] % 2 === 1 && my_numbers[i] !== 5) { total = total + 3 } – EsinYil Mar 06 '22 at 15:29
  • You're much welcome @EsinYil. – codedump Mar 06 '22 at 15:33