skip to Main Content

Attempting to remove all even numbers from the array.
I have managed to remove some but there are still two persisting. I am unsure what to do as I have very new to coding and only know the very basics.

function findEfficientBulbs(serialNumbers) {
console.log(serialNumbers);
const efficientSerialNumbers = [];
// Below

//create a loop to go through the array
for (let i = 0; i < serialNumbers.length; i++){
    //define a varible to look through the array via loop 
    const currentBulb = serialNumbers[i]
    //create if statement to find odd numbers and has 6 digits
    // remove items containing even numbers
        if (currentBulb === 6 && currentBulb % 2 === 1 ){
            //push variable into efficient array
            efficientSerialNumbers.push(currentBulb)
        }
}

return efficientSerialNumbers;
}    

error I received:

✕ AssertionError: expected [] to deeply equal [ 234567, 456789 ]

logs

[ 123456, 234567, 345678, 456789 ]

I don’t understand why numbers 123456 and 345678 came back.

6

Answers


  1. It seems like you want to remove all even numbers with exactly 6 digits from the serialNumbers array and store the remaining odd numbers in the efficientSerialNumbers array. However, your current code is checking if currentBulb is equal to 6 and if it’s odd, which is why it’s not working as expected. To fix this issue, you should check if the length of the number is 6 and if it’s odd. Here’s the corrected code:

    function findEfficientBulbs(serialNumbers) {
      console.log(serialNumbers);
      const efficientSerialNumbers = [];
    
      for (let i = 0; i < serialNumbers.length; i++) {
        const currentBulb = serialNumbers[i];
    
        // Check if the number has 6 digits and is odd
        if (currentBulb.toString().length === 6 && currentBulb % 2 === 1) {
          efficientSerialNumbers.push(currentBulb);
        }
      }
    
      return efficientSerialNumbers;
    }
    

    In this code, currentBulb.toString().length === 6 checks if the length of the number is 6 digits, and currentBulb % 2 === 1 checks if it’s an odd number. This should correctly filter out even numbers with 6 digits from the array.

    Login or Signup to reply.
  2. If you want to simplify your function, you can use filter. And only return anything that isn’t even. Also for length to work, the value needs to be a string not numeric.

    function findEfficientBulbs(serialNumbers) {
      return serialNumbers.filter((e) => e % 2 > 0 && String(e).length === 6)
    }  
    
    console.log(findEfficientBulbs([123456, 234567, 345678, 456789]))
    Login or Signup to reply.
  3. Remove currentBulb === 6 from the code and use like below,

    function findEfficientBulbs(serialNumbers) {
        console.log(serialNumbers);
        const efficientSerialNumbers = [];
    
        // Loop through the array
        for (let i = 0; i < serialNumbers.length; i++) {
            const currentBulb = serialNumbers[i];
    
            // Check if the current number is odd
            if (currentBulb % 2 === 1) {
                efficientSerialNumbers.push(currentBulb);
            }
        }
    
        return efficientSerialNumbers;
    }
    
    const serialNumbers = [123456, 234567, 345678, 456789];
    const result = findEfficientBulbs(serialNumbers);
    console.log(result);
    
    Login or Signup to reply.
  4. You can compare the number so it would be between 100000 and 999999 to have it 6 digits long:

    function findEfficientBulbs(serialNumbers) {
      return serialNumbers.filter((e) => e % 2 > 0 && e >= 10000 && e < 1000000)
    }  
    
    console.log(findEfficientBulbs([123456, 234567, 345678, 456789, 1233421, 9999]))
    Cycles: 10000000 / Chrome/117
    -----------------------------------------------------
    Alexander      151/min  1.0x  163  163  151  169  162
    Hitesh Kumar   292/min  1.9x  292  298  295  309  322
    -----------------------------------------------------
    https://github.com/silentmantra/benchmark
    
    <script benchmark="10000000">
    
    const serialNumbers = [123456, 234567, 345678, 456789, 1233421, 9999];
    
    // @benchmark Hitesh Kumar
    
    function findEfficientBulbs(serialNumbers) {
      const efficientSerialNumbers = [];
    
      for (let i = 0; i < serialNumbers.length; i++) {
        const currentBulb = serialNumbers[i];
    
        // Check if the number has 6 digits and is odd
        if (currentBulb.toString().length === 6 && currentBulb % 2 === 1) {
          efficientSerialNumbers.push(currentBulb);
        }
      }
    
      return efficientSerialNumbers;
    }
    
    // @run
    findEfficientBulbs(serialNumbers) 
    
    // @benchmark Alexander
    serialNumbers.filter((e) => e % 2 > 0 && e >= 10000 && e < 1000000)
    </script>
    <script src="https://cdn.jsdelivr.net/gh/silentmantra/benchmark/loader.js"></script>
    Login or Signup to reply.
  5. function findEfficientBulbs(serialNumbers) {
    return serialNumbers.filter(el => el % 2 !== 0 && el.toString().length === 6);
    }
    
    const results = findEfficientBulbs([123456, 123458];
    console.log(results);
    

    Since you are trying to remove the numbers having a length of 6 and the even numbers from an array of numbers, the above code snippet might work well for you. Here the code is filtering out the items from the array by using the above conditions. It will return a new array without the even numbers.

    Login or Signup to reply.
  6. Others here have brilliantly explained the correct way of achieving what you want, but I wanted to help explain what you did wrong.

    The crux of the problem lies here:

    if (currentBulb === 6 && currentBulb % 2 === 1 ){
    

    This statement is essentially saying "currentBulb has the value of 6 AND is an odd number" which will ALWAYS resolve to false, and thus always result in an empty array.

    The error message you got explains this further:

    ✕ AssertionError: expected [] to deeply equal [ 234567, 456789 ]
    

    I believe what led you to think that your code just wasn’t removing even numbers was this line:

    console.log(serialNumbers);
    

    You printed out your starting array rather than the array that was outputted, which would show all numbers.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search