Table Of Contents
- Multiples of 3 or 5
- 📃 Description
- 🤔 Thinking
- 👨💻 Code
- 🐞 Bugs
- Validate Credit Card Number
- 📃 Description
- 🤔 Thinking
- 👨💻 Code
- 🐞 Bugs
- 🏁 Finally
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);
}
🐞 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, returntrue
if it is a valid credit card number, andfalse
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;
}
Top comments (6)
Solution for your first kata can be shortened as
Thanks, it did improve the time complexity too
Shortened it even more. Cheers.
I just tried to submit it but it didn't work
I have not checked the kata, but my solution assumes given parameter is Array. That might be the source of your problem.
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