Last active
September 5, 2018 05:14
-
-
Save kshitijpurwar/f0fc1621e477fc505c79a431a4218fbd to your computer and use it in GitHub Desktop.
Faster and simpler solution to the problem mentioned in the hackernoon article
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
// https://hackernoon.com/how-to-lose-an-it-job-in-10-minutes-3d63213c8370 | |
var city = "Tokyo"; | |
var cities = ['Tokyo', 'London', 'Rome', 'Donlon', 'Kyoto', 'Paris']; | |
function getScore(s){ | |
var s = s.toLowerCase(); | |
var sum = 0; | |
for(var i = 0; i < s.length;i++){ | |
sum += s.charCodeAt(i); | |
} | |
return sum; | |
} | |
function getScoreRefactored(str) { | |
return str.toLowerCase() | |
.split('') | |
.map(a => a.charCodeAt(0)) | |
.reduce((a,b) => a+b, 0); | |
} | |
console.log(`score for ${city} is ${getScore(city)}`); | |
// score for Tokyo is 566 | |
console.log(`score for kyoto is ${getScore("Kyoto")}`); | |
// score for kyoto is 566 | |
console.log(`city scores are ${cities.map(city => getScore(city))}`); | |
// city scores are 566,650,435,650,566,543 | |
// Now just collect the strings with equal scores in one array |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment