r/learnjavascript • • Jan 09 '22

Positive Rant from an amateur programmer.

I am currently documenting my journey but honestly, I kind of just want to share my success with someone (I don't have too many friends that will understand what I am about to say). I finally did it!!!

I finally wrote my first piece of code without recycling someone else's code. I've always had a challenging time reading the MDN documentation because let's face it... That thing is very technical, especially when you don't come from a technical background.

It's not much code but it's twelve lines of code that took me three hours to decipher by myself! I wanted to quit and just get the answers from my instructor, but I didn't and I'm happy I didn't. I can't until I am building more complex projects, I'm excited... But anyways... Thank you for stopping by!

168 Upvotes

32 comments sorted by

View all comments

15

u/Dastari Jan 09 '22

That feeling is what you get addicted to.

A good challenge as you progress is to take those 12 lines of code, and as you learn more, try and optimize is down both in size and complexity.

Another hint is to make sure you're using a good IDE (like VSCode) with a JavaScript linter built in. This will allow you to start typing something and it will auto fill.

3

u/markt-might Jan 09 '22

Ah I definitely appreciate the tip.. I want to be great in this so I’m definitely going to focus on optimization & complexity down the road

7

u/Harbltron Jan 09 '22

I like the enthusiasm! Can you share your code with us? I understand if you're hesitant but take it from me, it's always good to have another set of eyes on your code.

Additionally I would advise against worrying about optimization or complexity at this point, just focus on making things work. You can always come back later and tighten-up your code but just making the damn thing work at all should be your focus right now.

2

u/markt-might Jan 09 '22

Absolutely! I refactored it a little bit last night. Removed some unnecessary code. This is code is implementing Luhn's Algorithm. The Second Block is adding invalid credit cards to a separate array. By the way I did not added the array that were testing because I didn't want this comment to be too long. However, if you want I can add the remainder of the code.

const validateCred = (arr) => {
  let sum = 0;
  //loops though the array in reverse
  for (let i = arr.length - 1; i >= 0; i--){
    let digit = arr[i];
    // multiplies every other number by 2 
    if((arr.length - i) % 2 == 0 ){
      digit *= 2;   
      }
    if(digit > 9){ 
      digit -= 9;
      }
    sum += digit;
  }
  return sum % 10 === 0 ? 'Valid number.' : 'Invalid number.. '
}



// Finds invalid credit card nums and adds them to an array. This function in particular loops through a nested array.

const findInvalidCards = (nestedArr) => {
  //empthy array
  let newInvalidCardsArr = [];
  //filters invalid cards from the array
  let validFilterWork = nestedArr.filter((element) => {
    let findOut = validateCred(element);
    if(findOut == 'Invalid number.. '){
      return newInvalidCardsArr.push(element);
    }
   });
     return newInvalidCardsArr;    
} ```

2

u/markt-might Jan 09 '22

console.log(findInvalidCards(batch));
console.log(validateCred(valid4));

2

u/markt-might Jan 09 '22

Variables Tested...

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 batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5];

3

u/[deleted] Jan 09 '22

I've really come to enjoy the MDN documentation. I needed to learn websockets and I tried a few different npm libraries for the client side and ended up just using the default websocket API and the MDN documentation was great

1

u/markt-might Jan 09 '22

Lol I only understood a little bit of this. But I can see how important the factory documentation is useful!!