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 Build a Quiz Challenge, Part 2 Solution

Grzegorz Zielinski
Grzegorz Zielinski
5,838 Points

Opinion about my JS

I wonder what you think about my solution for this challenge :)

let questionAndAnswers = [
  ['What is the capital city in Poland?','warsaw'],
  ['The longes river in Poland?','wisla'],
  ['How many milions people live in Poland?','35']
];

function print(message) {
 var outputDiv = document.getElementById('output');
 outputDiv.innerHTML = message;
}

function questions(question) {
  let answer = prompt(questionAndAnswers[question][0]);
  return answer.toLowerCase();  
}

function createAnswer(type) {
    if ( type.length > 0 ) {
    let print =`<ol><li>`;
    print += type.join('</li><li>');
    print += `</li></ol>`;
    return print;  
 } else {   
    return `None`;
 }
}

let html = '';
let counter = 0;
let goodAnswers = [];
let badAnswers = [];

for ( var i = 0; i < questionAndAnswers.length; i += 1) {

  let answer = questions(i);
  let position = questionAndAnswers[i].indexOf(answer); 

  if ( position === 1 ) {
    goodAnswers.push(questionAndAnswers[i][0]);
  } else if ( position === -1) {
    badAnswers.push(questionAndAnswers[i][0]);
  }

  if ( position === 1 ) {
    counter += 1;
  }   
}

html = `You got ${counter} question(s) right. 
<h2>You got these questions correct:</h2>${createAnswer(goodAnswers)} 
<h2>You got these questions wrong:</h2>${createAnswer(badAnswers)}`;
print(html);

Any advise about what I could change or improve? Thanks!

2 Answers

Steven Parker
Steven Parker
231,275 Points

Looks very nice, good job! :+1:

Some minor suggestions:

  • be more consistent about indentation amounts and using it to show nesting level
  • use ordinary strings for efficiency when there is no interpolation to be done
  • you can skip using an "else" when the "if" condition returns

Example:

function createAnswer(type) {
  if (type.length > 0) {
    return `<ol><li>${type.join("</li><li>")}</li></ol>`;
  }
  return "None";
}

Clever use of "join", by the way.

Grzegorz Zielinski
Grzegorz Zielinski
5,838 Points

Thank you for advice, I'll keep them in mind!

use ordinary strings for efficiency when there is no interpolation to be done

Do you mean using " " + " " rather than ${} ? I didn't know that using affects efficiency.

Clever use of "join", by the way.

Thanks!

Steven Parker
Steven Parker
231,275 Points

I mean use " or ' instead of ` when you don't have any ${} to be replaced in the string.