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

Craig Charlton
Craig Charlton
8,124 Points

It works but I don't see the link to the function.

It works and I get it (at the NaNteenth attempt)... EXCEPT!!!

Where or how does the link to the function "print" occur??

I think it correct (but feel free to correct me) to say I can see the links to all the variables created and their use in the program. I think it also fair to say that no matter how far above the "for loop" those var were in a more extensive program they would still apply and therefore be valid. Assuming they were some way above and their was more programming between the Var's and the guts of the program (for loop) the interpreter would jump up to the var's and back to the loop as many times as .length permitted ( I got some points for learning that :p ).

None of the var's nor the for loop are connected inside or to the function as far as I can tell.... so why does it not get jumped over....

(before you reply, I would ask that if you are going to make me look silly then please do it nicely.... I am quite chuffed to get this far so far.... )

var questions = [
  {
    question: 'How many states are in the United States?', 
    answer: 50
  },
  {
    question: 'How many continents are there?',
    answer: 7
  },
  {
    question: 'How many legs does an insect have?', 
    answer: 6
  }
];
var correctAnswers = 0;
var question;
var answer;
var response;

function print(message) {
  alert(message);
}

for (var i = 0; i < questions.length; i += 1) {
  question = questions[i].question;
  answer = questions[i].answer;
  response = prompt(question);
  response = parseInt(response);
  if (response === answer) {
    correctAnswers += 1;
  } 
}
alert("you scored " + correctAnswers);
Craig Charlton
Craig Charlton
8,124 Points

oh... that programming didn't paste too good!!!

Are you talking about calling the print function at the end of the program, and why does the for loop not access the print function because the objects are above the function in the code it's self? I'm asking so we can break your question down into parts so we can easily answer each question you have.

2 Answers

Craig Charlton
Craig Charlton
8,124 Points

see... I knew I would look daft.... but you gave me enuff (logic I couldn't see)... I adapted this from a lesson and the lesson printed it out to the document.... having done and passed the course I went back to try to understand it.....

I worked in the console so document.write was no use to me (what the function originally was part of)... I should have wiped the whole function..... I got there when you pointed the obvious.... thanks for your time.... ( I removed the whole thing.... could have been worse, I could have been trying to understand the link to the function for another week)...

Trying to understand the question: you aren't calling the print() function that you've defined anywhere in your program, so it never runs. You are using 'alert' by itself. If you want to use your function instead, you could do:

var message = "you scored " + correctAnswers
print(message);

The browser sees the function declaration and stores it for later use, so there is not an error or anything, but you aren't calling it, so it is unused.

It only interprets that function declaration once; actually, it interprets that before anything else in the program, as I understand it. So the 'skipping around' that you're imagining is not quite the right way of thinking about the program.