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 trialTomas Rinblad
Full Stack JavaScript Techdegree Student 12,427 PointsIs this code easier to understand?
Hi.
I have done this challange and i think that i have come up with a fine code. But i can be byas. So i was thinking of asking you:
Is this code easier to understand or not?
var question = [ ["Where do i live", "karlskrona"], ["Did i grow up here", "yes"], ["True or false, do i own a cat", "true"] ];
function printAnswer ( quest ) { var listHTML = "<ul>"; var wrongList = "<ul>"; var right = 0; for (var i=0; i<quest.length; i+=1) { var answer = prompt(quest[i][0]); answer = answer.toLowerCase(); if (answer === quest[i][1]) { listHTML += "<li>" + quest[i][0] + "</li>"; right += 1; }else { wrongList += "<li>" + quest[i][0] + "</li>"; } } listHTML += "</ul>"; wrongList += "</ul>"; document.write("<p> You got " + right + " correct answers"); document.write("<p> You got the following correct answers: </p>" ); print(listHTML); if (right < 3) { document.write("<p> You got the following wrong answers: </p>" ); print(wrongList); } } function print(message) { document.write(message); }
printAnswer(question);
Personnaly i think this code works better because you can easily create a new variable with different questions and answers and put it in the printAnswer(); This makes it better for future uses, i think. What do you think?
And still, how do i add my code directly to this question without it getting smoshed together?
1 Answer
Helmut Granda
10,343 PointsHi Tomas Rinblad,
I am not sure if the code is easier to understand but I can give you an initial feedback based on what you have posted.
var question = [ ["Where do i live", "karlskrona"], ["Did i grow up here", "yes"], ["True or false, do i own a cat", "true"] ];
// question talks about a singular value rather than a array or group of values
// each child element does not describe the data
function printAnswer ( quest ) { // argument as quest?
var listHTML = "<ul>";
var wrongList = "<ul>"; // Does this means the first list should be 'rightListHTM'?
var right = 0; // Why is right = 0? maybe numberOfRightElements...?
for (var i=0; i<quest.length; i+=1) { // i++ is the most common use to move into the next position
var answer = prompt(quest[i][0]);
answer = answer.toLowerCase(); //
if (answer === quest[i][1]) {
listHTML += "<li>" + quest[i][0] + "</li>";
right += 1;
}else {
wrongList += "<li>" + quest[i][0] + "</li>";
}
}
listHTML += "</ul>";
wrongList += "</ul>";
document.write("<p> You got " + right + " correct answers");
document.write("<p> You got the following correct answers: </p>" );
print(listHTML);
if (right < 3) {
document.write("<p> You got the following wrong answers: </p>" );
print(wrongList); }
}
function print(message) {
document.write(message);
}
printAnswer(question);
At any rate, after walking line by line through the code sampled here I think there can be many improvements to it. You have to remember that the teachers of these classes take their time to write/review/code what they are going to teach and although it may not be perfect for everyone they do a fine job.
With that said, at the end of the day if you are able to learn the principles on programming deciding if a piece of code is easier or not takes time away from learning more great stuff.
Thomas Nilsen
14,957 PointsThomas Nilsen
14,957 PointsDid you see my answer to your previous post?
To format your post - click on Markdown Cheatsheet to see how