Outcome problem!


#1

In the code below there is something wrong that i cant figure yet out. It should actually print out only where the name is, but in fact it prints out the whole string declared. Can you guys please help me find out what is wrong?

var text = "hello how are you doing jack/ this is vienna/ we are looking for some fun overhere/ dont worry jack/ the time has come, so lets code/ life is beautiful/ sometimes it can even get better my dear jack;"
var myName = "jack";
var hits = [];

for(i=0;i if(text[i] === 'j'){
for(j=0;j hits.push(text[j]);
}
}

}

if(hits===0){
console.log("Your name wasn't found");
}
else{
console.log(hits);
}


#2

Does this code run or do you get an error message? I ran it on 2 compilers and got error messages in both console displays.


#3

yes it runs and shows vertically all the string divided letter by letter.

https://www.codecademy.com/en/courses/javascript-beginner-en-XEDZA/0/6?curriculum_id=506324b3a7dffd00020bf661

can you view the link?


#4

I don't understand how your posted code would run. You have a semicolon after a closing parentheses and before a curly brace at the end of your 2nd loop. Then 2 random closing curly braces appear before your if condition. How does it execute anything?


#5


#6

Why is the myName variable from this image assigned "admir"? In your original post, it's "jack". Also, at the top left of the image is a different avatar than yours. This seems odd.


#7

changed my email in codecademy which changed my avatar also. it used to b my original code, i made some modifications so that it works. i am the same guy who tries to learn coding in js :slight_smile:

do you know what the problem in my code is?


#8

I don't understand how this came out of the code posted in the image.


#9

yeah as you can see it is identical, just instead of a name admir its jack or vice versa. i dont think thats the issue making the program print all that as a string, but in fact i cant understand why it prints it all and plus separated letter by letter


#10

The problem is the initializer in your 2nd loop: j=0.

0 corresponds to the first character in your text string. The 2nd loop is pushing everything in the text string into the hits array. You have to change what you assign to j. What character could you assign to j to make the loop collect the myName variable into the hits array?


#11

as i see i should initialize it with i; so j=i.

do you mean that?


#12

Run it and see what happens :slight_smile:


#13

haha another string, shorter but has to do more with a's:
[ 'a',
'r',
'e',
' ',
'y',
'a',
'd',
'm',
'i',
'r',
'a',
'/',
' ',
'w',
'e',
'a',
'r',
'e',
' ',
'l',
'a',
'd',
'm',
'i',
'r',
'a',
's',
' ',
'c',
'o',
'a',
'u',
't',
'i',
'f',
'a',
'n',
' ',
'e',
'v',
'a',
'r',
' ',
'a',
'd',
'a',
'd',
'm',
'i',
'r' ]


#14

Exactly! Your 2nd loop executes when it comes across the 1st letter of the myName variable. Then it collects the following 4 text characters into the hits array and scans the code for the next 'a'. Try capitalizing the 'a' in the myName variable and the text string and run the code.


#15

Perfect! thanks a lot, now it prints out only the name. I have one more last question for today!! sorry to ask too much.

Next case is when i delete all capital A from the test String, which means it should print: Your name wasn't found.

But in fact all i get is [], does it has to do with the declaration of my hits variable?


#16

Check the hint in the exercise for what your if condition should look like.


#17

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.