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 Basics (Retired) Making Decisions with Conditional Statements The Conditional Challenge

Nelly Nelly
Nelly Nelly
7,134 Points

Need feedback please :)

Hi there, this is my quizz...

I didn't find how to capitalize the players' answer? anyone could help ? It's easy in Ruby name.capitalize but in js it looks more complicated any one could help please ?

this is my js code

var score    = 0;
var question = 5;
var correctAnswer = 0;
var mkBirth       = 1949;
var mkBirthPlace  = "glasgow";
var mkBand        = "dire straits";
var dsLastAlbum   = "on every streets";
var mkFirstAlbum  = "golden heart";

var question1 = prompt("What year was born Mark Knopfler ? (yyyy)");
if (parseInt(question1) === mkBirth){
  document.write("<p>1. You are right, the answer was " + mkBirth + ".</p>");
  correctAnswer += 1;
  score += 1;
  question -= 1;
}
else {
  document.write("<p>1. Too bad the right answer was " + mkBirth + " not " + question1 + ".</p>");
  question -= 1;

}

var question2 = prompt("Where was born Mark Knopfler? (enter the city name in lowercase) " + question + " questions left and " + correctAnswer + "/ 5 correct answers");

if  (question2 === mkBirthPlace){
  document.write("<p>2. You are right, the answer was " + mkBirthPlace + ".</p>");
  correctAnswer += 1;
  score += 1;
  question -= 1;
} else {
  document.write("<p>2. Come on !! He was born in " + mkBirthPlace + " not " + question2 + ".</p>");
  question -= 1;

}

var question3 = prompt("In which band Mark Knopfler used to be the front man? (write in lowercase)" + question + " questions left and " + correctAnswer + "/ 5 correct answers");

if  (question3 === mkBand){
  document.write("<p>3. You are right, the answer was " + mkBand + ".</p>");
  correctAnswer += 1;
  score += 1;
  question -= 1;
} else {
  document.write("<p>3. Come on !! The band was " + mkBand + " not " + question3 + ".</p>");
  question -= 1;

}

var question4 = prompt("What was the name of Dire Straits' last album? (write in lowercase)" + question + " questions left and " + correctAnswer + "/ 5 correct answers");

if  (question4 === dsLastAlbum){
  document.write("<p>4. You are right, the answer was " + dsLastAlbum + ".</p>");
  correctAnswer += 1;
  score += 1;
  question -= 1;

} else {
  document.write("<p>4. Come on !! Dire Straits last album was " + dsLastAlbum + " not " + question4 + ".</p>");
  question -= 1;
}

var question5 = prompt("What was the name of Mark Knopfler first solo album? (write in lowercase)" + question + " questions left and " + correctAnswer + "/ 5 correct answers");

if  (question5 === mkFirstAlbum){
  document.write("<p>5. You are right, the answer was " + mkFirstAlbum + ".</p>");
  correctAnswer += 1;
  score += 1;
  question -= 1;
} else {
  document.write("<p>5. Come on !! Mark Knopfler's first solo album was " + mkFirstAlbum +  " not " + question4 + ".</p>");
  question -= 1;

}

alert("Well done! You have answered to all the questions, ready for the result?");

if (score === 5){
  alert("Congratulations you've received a Golden Crown");
  document.write("<h1> Golden Crown </h1>");
}
else if (score === 4 || score === 3){
  alert("Good job, you've received a Silver Crown");
  document.write("<h1> Silver Crown </h1>");
}

else if (score === 2 || score === 1){
  alert("Humm you don't really know Mark Knopfler... You've received a Bronze Crown");
  document.write("<h1> Bronze Crown </h1>");
}

else {
  alert("Sigh... go away an listen to Justin Bieber... You've received no Crown at all");
  document.write("<h1>Donkey cap</h1>");
}

1 Answer

Steven Parker
Steven Parker
231,236 Points

:point_right: It depends on what you mean by "capitalize":

Do you mean make every letter capital? That's easy:

newstring = oldstring.toUpperCase();

Now if you want only the first letter to be capital, you could do this:

newstring = oldstring[0].toUpperCase() + oldstring.slice(1);

And if you want to capitalize the first letter of every word, that's slightly trickier:

newstring = oldstring.replace(/\w\S*/g, function(s) { return s[0].toUpperCase() + s.slice(1) });
Nelly Nelly
Nelly Nelly
7,134 Points

Ruby's method capitalize lead me to confusion... By capitalize I meant the first letter of a word, and here in this case I needed the last solution. So thanks a lot for your post it's clearer now :)