js takes a string of words and returns the highest scoring word.
function high(x){ // 1. split x by ' ' to an array. const wordList = x.split(' '); // 2. calculate each word score to another array. const getScore = (word) => { return word.split('').reduce((prevScore, currWord) => prevScore + currWord.charCodeAt(0) - 96, 0) } const scoreList = wordList.map(word => getScore(word)); // 3. get the highest score and index let highestIndex = 0; let highestScore = 0; scoreList.forEach((score, i) => { if (score > highestScore) { highestIndex = i; highestScore = score; } }); // 4. return the string of the highest score index of wordList return wordList[highestIndex];}
Also in JavaScript:
- fullcalendar angular add events
- format currency javascript
- tick.json code
- how to use a js class in react
- create react app not creating template
- rotation in p5 play
- jquery closest
- js detect mouse support
- express and node pakages
- javascript array find highest value of array of objects by key
- list pci express version command line
- add 2 for hours in date timestamp js
- find length of longest string in array javascript
- javascript set localstorage
- what does appendchild do in javascript
- how to get header in all the pages in visualforce page
- redirect using javascript
- deutsches ajax framework
- multiple line string in jquery
- vendor bundle base js and bootstrap min js conflict
- html select multiple selected values
- js get the filename you uploaded
- object to json c#
- javascript define global variable