-1
var firstName= prompt("What is your first name?");
var lastName= prompt("What is your last name?");
var nicknames = ["the champ","the boss","the amazing");

function generator(){
var random = Math.floor(Math.random()*nicknames.length);
var nickname=firstName+nicknames[random]+lastName;
document.write(nickname);
}
window.onload=generator;

The above code is linked to an html page and it is not doing anything

4
  • 1
    Pull up your console (F12) -- any errors?
    – tymeJV
    Commented Dec 18, 2013 at 21:22
  • 5
    I think it might be that there's a syntax error closing your nicknames array shoudl be a ] and you have ). As stated above the console would show that? Commented Dec 18, 2013 at 21:23
  • in what way is it not working? Commented Dec 18, 2013 at 21:25
  • wow that was the problem, silly mistake but thank you
    – Justin
    Commented Dec 18, 2013 at 21:27

2 Answers 2

6

You don't seem to be closing your array correctly (you need to change the final bracket from ")" to "]" ), try:

var firstName= prompt("What is your first name?");
var lastName= prompt("What is your last name?");
var nicknames = ["the champ","the boss","the amazing"];

function generator(){
var random = Math.floor(Math.random()*nicknames.length);
var nickname=firstName+nicknames[random]+lastName;
document.write(nickname);
}
window.onload=generator;
2

The short answer is that you have a typo on this line:

var nicknames = ["the champ","the boss","the amazing"];
                                                    ^^^

If you use Chrome, FireFox, or Safari, then you can open something called the Developer Console by hitting [ctrl]-[shift]-[j]. This will open "the console". Other browsers have similar functionality as well.

The console will show you errors (in red text) describing any JavaScript errors that have occurred while the page was loading. It has a lot of other great features that will help you whenever you work in JavaScript or HTML.

You can also paste JavaScript directly into the console, as shown here

Not the answer you're looking for? Browse other questions tagged or ask your own question.