DEV Community

loading...
Cover image for Day 04: 30 Days of Codewars.js

Day 04: 30 Days of Codewars.js

Youssef Rabei
I'm a 17 years old Javascript enthusiast, Front-End Web Developer 👨‍💻, Musician 🎻 🎹 and a Photographer 📷, Who's probably making coffee ☕ right now.
・3 min read

Table Of Contents


Multiples of 3 or 5 : ✍ by jhoffner

📃 Description

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

🤔 Thinking

I'm gonna make a for loop will check if a number less than the limit divisible % by 3 or || 5
Then push it to an array
Then add it

👨‍💻 Code

const solution = num => {
  let divBy3Or5 = [];

  for(let i = 1; i < num; i++) {
    ((i % 3 === 0) || (i % 5 === 0)) ? divBy3Or5.push(i) : null;  
  }

  return divBy3Or5.reduce((acc, elm) => acc + elm, 0);
}
Enter fullscreen mode Exit fullscreen mode

🐞 Bugs

I think it's a stupid code like it has to be a better way to solve this kata
Please if you have a better solution let me know in the comments


Validate Credit Card Number : ✍ by mcclaskc

📃 Description

In this Kata, you will implement the Luhn Algorithm, which is used to help validate credit card numbers.
Given a positive integer of up to 16 digits, return true if it is a valid credit card number, and false if it is not.

Here is the algorithm:

Double every other digit, scanning from right to left, starting from the second digit (from the right).
Another way to think about it is: if there are an even number of digits, double every other digit starting with the first; if there are an odd number of digits, double every other digit starting with the second:

1714 ==> [1*, 7, 1*, 4] ==> [2, 7, 2, 4]

12345 ==> [1, 2*, 3, 4*, 5] ==> [1, 4, 3, 8, 5]

891 ==> [8, 9*, 1] ==> [8, 18, 1]
Enter fullscreen mode Exit fullscreen mode

If a resulting number is greater than 9, replace it with the sum of its own digits (which is the same as subtracting 9 from it):

[8, 18*, 1] ==> [8, (1+8), 1] ==> [8, 9, 1]

// OR

[8, 18*, 1] ==> [8, (18-9), 1] ==> [8, 9, 1]
Enter fullscreen mode Exit fullscreen mode

Sum all of the final digits:

[8, 9, 1] ==> 8 + 9 + 1 = 18
Enter fullscreen mode Exit fullscreen mode

Finally, take that sum and divide it by 10. If the remainder equals zero, the original credit card number is valid.

18 (modulus) 10 ==> 8 , which is not equal to 0, so this is not a valid credit card number
Enter fullscreen mode Exit fullscreen mode

🤔 Thinking

I will create an array out of the credit card number
Then check its length if it's even I will loop over it starting in the first index 0 jumping one index at a time like 0, 2, 4, 6, n.length if its odd I will do the same but starting in the second element index number 1
Then double it and add them into another array and then sum it
Then divide it by 10 and check if its remainder is equal to 0

👨‍💻 Code

const validate = num => {
  let numArr = Array.from(String(num), Number);

  if (numArr.length % 2 === 0) {
    for(let i = 0; i< numArr.length; i+=2) {
      numArr[i] *= 2;
    }
  } else {
    for(let i = 1; i< numArr.length; i+=2) {
      numArr[i] *= 2;
    }
  }

  const lessThan18Arr = numArr.map(num => num > 9 ? num - 9 : num)

  const sum = lessThan18Arr.reduce((acc, elm) => acc + elm, 0)

  return sum % 10 === 0;
}
Enter fullscreen mode Exit fullscreen mode

🐞 Bugs

I think it's the Time complexity (Both Solutions takes about 1000ms give or take 100ms)
And there is repetitive code
Not DRY (Don't Repeat Yourself)

🏁 Finally

const validate = num => {
  let numArr = Array.from(String(num), Number);  
  let i = numArr.length % 2 === 0 ? 0 : 1; 

  while(i < numArr.length) {
    numArr[i] *= 2;
    i+=2;
  }

  const lessThan18Arr = numArr.map(num => num > 9 ? num - 9 : num)

  const sum = lessThan18Arr.reduce((acc, elm) => acc + elm, 0)

  return sum % 10 === 0;
}
Enter fullscreen mode Exit fullscreen mode
If you know a better way to solve any of the previous katas let me know in the comment

Thanks for reading, I really appreciate it.

Discussion (6)

Collapse
ogzhanolguncu profile image
Oğuzhan Olguncu • Edited

Solution for your first kata can be shortened as

const solution = num =>
  num.reduce((acc, elm) => acc + ((elm % 3 === 0 || elm % 5 === 0) && elm),  0)

Collapse
youssefrabeiii profile image
Youssef Rabei Author

Thanks, it did improve the time complexity too

Collapse
ogzhanolguncu profile image
Oğuzhan Olguncu

Shortened it even more. Cheers.

Collapse
youssefrabeiii profile image
Youssef Rabei Author • Edited

I just tried to submit it but it didn't work

Collapse
ogzhanolguncu profile image
Oğuzhan Olguncu

I have not checked the kata, but my solution assumes given parameter is Array. That might be the source of your problem.

Thread Thread
youssefrabeiii profile image
Youssef Rabei Author

I guessed that and it's one of the problems and i fixed it by transform the given number to an array of digits and then run your code over it but it returned 0