Back

Technologies:

javascriptjavascript
avatar
Tolerim
a month ago

Struggling with a JavaScript for-loop challenge, trying to sort data in an array.

My goal is to extract a subset of serial numbers that are both 6 digits long and odd. I have written some code that seems to perform this task, but it is letting some even numbers enter the final array of efficient serial numbers. Below is the code that I am using, where I am logging the original serial numbers to the console, creating an empty array, and running a for loop that checks if the current serial number is 6 digits long and if it is odd. If it passes these conditions, it is added to the efficient serial numbers array:

function findEfficientBulbs(serialNumbers) {
  console.log(serialNumbers);
  const efficientSerialNumbers = [];
  for (let i=0; i<serialNumbers.length; i++) {
    if (serialNumbers[i].length === 6 && serialNumbers[i] % 2 !== 0) {
      efficientSerialNumbers.push(serialNumbers[i])
    }
  }
  return efficientSerialNumbers;
}
Despite the condition that serialNumbers[i] % 2 !== 0 should prevent even numbers from being added, some still sneak through. Can you help me identify the issue?

Answers(2)
avatar
Vogi
a month ago
Verified Answer
The issue with your code is that the condition serialNumbers[i] % 2 !== 0 is not checking if the number is odd. Instead, it is checking if the remainder of the number divided by 2 is not equal to 0. This means that any number that has a remainder of 1 when divided by 2 will be considered odd, but any number that has a remainder of -1 when divided by 2 will also be considered odd. This is why some even numbers are still being added to the final array. To fix this issue, you can use the modulus operator % along with an if conditional statement to check if a number is odd or even. Here's an example:
console.log(serialNumbers);
  const efficientSerialNumbers = [];
  for (let i=0; i<serialNumbers.length; i++) {
    if (serialNumbers[i].length === 6 && serialNumbers[i] % 2 === 1) {
      efficientSerialNumbers.push(serialNumbers[i])
    }
  }
  return efficientSerialNumbers;
}
In this example, we are checking if the remainder of the number divided by 2 is equal to 1, which means that the number is odd. This should fix the issue and ensure that only odd numbers are added to the final array. I hope this helps! Let me know if you have any other questions.

Sources

How to Check if a Number is Even or Odd in JavaScriptTesting whether a value is odd or evenHow to determine if a number is odd in JavaScriptJavascript Program to Check if a Number is Odd or Even
avatar
Tolerim
a month ago
The issue with your code is that serialNumbers[i] % 2 !== 0 only checks for oddness in the last digit of the number, not the entire number itself. To check if a number is odd, you'll need to modify your code to check if the first digit of the number is odd. Here's an updated version of your function that should work as expected:
function findEfficientBulbs(serialNumbers) {
    const efficientSerialNumbers = [];
    for (let i=0; i<serialNumbers.length; i++) {
        if (serialNumbers[i].length === 6 && Number(serialNumbers[i][0]) % 2 !== 0) {
            efficientSerialNumbers.push(serialNumbers[i])
        }
    }
    return efficientSerialNumbers;
}
In this version, we're checking if the first digit of the serial number is odd by using Number(serialNumbers[i][0]) to convert the first digit to a number and checking if it's odd using % 2 !== 0.
;