I am doing the second exercise in the loops, arrays, iterators intermediate practice but am stuck here.
Can anyone tell me why the console is logging the index of both characters as 1. I want it to log the character a every time it appears in the word. So for example, if we ran the function with the word ‘Saturday’ and ‘a’ as below, it should log an array [1,6]. Instead it is logging [1, 1].
const subLength = (word, letter) => {
let wordArray = word.split("");
let indexArray = []
for (i=0; i<wordArray.length; i++) {
if (wordArray[i]===letter) {
indexArray.push(wordArray.indexOf(letter));
}
} console.log(indexArray);
}
subLength('Saturday','a');
.indexOf() simply finds the first instance of a letter in an array and returns the index of that. Therefore the reason it becomes [1, 1] is because it is returning the index of the first instance of the letter, which is at index 1. This actually doesn’t need to be as complicated as using indexOf(), as you already have an element used in the loop, which is effectively tracking the “current” index. If you use this instead then your code will work.
Thanks Adam, I switched it so the push part is indexArray.push([i]) and it is working. So now my code is like this and it all works just fine but when I check my answer codecademy is telling me I have a syntax error. Can you spot it?
const subLength = (word, letter) => {
let wordArray = word.split("");
let indexArray = []
for (i=0; i<wordArray.length; i++) {
if (wordArray[i]===letter) {
indexArray.push([i]);
}
} console.log(indexArray);
if (indexArray.length === 2) {
return indexArray[1] - indexArray[0] +1;
} else {
return 0;
}
}
console.log(subLength('planetary','a'));
My best guess would be doing push([i]) not being quite accurate, as you’re making nested arrays unnecessarily. It still works as they are mini arrays of length 1, but not best practise. Try just push(i), and if that doesn’t work, I’m not sure what else it could be. Code runs fine on VS Code so I presume it’s a soft error i.e. and error flagged by codecademy’s checks to make sure you’re doing it correctly, rather than an actual coding error.
Hi, I’m developing a bowling scoring game and I’m taking a string of bowling scores which are either a number or an X or / and totalling the score. I’m going to work out the more complex logic later but for now I want to take the string and if there is an x in the score I want to add 10 to the total score. The output I’m getting is NaN. Please can someone suggest where I’m going wrong? thank you
const string = '394829502X';
let myString = string.split('');
//console.log (myString);
let totalScore = 0;
for (i = 0; i < myString.length; i++) {
totalScore = totalScore + Number(myString[i]); //had to use 'Number' to get it to convert, is this an ok method?
}
console.log(totalScore);
function SplitOrSpare (totalScore) {
let strike = 0;
let spare = 0;
if (totalScore === 'X') {
totalScore = (totalScore +strike);
} else {
totalScore = (totalScore);
}
SplitOrSpare ();
console.log (totalScore);```
Thank you so much for your help, that makes a lot more sense how I was using ‘Number’. I’d used that as I couldn’t get the loop to sum without it. If I take my ‘X’ out for now I can’t seem to total the number, the output for this is 019. Is there something really obvious I’m missing with how to sum in the loop please?
const string = '19';
let myString = string.split('');
console.log (myString);
let totalScore = 0;
for (i = 0; i < myString.length; i++) {
totalScore = totalScore + (myString[i]);
}
console.log(totalScore);
You were on the right track before. Converting each character to a number and adding to the total score is the right idea. The problem in your original attempt was that when you tried to convert a non-numeric character such as 'X' or '/', the result of the conversion was not a number and it ended up messing up the totalScore.
One way to remedy that would be to check whether the conversion Number(myString[i]) would result in a number or a NaN. To accomplish that, you can use the isNaN function (see documentation: isNaN() - JavaScript | MDN)
const string = '394829502X';
let myString = string.split('');
let totalScore = 0;
for (i = 0; i < myString.length; i++) {
if (isNaN(myString[i])) {
continue;
} else {
totalScore = totalScore + Number(myString[i]);
}
}
console.log(totalScore);
// 42
If converting the character you are iterating over doesn’t result in a number, then you just continue to the next character. If the conversion to number is valid, then you add the number to totalScore.
If you want to add 10 points for an 'X' or do something else for other characters such as '/', then instead of continue, you can write an if statement (and else if for other situations) e.g.
const string = '394829502X';
let myString = string.split('');
let totalScore = 0;
for (i = 0; i < myString.length; i++) {
if (isNaN(myString[i])) {
if (myString[i] == 'X') {
totalScore += 10;
} // else if (myString[i] == '/') ...
} else {
totalScore = totalScore + Number(myString[i]);
}
}
console.log(totalScore);
// 52