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

Is this code too wierd for a beginner?

//questions and correct/wrong answers var question1 = prompt('What is the best programming language?'); var question2 = prompt('What is the worst programming language?'); var question3 = prompt('Can you make math in javascript?'); var question4 = prompt('Can you create websites in javascript?'); var question5 = prompt('Can you make games with javascript?'); var number1= 0; var number2= 0;

//testing is they asking correct or wrong if (question1.toUpperCase() === 'JAVASCRIPT') { number1 += 1 } else { number2 += 1 }

if (question2.toUpperCase() === 'PHP') { number1 += 1 } else { number2 += 1 }

if (question3.toUpperCase() === 'YES') { number1 += 1 } else { number2 += 1 }

if (question4.toUpperCase() === 'YES') { number1 += 1 } else { number2 += 1 }

if (question5.toUpperCase() === 'NO') { number1 += 1 } else { number2 += 1 }

//comparing the correct answers and reward the user

if (number1 === 5) { alert('You got the gold crown!'); } else if (number1 === 4 || number1 === 3) { alert('You gold the silver crown!') } else if (number1 === 2 || number1 === 1) { alert('You gold the bronze crown!'); } else { alert('Sorry, you didn`t get any crown!'); }

//print out the result document.write('<p>You have answered ' + number1 + ' questions right</p>') document.write('<p>You have answered ' + number2 + ' questions wrong</p>')

2 Answers

Hi Adam,

There really isn't a clear answer to your question.

The code works and is easy to read. If you in the early stages of TreeHouse courses keep going and you will be able to refactor (improve) this code to make it even easier to read and more robust.

So no, it's not too weird for a beginner but there is always room for improvement!

// questions and correct/wrong answers
var question1 = prompt('What is the best programming language?');
var question2 = prompt('What is the worst programming language?');
var question3 = prompt('Can you make math in javascript?');
var question4 = prompt('Can you create websites in javascript?');
var question5 = prompt('Can you make games with javascript?');
var number1 = 0;
var number2 = 0;

// testing is they asking correct or wrong 
if (question1.toUpperCase() === 'JAVASCRIPT') {
  number1 += 1
} else {
  number2 += 1
}

if (question2.toUpperCase() === 'PHP') {
  number1 += 1
} else {
  number2 += 1
}

if (question3.toUpperCase() === 'YES') {
  number1 += 1
} else {
  number2 += 1
}

if (question4.toUpperCase() === 'YES') {
  number1 += 1
} else {
  number2 += 1
}

if (question5.toUpperCase() === 'NO') {
  number1 += 1
} else {
  number2 += 1
}

// comparing the correct answers and reward the user
if (number1 === 5) {
  alert('You got the gold crown!');
} else if (number1 === 4 || number1 === 3) {
  alert('You gold the silver crown!')
} else if (number1 === 2 || number1 === 1) {
  alert('You gold the bronze crown!');
} else {
  alert('Sorry, you didn`t get any crown!');
}

// print out the result
document.write('<p>You have answered ' + number1 + ' questions right</p>');
document.write('<p>You have answered ' + number2 + ' questions wrong</p>');
Adam Beer
Adam Beer
11,314 Points

Ross has right! Your code is good and easy to read!

Thank you guys for motivation. Yes it works pretty much the same as Dave but is different so I was wondering that I've done it wrong or too much duplicates. But i get the same result, for sure i will continue to learn from here it's awesome btw