JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1 Solution

Joanna Suau
Joanna Suau
1,306 Points

how to incorporate toUpperCase() method

Hi, I've been following the video and my code is quite the same Dave's has posted in his solution, however, my answers are strings. The counter works only if i pass the exact same string which I wanted to cater for using the toUpperCase() method. I'm not quite sure where sure I put it for the code to work as expected. Here's my code:

const questions = [
['Capital of England', 'London'],
['Capital of Spain', 'Madrid'],
['Capital of Poland', 'Warsaw']
]

let question;
let answer;
let html;
let correctAnswers = 0;

function print(message) {
  document.write(message);
}


for (let i = 0; i < questions.length; i += 1) {
  question = questions[i][0];
  answer = questions[i][1];
  response = prompt(question);
 if (response === answer) {
   correctAnswers += 1;
 }
}

html = `You've got ${correctAnswers} question(s) right.`
print(html);

3 Answers

Harris Keka
PRO
Harris Keka
Pro Student 1,636 Points

Hi Joanna, if you want to use the .toUpperCase() make sure your answers in your inner array are in uppercase. For example const questions = [ ['Capital of England', 'LONDON'], ['Capital of Spain', 'MADRID'], ['Capital of Poland', 'WARSAW'] ] Meaning if a user enter's an answer london or London, the response will be converted to LONDON and compared to your value in questions[0][1].

But if you decide to use .toLowerCase(), ensure your questions[0][1] = london.

I hope my answer helps. Otherwise try to make your question clear enough if I do not understand.

Thanks.

Joanna Suau
Joanna Suau
1,306 Points

That's the exact problem I was having thanks a lot for your help. I did it a little bit different to what you're suggesting but it originally started with my changing the array into all caps. See my comment below for the code details.

Joanna Suau
Joanna Suau
1,306 Points

After playing with the code a little bit more I have finally managed to make it work. I needed both variables, response and answer to transform into upper cases so they match, previously i would do it to only response so it would still not match the string in the array that was left unmodified.

const questions = [ 
  ['Capital of England', 'London'], 
  ['Capital of Spain', 'Madrid'],
  ['Capital of Poland', 'Warsaw'] 
]

let question; 
let answer; 
let html; 
let correctAnswers = 0;

function print(message) {
  document.write(message);
}


for (let i = 0; i < questions.length; i += 1) { 
  question = questions[i][0]; 
  answer = questions[i][1].toUpperCase(); 
  response = prompt(question).toUpperCase(); 
  if (response === answer) { 
    correctAnswers += 1; 
  } 
console.log(response);
console.log(answer);
}

html = `You've got ${correctAnswers} question(s) right.`;

print(html);
Zimri Leijen
Zimri Leijen
8,957 Points
if (response === answer[0].toUpperCase() + answer.slice(1).toLowerCase())
Joanna Suau
Joanna Suau
1,306 Points

I'm afraid it did not work for me. Can you explain the logic behind this code snippet?

Zimri Leijen
Zimri Leijen
8,957 Points

I can't see the original question, but the idea was that since your answers started with one uppercase character, and the rest was lowercase, this would convert the answer to the same format.

I may have confused the answer with the response.

The response is the input from the user right? So response and answer would need to be swapped in that case.

answer[0] would select the first character of the answer (I intended it to be the reply), and answer.slice(1) is everything from the second character onwards.