102 - Highest Scoring Word

Q.

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.

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.

A)

function high(x){
  // 'a' 의 charCode는 97 z는 122
  let toArr = x.split(' ')
  let resArr = []
  let sum = 0;
  let toArr2 = toArr.map(el => el.split(''))
  for (let arr of toArr2) {
    for (let i = 0; i < arr.length; i++) {
      sum += arr[i].charCodeAt() - 96
    }
    resArr.push(sum)
    sum = 0;
  }
  return toArr[resArr.indexOf(Math.max(...resArr))]
}  

좋은 웹페이지 즐겨찾기