Hey there, I need help invoking the function.
I’ve stored the value for the array index in namesIndex variable, and I’ve tried returning the names with return names[namesIndex]; but I’ve got no results.
I’ve seen that on MDN Github the solution was storing the variable to
para.textContent but I don’t get it, can someone explain how does para.textContent work?
Is there an alternative way to solve this by using return as stated in my example?
With textContent we can add text inside a HTML tag. On line 30 of your code you see that we are creating a <p> element with the name para. By using para.textContent = ...; we write the name found in the array into the <p>. Finally the <p> will be added to the existing section of the HTML. In the end the HTML will look something like this:
<section class="preview">
<p>Tina</p> /* or whatever name was calculated by the function */
</section>
The whole idea of the textContent thing is that we can see the result of our JS code in HTML. When using return you need to also assign the returned value to a variable. You could do this: const name = chooseName(); to save the name from the function into the variable name. But then you still don’t know if your function works correctly because you can’t see the value in the variable. Beside textContent you could also use console.log(name); to write the name to the console of the DevTools.
This textContent method will be used in several other task to display the result of your calculations.
I hope my explanation didn’t confuse you more. Feel free to tell me if something isn’t clear, yet.
Thank you so much for your explanation, it is very clear and it makes much more sense now. I’ve tried the solutions on an editor and it works now. Have a good day!