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 trialALEXANDER MARININ
Full Stack JavaScript Techdegree Student 5,276 PointsHey there!For me, my code looks too different from Guil's. Is it OK or not,need you opinion about it.
//I ve made it to look shorter, it is not hard and easy readable.I know i forgot about upperCase check:(
const answer1 = "1";
const answer2 = "2";
const answer3 = "3";
const answer4 = "4";
const answer5 = "5";
let q1 = prompt('0+1');
let q2 = prompt('3-2');
let q3 = prompt('5-2');
let q4 = prompt('80-76');
let q5 = prompt('10/2');
if (q1 == 1) {q1 = 1} else {q1=0}
if (q2 == 2) {q2 = 1} else {q2=0}
if (q3 == 3) {q3 = 1} else {q3=0}
if (q4 == 4) {q4 = 1} else {q4=0}
if (q5 == 5) {q5 = 1} else {q5=0}
let score = q1 + q2 + q3 + q4 + q5;
let crown = "Crown earned:";
if (score == 5) {crown = `${crown} Gold`}
if (score == 4 || score == 3) {crown = `${crown} Silver`}
if (score == 1 || score == 2) {crown = `${crown} Bronze`}
if (score == 0 ) {crown = `${crown} 0. Better luck next time!`}
let PlayerRank = `You got ${score} out of 5`;
document.querySelector('main').innerHTML =` <h1>${PlayerRank}</h1> <p>${crown}<p>`
2 Answers
Piotr Baran
6,022 PointsWriting conditional statement this way, you use 1 variable to 2 different application.
This makes the code work, but if someone else would like to add new features later, they can no longer use the responses from the user. At the beginning the variable q1 is the content of the answer, then the point value. Better to use the 2nd variable to sum the points.
if (q1 == 1) {q1 = 1} else {q1=0} if (q2 == 2) {q2 = 1} else {q2=0} if (q3 == 3) {q3 = 1} else {q3=0} if (q4 == 4) {q4 = 1} else {q4=0} if (q5 == 5) {q5 = 1} else {q5=0}
Sufiyaan Haroon
Full Stack JavaScript Techdegree Student 9,558 PointsWriting your code like that will work and might look like simpler right now but another developer will have to take more time than needed to understand the code. Even for you it will be hard to understand your own code. Other than that, it should definitely work 😁