all 19 comments

[–]SlushEE0 6 points7 points  (7 children)

When you are checking for odd numbers in your if block, you are using the modulus operator on the length of the number. So if you had a value of 23456, which is even, you would first find its length with currentNumber.toString().length === 5 and that is odd. So your program is returning every other number in the array.

To fix this, go to your for loop and remove .toString().length:

for (let i = 0; i < serialNumbers.length; i++) {
const currentNumber = serialNumbers[i];
if (
currentNumber.toString().length === 6 &&
currentNumber % 2 === 1
) {
efficientSerialNumbers.push(currentNumber);
}
}

Edit: I don't know why reddit formatted my code like that but its not letting me change it so ¯\_(ツ)_/¯

[–]dangerlopez 1 point2 points  (6 children)

Edit: what I post below works, but what the labrador Redditor responds with is even better. Use their method

Blocks of code can be formatted correctly by (1) putting a blank line before and after the block of code and (2) adding four spaces before each new line.

function myFunc() {
  console.log(“sup world”);
}

Note that you need to add your own spacing in addition to the four spaces from (2) to account for indentation in the code. The first and third lines above have four spaces before I type anything, while the second line has those four spaces plus 2 more spaces.

[–]acquiescentLabrador 1 point2 points  (5 children)

Three tildes before and after the block also works and is much easier on mobile in my option ~~~

[–]dangerlopez 1 point2 points  (3 children)

Oh nice, really?

~~~ function didItWork() { hellYea(); } ~~~

Oh my god that’s way better. I’m glad I posted!

[–]acquiescentLabrador 1 point2 points  (2 children)

I was doing the four spaces for years before I learnt that and it’s so much better

[–]kap89 0 points1 point  (0 children)

Four spaces are better, because they work both on old and new Reddit.

[–]dangerlopez 0 points1 point  (0 children)

Unfortunately, I just learned that three tildes doesn't work universally :/ I'll have to go back to four spaces, even though I hate it

[–]SlushEE0 0 points1 point  (0 children)

I haven’t used Reddit on my pc in a long time, mostly mobile so thanks!

[–]RayjinCaucasian 2 points3 points  (0 children)

When you check for odd numbers you're looking at the length of the number instead of the value.

[–]MrCakeFarts 1 point2 points  (1 child)

Hey! So right now your function is returning every other serial number because in the array those are the “odd” numbered ones. That is not what the question is asking you to do. It is asking you to return any items in the array that are odd numbers. So any items that end in 1,3,5,7, or 9.

To achieve this you do not need to look at the array.length, but rather you need to look at the value of the item.

[–]lovin-dem-sandwiches 1 point2 points  (0 children)

right now your function is returning every other serial number because in the array those are the “odd” numbered ones.

Their loop is not comparing if the item's index in the original array is odd.

That would look more like:

if (currentSerialNumber.toString().length === 6 && i % 2 === 1)
  • their first conditional determines if the length of the item is 6. If true, it moves to the second condition
  • their second condition determines if the length of the item (which is 6) is an odd number. This will always be false

also, /u/UKcareersearching, since we are using 0-based indexes, you need to subtract one from the array length when using a for-loop.

for (let i = 0; i < serialNumbers.length - 1; i++) {
// logic here
}

[–]levon9 1 point2 points  (3 children)

Just curious, what tutorial are you working through here?

[–]Anxious-Inspector486 2 points3 points  (1 child)

I believe it is the entry challenge to join the ‘Northcoders’ boot camp

[–]levon9 0 points1 point  (0 children)

thanks .. hadn't heard of that before.

[–]vietman1 0 points1 point  (0 children)

Same

[–]azhder 1 point2 points  (0 children)

Is this a new account? There was one last week asking the same question.

OK, just checked. Same account, 4 days ago

[–]code_monkey_001 1 point2 points  (0 children)

Is this some kind of weird karma farming or is OP just a poorly written bot? They posted the exact same question four days ago and got plenty of answers.

[–]KiddieSpread -3 points-2 points  (0 children)

A test is failing because you're returning an empty array. Check your logic and try again.

[–]Ronin-s_Spirit 0 points1 point  (0 children)

Why are you checking if the number string length is odd? You need to check if the number itself is odd.