Credit Card checker solution with using .reduceRight

let validateCred = array =>  {
  let sumArray = [];
 let summed = array.reduceRight(sum, currentValue, index) => {
    if(index === array.length - 1) {
      sum = currentValue;
    } else if (index%2 !== 0) {
      const s = currentValue * 2;
      sum += s > 9 ? s - 9 : s
    } else {
      sum += currentValue
    }
    return sum
  }, 0)
  //console.log(summed)
  if (summed%10 === 0) {
    return true;
  } else {
    return false;
  }
}

this will then turn the error;

let summed = array.reduceRight(sum, currentValue, index) => {
^
SyntaxError: Unexpected token .

Does anyone have an explanation? (the error pointing towards the dot in the array . reduceRight

Alright, I think I got it. I had to look up the Mozilla developer docs.

What you’re going for is this:
reduceRight((accumulator, currentValue, index) => { ... }, initialValue)
What you have is this:
reduceRight(accumulator, currentValue, index) => { ... }, initialValue)

You’re missing the opening parenthesis after opening reduceRight.

1 Like

Hi,

Thanks. You were absolutely right. Can’t believe neither me nor my mom (who herself works in coding) could figure it out

To be fair, I don’t think I would’ve caught it with your error message.
I threw it in JSFiddle, which throws this: Uncaught SyntaxError: Malformed arrow function parameter list

1 Like