https://www.codewars.com/kata/57eb8fcdf670e99d9b000272
Codewars - Achieve mastery through coding practice and developer mentorship
A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!
www.codewars.com
DESCRIPTION:
Given a string of words, you need to find the highest scoring word.
Each letter of a word scores points according to its position in the alphabet: a = 1, b = 2, c = 3 etc.
For example, the score of abad is 8 (1 + 2 + 1 + 4).
You need to return the highest scoring word as a string.
If two words score the same, return the word that appears earliest in the original string.
All letters will be lowercase and all inputs will be valid.
코드
function high(x){
x = x.split(' ');
let max = 0;
let maxIndex = 0;
for(let i = 0; i < x.length; i++){
let sum = 0;
for(let j = 0; j < x[i].length; j++){
sum += x[i].charCodeAt(j) - 96;
}
if(sum > max){
max = sum;
maxIndex = i;
}
}
return x[maxIndex];
}
다른 사람의 좋았던 코드
function high(s){
let as = s.split(' ').map(s=>[...s].reduce((a,b)=>a+b.charCodeAt(0)-96,0));
return s.split(' ')[as.indexOf(Math.max(...as))];
}
'알고리즘 > Codewars' 카테고리의 다른 글
[Codewars] Will you make it? (8 kyu) / JavaScript (0) | 2023.01.26 |
---|---|
[Codewars] Calculate BMI (8 kyu) / JavaScript (0) | 2023.01.25 |
[Codewars] Beginner - Reduce but Grow (8 kyu) / JavaScript (0) | 2023.01.23 |
[Codewars] Reversed Words (8 kyu) / JavaScript (0) | 2023.01.21 |
[Codewars] Reversed sequence (8 kyu) / JavaScript (0) | 2023.01.20 |
댓글