Last active
December 10, 2020 06:31
-
-
Save pllearns/5951b41705b187ab91b2585259668bd6 to your computer and use it in GitHub Desktop.
CodeFights Solutions
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
function adjacentElementsProduct(inputArray) { | |
var numbers = -100 | |
for (var i = 0; i < inputArray.length; i++) { | |
if (inputArray[i] * inputArray[i + 1] >= numbers) { | |
numbers = inputArray[i] * inputArray[i + 1] | |
} | |
} | |
return numbers | |
} | |
adjacentElementsProduct([3, 6, -2, -5, 7, 3]) |
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
function centuryFromYear(year) { | |
return Math.ceil(year / 100) | |
} | |
centuryFromYear(1905) |
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
function checkPalindrome(inputString) { | |
var reversedString = '' | |
for(var i = inputString.length; i > 0; i--) { | |
reversedString += inputString[i - 1] | |
} | |
return isSame(reversedString, inputString) | |
} | |
function isSame(reversedString, inputString) { | |
if (reversedString === inputString) { | |
return true | |
} else { | |
return false | |
} | |
} |
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
function firstDuplicate(a) { | |
var findFirstDup = {} | |
for (var i of a) { | |
if (findFirstDup[i]) return i | |
findFirstDup[i] = i | |
} | |
return -1 | |
} |
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
function firstNotRepeatingCharacter(s) { | |
var noRepeatChar = s.split('').filter(function (char, i, string) { | |
return string.indexOf(char) === string.lastIndexOf(char) | |
}).shift() | |
if (!noRepeatChar) { | |
return '_' | |
} | |
return noRepeatChar | |
} |
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
function isCryptSolution(crypt, solution) { | |
let expectedResult = true | |
solution = solution.reduce((acc, index) => { | |
acc[index[0]] = index[1] | |
return acc | |
}, {}) | |
let result = crypt.map(word => { | |
let num = '' | |
word.split('').forEach(letter => { | |
num += solution[letter] | |
}) | |
return num | |
}) | |
if ((+result[0] + +result[1] !== +result[2])) { | |
expectedResult = false | |
} | |
if ((result[0][0] === '0' || result[0][1] === '0' && result [2][0] === '0'|| result[1][0] === '0' && result[1][1] === '0') && result[0].length !== 1) { | |
expectedResult = false | |
} | |
return expectedResult | |
} |
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
function makeArrayConsecutive2(statues) { | |
var sortStatues = statues.sort(function(a, b) { | |
return (a - b) | |
}) | |
var neededStatues = 0 | |
if (sortStatues.length -1 === 0) { | |
return 0 | |
} else { | |
for (i = 0; i <= sortStatues.length - 2; i++) { | |
neededStatues += (sortStatues[i + 1] - sortStatues[i] - 1) | |
} | |
return neededStatues | |
} | |
} |
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
function rotateImage(a) { | |
var newImgStore = [] | |
var img = a.length | |
for (var i = 0; i < img; i++) { | |
var holdImg = [] | |
for (var j =0; j < img; j++) { | |
var moveImg = a[j][i] | |
holdImg.unshift(moveImg) | |
} | |
newImgStore.push(holdImg) | |
} | |
return newImgStore | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment