Optimize coding/ choose correct way to code

I would like to get some suggestions from experts/advanced developers on my current situation. I am currently on the second last project of JavaScript Syntax, Part II Crdit Card Checker. I have solved many problems/projects that came on the way.

Credit Card Checker took me 3 hours to write down a plan(algorithm) on paper, implement it on the computer, including required fixes during implementation. After that, I checked the Codecademy solution and realized that it could be done easily. There is a good thing that I never need to see the hint, one way or another, I always find the solution but in a long and complex way. Mentioned code below

For example: while implementing _chunk() method in Loadash I didn’t know that in slice(start,end) end can handle numbers greater than array. length and automatically selects the last remaining elements for a chunk. This ended me implementing the entire functionality of handling the last chunk with lesser elements than whatever we specified in chunksize chunk(array,chunkSize). It was frustrating to miss a line from the documentation and waste time&energy but
I learned my lesson regarding documentation.

how can I improve this and code more efficiently in less time, or one can say choose the rightest solution for the problem quickly

// All valid credit card numbers
const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];
const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9];
const valid3 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6];
const valid4 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5];
const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6];

// All invalid credit card numbers
const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5];
const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3];
const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4];
const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5];
const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4];

// Can be either valid or invalid
const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4]; //invalid
const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9]; //valid
const mystery3 = [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3]; //invalid
const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3]; //invalid
const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3]; //valid

// An array of all the arrays above
const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5];


// Add your functions below:
const validateCred = ccNo => {
  let firstStepArray = []
  let seconeStepValue = 0;
  let doubleFlipper;
  const firstStep = (x,index) => {
    if (index == 0) {
      firstStepArray.push(x);
      doubleFlipper = true;
      return;
    }
    if (doubleFlipper) {
      let nextValue = x*2
      if (nextValue > 9) {
      nextValue = nextValue - 9;
      firstStepArray.push(nextValue);
      }
     else {firstStepArray.push(nextValue);}
      doubleFlipper = false;
      return;
    }
    if (!doubleFlipper) {
      firstStepArray.push(x);
      doubleFlipper = true;
    }
  };
  const secondStepTotal = () => {
  for (element in firstStepArray) {
    seconeStepValue += firstStepArray[element];
  }
  return seconeStepValue;
}
const thirdStepCheck = () => seconeStepValue % 10 == 0 ? 'Valid':'Invalid'

  ccNo.slice().reverse().forEach((element,index) =>       {firstStep(element,index)})
  firstStepArray = firstStepArray.reverse()
  
  secondStepTotal()
  return thirdStepCheck()
  
};

const findInvalidCards = batch => {
  let secondStepResultArray = []; 
  batch.forEach( element => {
   secondStepResultArray.push(validateCred(element))
  })
  console.log(secondStepResultArray)
  return secondStepResultArray;
}

const idInvalidCardCompanies = batch => {
  let invalidCreditCards = [];
  let InvalidCardCompanies = [ ];
  batch.forEach( element => {
    if(validateCred(element) == 'Invalid') {
      invalidCreditCards.push(element)
    }
  })
    invalidCreditCards.forEach(element => {
     switch(element[0]) {
      case 3:
        if (InvalidCardCompanies.indexOf('Amex') == -1)
        {InvalidCardCompanies.push('Amex');}
        break;
      case 4:
        if (InvalidCardCompanies.indexOf('Visa') == -1)
        {InvalidCardCompanies.push('Visa');}
        break;
      case 5:
        if (InvalidCardCompanies.indexOf('Mastercard') == -1)
        {InvalidCardCompanies.push('Mastercard');}
        break;
      case 6:
        if (InvalidCardCompanies.indexOf('Discover') == -1)
        {InvalidCardCompanies.push('Discover');}
        break;
      default:
        console.log('Company not found”');
    } 
    })
  console.log(invalidCreditCards.length)
  console.log(InvalidCardCompanies)
  
}
idInvalidCardCompanies(batch);

As you can see I created Unnecessary functions, variables to handle the problem

Hi,

Coming to solutions quicker is a matter of focused practice and experience (and best practices for this is a long topic).
I would add that coming to the solution is often not going to be enough, because you also want your solution to be efficient with time/space complexity. Then you might also have to deal with things like, what is a proper way to test this type of solution methodically (for validity).

In the very beginning there is no shortcut to doing problems fast. Itzhak Perlman loves to say “if you learn fast, you forget fast. if you learn slow you forget slow” (though I don’t know who’s quote it originally is).

There’s often a lot of things to consider while working slowly through a problem, and even if your answer is not the most efficient (time/space wise), it might actually be a perfect answer for a different set of problems e.g. recursion is really inefficient for working with fibonacci type problems, but can do certain tree traversals quite well.

1 Like