Skip to content

Instantly share code, notes, and snippets.

@sergey12313
Last active April 4, 2018 13:12
Show Gist options
  • Save sergey12313/53fd1eef7b77451dc3479c2745bc3ea5 to your computer and use it in GitHub Desktop.
Save sergey12313/53fd1eef7b77451dc3479c2745bc3ea5 to your computer and use it in GitHub Desktop.
I Spy
https://www.codewars.com/kata/555185132c0d4cca3d000197
function spyOn(func) {
let callCount = 0;
let args = [];
let results = [];
const result = (...arg) => {
args.push(...arg)
const returnValue = func.apply(this, arg);
callCount += 1;
results.push(returnValue)
return returnValue;
};
result.callCount = () => callCount;
result.returned = value => results.includes(value);
result.wasCalledWith = value => args.includes(value);
return result;
}
----------------------------------------------------------------------------------------------
Validate Credit Card Number
https://www.codewars.com/kata/5418a1dd6d8216e18a0012b2
function validate(n){
const arr = n.toString().split('');
let sum = 0;
for (let i = 0, length = arr.length; i < length; i += 1) {
const condition = length % 2 === 0 ? i % 2 === 0 : i % 2 !== 0;
const elem = arr[i];
let currentResult = elem;
if (condition) {
let mult = elem * 2;
currentResult = mult > 9 ? mult - 9 : mult;
}
sum += +currentResult;
}
return (sum % 10 == 0)
}
----------------------------------------------------------------------------------------------
Creating a string for an array of objects from a set of words
https://www.codewars.com/kata/5877786688976801ad000100
function wordsToObject(input) {
const arr = input.split(' ');
const result = arr.reduce((acc, cur, index, arr) => {
if(index % 2 === 0) acc +=
`${(index === 0)?'':', '}{name : \'${cur}\', id : \'${arr[index+1]}'\}`
return acc;
},'[');
return `${result}]`;
}
----------------------------------------------------------------------------------------------
Find the unique number
https://www.codewars.com/kata/585d7d5adb20cf33cb000235
function findUniq(arr) {
if(arr[0] !== arr[arr.length - 1]) {
if(arr[0] === arr[1]) {
return arr[arr.length - 1]
} else {
return arr[0]
}
}
let result;
for(let i = 1; i < arr.length - 1; i += 1 ){
if(arr[i] !== arr[0]) {
result = arr[i];
break;
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment