JavaScript JavaScript Array Iteration Methods Array Manipulation Practice reduce()

victor escarcega
victor escarcega
6,640 Points

Using reduce

Not sure what I am missing, i have a feeling it has something to do with scope

app.js
const phoneNumbers = ["(503) 123-4567", "(646) 123-4567", "(503) 987-6543", "(503) 234-5678", "(212) 123-4567", "(416) 123-4567"];
let numberOf503;
numberOf503 = phoneNumbers.reduce((count, number) => {
    if(number[1] === '5') {
      return count + 1;
    }else return
}, 0);




// numberOf503 should be: 3
// Write your code below

1 Answer

Steven Parker
Steven Parker
194,962 Points

There are no scope issues here. But when the count is not incremented, you still need to retain the count:

    } else return count;  // without "count" it returns "undefined"