Assignment 2 Wrong score after fixing Transform function

I am struggling. I fixed my transform function to output the expected output. Previously the sample words scored just fine and got the expected scores, but my transform function was not outputting correctly. Now when I run the word javascript through the program it gives me the correct output for score options 0 and 1 but the last option gives me a 0 pts rather than 24 pts. Any suggestions?

I got it fixed! I had to remove the second for loop and then it ran just fine!

Hi @christinichka! My scrabbleScore() function is similar to yours, but we seem to have things in different order. My code (see below) is outputting 0. Wondering if you have any idea why mine isn’t working? Wondering why you update score twice (your line 71 and 74)?

let scrabbleScore = function(word) {
word = word.toLowerCase();
let score = 0;
for (let i = 0; i < word.length; i++) {
for (key in newPointStructure) {
if (key ===word[i]) {
score += newPointStructure[key];
}
}
}
return score;
}

Sorry, above doesn’t show the formatting well - here’s a screen shot
Screen Shot 2021-03-02 at 8.28.30 PM

Hey emgraham,

If your function is returning a score of 0, then it’s likely that your if (key === word[i]) isn’t triggering, and thus not updating the value of score. Since the characters of ‘word’, e.g word[i], are all strings, you could check the data type of your newPointStructure keys. A mismatch there might be causing your problem.

Hope that helps!

Thanks @jwrounds, really appreciate the help.

My transform function currently makes the newPointStructure keys Numbers. See screenshot. Do you mean to say that these keys should be strings instead, so that they match up with word[i] type when comparing?

I did try to take out the Number() conversion, but I’m still getting 0.

Screen Shot 2021-03-02 at 10.37.32 PM

Hi @emgraham,

I did mean that the keys should be strings, but I must’ve had a brain fart, because the newPointStructure object’s keys are the letters themselves, and thus would be strings, so disregard that.

Looking at your transform function, I think the problem may be with how you initialize transformedObject in line 2. Have you double checked the output of your transform function to make sure it transforms the point structure as you expect it to? That could be why no letter corresponds to any key in your scrabbleScore function above. You could also check what key you are checking each letter against in your scrabbleScore function with some console.log statements.

Good eye. For scrabbleScore I ended up having to change my .toUpperCase to .toLowerCase which then output a score but it was double what was expected. Next I took out lines 72-74 because that was what was doubling my score! Then viola it worked.

I don’t think you need the second for loop or the if statement. Just score += newPointStructure[word][i] after the first for loop. Then return at the end.

For the transform function I have a few things different. transformedObject should be = {} then newLetterKey = newLetterKey.toLowerCase(); then I used a template literal around my newletterkey on the next line down within the []

thanks guys!! that did it!

1 Like

@christinichka @jwrounds Thank you guys so much for asking these question. I have had a tough time understanding the logic behind these issues. I have managed to erase 4 of the 6 failures that I had. I do have two new ones that I am not sure how to solve. I am not sure if my coding is correct to fit the math part of the functions. I will admit my math for vowelBonus score just looks wrong in my opinion but I could not get it to work any other way. It still displays the correct number though.

I am also having the same issue in simpleScore but I figured if I can figure this out, I would be able to understand what is going wrong and fix the issue. Any advice would help!

I have not been able to find a way for the program to delete multiple vowels of the same kind. If I type in a vowel two times, the program decides not to delete it. The only way I could work around this was adding a second splice but it still won’t do it if I type it in three times.


This is the result I get when I only use one statement. I believe the issue lies in the splice portion of the code but I honestly have no clue what kind of logic is needed to delete multiple characters of the same kind.

Hi @miclamb241,

Looking over your code for vowelBonusScore, I think you should reconsider just the bare essentials of the problem. It helps me to think first about the input and output. We have a word, like ‘zach’ and need to output a score in the form of a number, 1 point for each consonant and 3 for each vowel. We get a string and then return a number based on the characters in that string.

I think you’re getting lost by over-complicating the problem. For example, you split the string into an array of characters and then remove vowels from that array. You then use several similarly named variables num, num1, etc. to keep track of vowels and consonants by iterating over the word and array of consonants, and it’s hard to follow.

A simpler approach might be to use one variable to track the score, and then increment that variable whenever you encounter a consonant or vowel. You have an array of vowels to check each letter against, and that’s perfect! I don’t think you need to delete any vowels, or split up the word. You can instead iterate through the word with a for statement (remember: strings work a lot like arrays with for loops), and use a conditional statement to increment score appropriately. Then, once you’ve gone through the given word and checked each letter against your set of vowels, you can just return your score variable.

@jwrounds Is there a way to keep track of vowels going through the loop? I have not had much experience using one variable for two forms of data.

@jwrounds


I also have no clue what it means when it says expected 3 to be 0. The function gives me the exact result I need.

Hi @miclamb241,

The spec expects the score of a word to equal the length of the word for the simpleScore function, and it looks like the function is returning 3 when passed an empty string.

Your simpleScore function is returns the length of a word, albeit via i and num and a for loop, which won’t run if the length of the string is 0. You already know the length of the string, since you use it for the for loop, and should try just returning that.

As for keeping track of vowels, I’m not really sure what you mean about using one variable for two forms of data. You have an array of vowels. I recommend just using that, and checking each letter of a given word against that array, see if the array includes each letter. If a letter is a vowel, add 3 to the score, and if it’s not a vowel, add one to the score.