Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Sven Huff
Courses Plus Student 4,355 Points[Solved] JavaScript Wordchanger
Hi there,
Challenge I want to display a static word on the screen, which is completed by words, which are changing. For example: (static) "My name is…" (dynamic)"Joe" than next word "Jack" etc.
I became a hint, to use the setInterval() method. And I think, it leads me on a promising path. But unfortunately without reaching the intended aim.
Observed behaviour By running my code, the static "My name is" ist not static. It is going to be substituted by the names – all of them – which are repeating themselves. Like this
"Joe Jim Sag ich nicht Joe Jim Sag ich nicht Joe Jim Sag ich nicht Joe Jim Sag ich nicht Joe Jim Sag ich nicht Joe Jim Sag ich nicht Joe Jim Sag ich nicht Joe Jim Sag ic"
My code
__ <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>JS Word Changer</title>
<script> var displayWord; var resetWord; var wordPool = ["Joe ", "Jim ", "Sag ich nicht "];
/This function shows a Word for 500 milli-secs. It calls another function, which delivers the words/
function changeWord () { setInterval(getWord, 700); }
/This function should show the words from the array "wordPool". It iterates through the words and replaces every single word with a blank, and then shows the next word from the array./
function getWord () { for(var i = 0; i<wordPool.length; i++) { var manyWords = document.write(wordPool[i]); } } </script> </head> <h1>Hello, my name is</h1>
<body onload="changeWord();">
<div id="my_box">
</div> </body> </html> __
Sorry for not using the code highlighting but when using these ''' it justs shows the "Hello, my name is" part accuratly as code.
Issues
- Could you please tell my, why even the "Hello, my name is" part is changing? (I put it above the <body> section, thinking in this way it is out of reach of the onload command.
- Could you give me further hint, of how to reset the just shown name - like Joe – to substitute it to the next one – and so on.
It would be great, if you could just give me a few hints. Not the full solution.
Many thanks for your effort,
Sven
2 Answers
Marcus Parsons
15,719 PointsHey Sven,
If it's okay with you, I'd like to both give you hints and give you the full solution. You can choose to look at it or not. It can help with deciding how your code should go.
It looks like you want each name to come up in that specified interval from an array. What I did was put a span element inside of my h1 element that is specifically for each name and gave it an id. Then, target that span element from inside JavaScript to change the name.
<h1>Hello, my name is <span id="names"></span></h1>
setInterval is the function I used, as well, because it continuously goes until you stop it. You can choose to include some kind of interactive piece to stop the names from being cycled through such as a Stop button or something of that sort.
Here is the version I did:
Sven Huff
Courses Plus Student 4,355 PointsHi Marcus, thank you very much. Your using of the [ìf-statement] was very helpful for me. My, Joes and Jims brain was stucked in the [for-Loop]. I think, this was part of the problem. Also helpful for me was the refresher of the [span-attribut].
"querySelector" and "innerText" are commands I did not know yet. Your answer and your code example were very instructive for me. Thank you again!
Have a nice weekend,
Sven
Marcus Parsons
15,719 Points"querySelector" is an awesome, powerful command! You'll grow to love it :) You're very welcome, Sven! You have a nice weekend yourself :P