## DEV Community is a community of 704,743 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# 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.

# 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

### ๐จโ๐ป 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);
}

### ๐ 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]

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]

Sum all of the final digits:

[8, 9, 1] ==> 8 + 9 + 1 = 18

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

### ๐ค 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;
}

### ๐ 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;
}
##### 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)

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)

Youssef Rabei

Thanks, it did improve the time complexity too

Oฤuzhan Olguncu

Shortened it even more. Cheers.

Youssef Rabei • Edited

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

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.