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

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Useful Array Methods

Blake Powell
Blake Powell
4,175 Points

why the while loop?

just to clarify, the while loop is strictly there to cause the search box prompt to keep appearing correct? ordinarily we would see a search box fixed to a page, with a conditional function linked to the user's input in a way that we haven't covered yet?

the reason i ask is that indexOf() can be seen as a conditional loop in itself can't it? it takes an argument and compares each item in the array and returning a number when it finds one thats equal?

it might get misconstrued that the "while" loop is somehow needed for the indexof function to happen, when its really only there to activate the prompt and include the list condition which could've been added in a million other ways.

that was at least my first impression of what was going on when it was wrote. Only after i paused the video and examined each item individually that it dawned on me that if idexof() statement could work independently should the users inquiry be taken up another way. provide the correct syntax was used of course.

also i noticed i wasn't the only one curious as to why a second if statement rather than just another else if. its not that we can't figure out that both can work, but when you do something like that that seems almost counter intuitive, at least to someone who knows zilch about programming, it'd be great if we could get a bit of the reasoning behind it. I'm still on the edge of my seat waiting to see why we would store a function inside a variable and that was what basics 1? you mentioned updating videos in the teachers notes, just wanted to give a little feedback.

3 Answers

Steven Parker
Steven Parker
229,732 Points

It sounds like you've answered your own question. :+1:

Your description of the loop process seems essentially correct. And I agree that once you are fluent with dynamic page elements you'll not be likely to use a modal like prompt for input.

As for storing functions in variables, that's a feature of the language and in practice there's very little difference between a named function declaration and a variable assigned with a function.

ian izaguirre
ian izaguirre
3,220 Points

"As for storing functions in variables, that's a feature of the language and in practice there's very little difference between a named function declaration and a variable assigned with a function."

Hi, if you are referring too a function declaration vs a function expression vs an Immediately invoked function expression (IIFE), then saying there is little difference would be incorrect. If that is what you were referring too though, I might be misreading that comment?

Steven Parker
Steven Parker
229,732 Points

No, IIFE's weren't mentioned here. But there really isn't much difference between a function declaration and a function expression that is assigned to a variable. Other than only the first is "hoisted", can you think of anything else?

Blake Powell — did your question get resolved? Remember to choose a "best answer" to mark the question compete.

And happy coding!

ian izaguirre
ian izaguirre
3,220 Points

You mean "Hoisted"? Yea but isn't the fact that a function expression not being processed until the interpreter gets to that statement considered a big difference since saving memory is important in JS ? or maybe Im wrong since I am still not that far in learning about memory just yet. Question: if the interpreter looks for variables and functions before going through the script, would that not mean that no memory is saved in a function expression since it is still assigned a variable ? I know I contradicted myself, now I am not sure if it saves memory lol.

Steven Parker
Steven Parker
229,732 Points

I have not benchmarked it, but I would not expect to see a significant difference in memory using between a function declaration and a function expression assigned to a variable.

Blake Powell
Blake Powell
4,175 Points

oh i see what i was doing, i left out the parenthesis after the variable name so i was seeing the entire function. that statement makes more sense now. that said, it might be worth noting that

var varName = function ()
{return}

will run differently from

function functionName()
   {return}

var varName = functionName()

in that if your trying to return something like a random number function the first variable will always change while the second will stay the same. my first time thru i think i had gotten the impression that if you stored the function in a variable you were just storing the result there.

Steven Parker
Steven Parker
229,732 Points

In the first case, when you wanted to call the function, you would do this:

var newVar = varName();  // since varName stores the function itself
Blake Powell
Blake Powell
4,175 Points

yea the parenthesis after varName were what i was talking about forgetting. forget the second function i typed for a second, what i was trying to say in the last comment was that just typing the varName() by itself wouldn't create a stand alone variable with the result of the function stored in it, you have to store the result in another variable as you just did.
i might be alone in this, but i didn't pick it up the first time time through the functions page and it kind of screwed me up not only with the more advanced stuff, but i think it sort of shifted the concept of a variable that i gotten from that section as well. or maybe it was the other way around, i didn't pick up everything from the variables i needed then got to functions and it only got worse.

either way it wasn't until i watched the variable and function sections back to back while "what if-ing" a couple of things in the text editor before i came to those conclusions.