Skip to content

Instantly share code, notes, and snippets.

@kshitijpurwar
Last active September 5, 2018 05:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kshitijpurwar/f0fc1621e477fc505c79a431a4218fbd to your computer and use it in GitHub Desktop.
Save kshitijpurwar/f0fc1621e477fc505c79a431a4218fbd to your computer and use it in GitHub Desktop.
Faster and simpler solution to the problem mentioned in the hackernoon article
// 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