Created
May 5, 2018 20:38
-
-
Save Rolando-Barbella/0727f28151213a1497ed82d588ad8b53 to your computer and use it in GitHub Desktop.
Code wars kata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var isSquare = function (n) { | |
return n >= 0 && Math.sqrt(n) % 1 === 0 ? true : false; | |
} | |
// Sum of a sequence | |
const sequenceSum = (begin, end, step) => { | |
if (begin > end) { | |
return 0; | |
} | |
return begin + sequenceSum(begin + step, end, step); | |
}; | |
///Remove the smallest element of the array | |
function removeSmallest(numbers) { | |
const minimun = numbers.sort((a, b) => a - b)[0] | |
const forDeletion = [ minimun ] | |
return numbers.filter(x => !forDeletion.includes(x)); | |
} | |
/* Second vs*/ | |
function removeSmallest(numbers) { | |
var smallest = Math.min(...numbers); | |
numbers.splice(numbers.indexOf(smallest), 1); | |
return numbers; | |
} | |
// Firts and last str | |
str = 'hello' | |
function removeChar(str) { | |
return str.slice(1, -1); | |
} | |
// String to number | |
var stringToNumber = function(str) { | |
return +str; | |
}; | |
// Find the number with the most digits. | |
//If two numbers in the argument array have the same number of digits, return the first one in the array. | |
array = [2,3,4000]; | |
function findLongest(array) { | |
const arrayToString = array.map(String); | |
const sortByMaxLength = arrayToString.sort((a, b) => b.length > a.length)[0]; | |
return parseFloat(sortByMaxLength); | |
} | |
/* Second vs*/ | |
function findLongest(array){ | |
return array.reduce((a, b) => ( | |
String(a).length > String(b).length) ? a : b | |
); | |
} | |
function printerError(s) { | |
const validCode = "abcdefghijklm".split(""); | |
const invalidCode = []; | |
for (let i of s) { | |
if (!validCode.includes(i)) { | |
invalidCode.push(i); | |
} | |
} | |
return invalidCode.length + "/" + s.length; | |
} | |
// String with no repeted letters: 'abaa'= false, 'abc' = true | |
function isIsogram(str) { | |
return new Set(str.toLowerCase()).size === str.length; | |
} | |
// Your task is to create functionisDivideBy (or is_divide_by) to check if an integer number is divisible by each out of two arguments. | |
// (-12, 2, -6) -> true | (-12, 2, -5) -> false | |
function isDivideBy(number, a, b) { | |
return [a, b].every(i => number % i === 0); | |
} | |
// Replace numbers for string | |
function stringClean(s) { | |
// Function will return the cleaned string | |
return s.replace(/\d/g, ""); | |
} | |
// Write a function that takes a single string (word) as argument. The function must return an ordered list containing the indexes of all capital letters in the string. | |
// Test.assertSimilar( capitals('CodEWaRs'), [0,3,4,6] ); | |
var capitals = function(word) { | |
return word.split("").reduce(function(memo, v, i) { | |
return v === v.toUpperCase() ? memo.concat(i) : memo; | |
}, []); | |
}; | |
// Write a function named sumDigits which takes a number as input and returns the sum of the absolute value of each of the number's decimal digits | |
// sumDigits(99); // Returns 18 - sumDigits(-32); // Returns 5 | |
function sumDigits(number) { | |
return Math.abs(number) | |
.toString() | |
.split("") | |
.reduce(function(a, b) { | |
return +a + +b; | |
}, 0); | |
} | |
// We want to know the index of the vowels in a given word, for example, there are two vowels in the word super (the second and fourth letters). | |
const vowelIndices = word => | |
[...word].reduce( | |
(ac, cur, i) => (/[aeiouy]/i.test(cur) ? [...ac, i + 1] : ac), | |
[] | |
); | |
// Complete the solution so that it returns true if the first argument(string) passed in ends with the 2nd argument (also a string). | |
// Examples: | |
// solution('abc', 'd') // returns false | |
function solution(str, ending) { | |
return str.endsWith(ending); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment