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 trialJoseph Rimar
17,101 PointsSolution to quiz using a function?
Hey everyone. I used the same type of function Dave showed us in the previous videos to come up with a solution that seems to work just as well. I'm wondering what benefit there is to using Dave's suggested method instead of something like this if functions are normally the backbone of JavaScript?
var quizQuestions = [
['How many days are there in a week?', '7'],
['What is the capital of England?', 'london'],
['What is the highest mountain on Earth?', 'everest'],
['What is the capital of Japan?', 'tokyo']
]
function print(message) {
document.write(message);
}
function answeredQuestions (questions) {
var numberRight = 0;
var correctListHTML = '<h2>You got these questions right:</h2>' + '<ol>';
var incorrectListHTML = '<h2>You answered these questions wrong:</h2>' + '<ol>';
for (var i = 0; i < questions.length; i++) {
var answers = prompt(questions[i][0]);
if (answers === questions[i][1]) {
correctListHTML += '<li>' + questions[i][0] + '</li>';
numberRight ++;
} else {
incorrectListHTML += '<li>' + questions[i][0] + '</li>' ;
}
}
correctListHTML += '</ol>';
incorrectListHTML += '</ol>';
print('You got ' + numberRight + ' question(s) right.');
print(correctListHTML);
print(incorrectListHTML);
}
answeredQuestions(quizQuestions);
1 Answer
Marcus Parsons
15,719 PointsHey Joseph,
I believe that answers lies within what your ultimate goal of the code would be. You create functions for pieces of code that you want to run multiple times. Since this challenge was a stand alone program, there's really no need to create any functions for this program. And it is faster to use straight through code, instead of calling custom functions, as you can see in this JavaScript performance test: http://jsperf.com/custom-functions-vs-no-functions
Joseph Rimar
17,101 PointsJoseph Rimar
17,101 PointsThanks Marcus, I thought that using as few variables as possible would actually be more efficient and I did not realize that using functions was actually slower.
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsIf you think about it, it's having to make more calls than it would if the code were straight through i.e. using print(message) it has to find the function print, pass in the values given, then find document.write() and pass in the values given, and execute it whereas if you just used document.write(), it would only need to find document.write() and then pass in the info and execute it.
I made a test case for your code versus a straight through version of your code, but evidently, document.write() messes with the performance operation. I'll make another one that uses alert() instead of document.write().
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsHere is the performance test where your code was put into the top portion. The only part that was changed was instead of using document.write(), I used alert(). The bottom part uses the same variables, same questions, just in a straight through format. http://jsperf.com/custom-functions-vs-no-functions/2