I am trying to learn the solution from this in regards to converting numbers input into Roman numerals.
(Referencing the second solution with 52 upvotes and uses both a for…in and while loop)
At the moment, I am struggling with applying for…in loops.
I have a problem to solve which is:
Given a string of words, you need to find the highest scoring word.
Each letter of a word scores points according to it’s 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.
My attempt so far, is below:
function high(x){
var total=0;
var letterPts= {a:1, b:2 , c:3 , d:4 , e:5 ,f:6, g:7, h:8, i:9, j:10,
k:11, l:12, m:13 , n:14 , o:15, p:16, q:17 , r:18 , s:19 , t:20 , u:21,
v:22, w:23 , x:24 , y:25 , z:26 }
var lowerCaseX = x.toLowerCase();
for (letter in letterPts) {
I am confused on what to do next as the syntax for the for…in loop looks complex to me.
The idea is, if the input x = ‘‘example’’
e is 5 therefore total = 0 + 5 = 5
x is 24 therefore total = 5+24 = 29
a is 1 therefore total = 29 + 1 = 30
m is 13 therefore total = 30 + 13 = 43
p is 16 therefore total = 43 +16 = 59;
l is 12 therefore total = 59+12 = 71
e is 4 therefore total = 70 + 5 = 75
So ‘‘example’’ scores 75 points.
How can for… in be used to reflect the above logic?