Mutating the array not returning correct values

Track: Full Stack
Exercise : Credit Card Checker

Problem: Pushing array values to an empty array returning unwanted values

code

const validateCred = (array) => {
    // remove last value from the array
    const newPoppedArray = array.pop();
    
    // Reverse the array that has been popped
    const reversedArray = array.reverse();
    
    // Get values from odd indexes in the reversedArray
    let oddArrays = [];
    for (let i = 0; i < reversedArray.length; i++) {
        oddArrays.push(reversedArray.splice(i, 1));
        console.log(oddArrays);
    }

}

validateCred(valid1);

Output

Hello, @ngugingugi.

I’m a little unclear on what the goal is here. If you want to construct an array that contains only the elements of the original array at odd indices, do you really need the Array.splice() method? What does the method promise to do that you find helpful for this task?

Granted, there are many, many ways to solve this challenge. It may help to make a plan on paper first. Write out the steps necessary to validate a card number. If you were to manually check a number, would you re-write the array multiple times? You could, but you wouldn’t have to. You could also examine each digit, and using a calculator sum the appropriate values without ever re-writing the array. Once you determine the necessary steps, you can instruct the computer to follow those same steps with code.

How you go about solving it is up to you. If you aren’t sure what a particular tool (function) does, you may need to do a little research.

Thanks you @midlindner , I might have to do more research

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.