function calcAlphabet(str) {
let total = 0;
for (letter in str.toLowerCase()) {
total += str.charCodeAt(letter) - 96
}
return total;
}
calcAlphabet("example")
These look complicated but great! Thank you. I am currently trying to complete the following problem:
‘’ 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.’’
Now that I have (sort of) grasped the for loop being used to lookup some table, I need to manage an input into
function high(x)
where x consists of two different strings. The function should return the string which scores the higher amount of points which is determined by adding up each letter’s value.
How would I ‘‘upgrade’’ the original code so that it works with multiple string inputs? Hope this makes sense
I wouldn’t “upgrade” the other function, I would use it as a helper function to make high() work.
(and I guess with your problem parameters, the toLowerCase() in my solutions are unnecessary.)
essentially, what I’m talking about is something like this:
function high(str) {
// divide str into the first and second word
// figure out if calcAlphabet(firstWord) is bigger or calcAlphabet(secondWord)
// return the bigger scoring word, or if they tie, firstWord
}
function calcAlphabet(str) {
// your stuff you already figured out
}
Of course if the string is longer than two words, which it looks like it might be by the requirements, you’re gonna need a loop similar to the one you used in calcAlphabet()
Yeah I tried my best in making a standardised version that looks through n words but it’s easier to make a 2 words version first to check. Unfortunately, this has not sparked the intuition for making it work if the input is, say
“An example of text”
It only seems to work for
‘‘ExampleOne’’
‘‘ExampleTwo’’
It would compare the points of both and return the one with a higher score.
I want the function to be able to calculate multiple words in a string like
“I am an example” (this would score 114)
Unfortunately I am stuck so any guidance in the right direction would be appreciated.
@ilenia already pointed out some problems, but the first one is right at the top:
function high(x) {
var y = x[0]; //first string
var z = x[1]; //second string
yArr = y.split('')
zArr = z.split('')
The x that is being passed in is a string multiple words long, right? let’s say “Ieahleen NiSofwareEngineer”
so where you assign y the value of x[0] and x the value of x[1], what you are assigning to those variables are actually the 0th and 1st characters of the string, "Ieahleen NiSofwareEngineer": I and e
Something like this would make it work:
function high(x) {
const inputArr = x.split(' ');
const y = inputArr[0];
const yArr = y.split('');
const z = inputArr[1];
const zArr = z.split('');
BUT I would not recommend this. Have you heard of the concept of DRY?
That seems unnecessary complex… if in your string you have an a, then alph[“a”] is already equal to 1. You don’t need the second loop to compare if you use the letters of the string to get the value.