add: solution to project euler problem 22 md file (#38263)

* add: solution to project euler 22 md file

* Copied the array instead of using it directly

Co-Authored-By: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
Sanket Kogekar
2020-03-14 03:01:13 +05:30
committed by GitHub
parent 4ef78f7f7d
commit 705cc32579

View File

@ -75,7 +75,27 @@ const names = ['MARY','PATRICIA','LINDA','BARBARA','ELIZABETH','JENNIFER','MARIA
<section id='solution'>
```js
// solution required
function nameScoreCalc(word) {
let sum = 0;
let alphabets = ["", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
for (let i = 0 ; i < word.length; i++)
sum += alphabets.indexOf(word[i].toLowerCase());
return sum;
}
function namesScores(arr) {
arr = [...arr].sort();
arr.unshift('');
let total = 0;
for (let i = 1 ; i < arr.length; i++)
total += nameScoreCalc(arr[i]) * i;
return total;
}
const test1 = ['THIS', 'IS', 'ONLY', 'A', 'TEST'];
const test2 = ['I', 'REPEAT', 'THIS', 'IS', 'ONLY', 'A', 'TEST'];
```
</section>