JavaScript JavaScript Basics Working With Numbers The Mad Libs Challenge Revisited

James Barshaw
James Barshaw
5,548 Points

code won't work for strings and numbers

I am learning about strings and numbers. whats wrong with my code please explain because It won't do anything and I can't figure out whats wrong with it

var questions = 3; var questionsLeft = '['+ questions + ' questions left]'; var adjective = prompt('please type an adjective' + questionsLeft); var verb = prompt ('please type a verb'); var noun = prompt ('please type a noun'); alert ('All done. Ready for the message?'); var sentence = "<h2> there once was a" + adjective; sentence + = 'programmer who wanted to use javascript to ' + verb; sentence + = 'the' + noun+'.</h2>'; document.write(sentence);

1 Answer

Matthew Long
Matthew Long
28,358 Points

Looks like you have some spaces where there shouldn't be. Specifically where you use the addition assignment operator, +=. You have it as + =.

There are some other areas where your code could be cleaned up as well. Such as removing the spaces between between alert/prompt and the opening parentheses. This isn't breaking your code though. You could also add a space in your strings so that the words don't run together. But other than the assignment operator it looks ok. You should be able to continue the videos with this:

var questions = 3; 
var questionsLeft = '['+ questions + ' questions left]'; 
var adjective = prompt('please type an adjective' + questionsLeft); 
var verb = prompt('please type a verb'); 
var noun = prompt('please type a noun'); 
alert ('All done. Ready for the message?'); 
var sentence = "<h2>There once was a " + adjective; 
sentence += 'programmer who wanted to use javascript to ' + verb; 
sentence += ' the ' + noun + '.</h2>'; 
document.write(sentence);

Side note: you can wrap your code snippets in 3 back ticks to have it formatted here in the forums.