2

I wrote a simple code to choose random name from four, using "random_word();" function and it doesn't work. It says:"undefined". Can anyone explain me why and help to fix it? Thanks for all help.

var randomWord;

var word = new Array(3);
word[0] = "Michael";
word[1] = "Simon";
word[2] = "Peter";
word[3] = "Mark";

function random_word(){
var randomWord = word[Math.floor(Math.random()*word.length)]
}

random_word();

document.write(randomWord);
2
  • btw, the array declaration with 3 makes no sense, because the assignment has 4 items. Commented Nov 29, 2016 at 21:25
  • If you're learning Javascript, make sure to use recent references and tutorials Commented Nov 29, 2016 at 21:40

3 Answers 3

3

You make randomWord to a private variable of the function with a var statement. Without, you could use the global variable for the result.

function random_word() {
    randomWord = word[Math.floor(Math.random() * word.length)];
}

var randomWord;
    word = ["Michael", "Simon", "Peter", "Mark"];

random_word();
document.write(randomWord);

A better style and more concise would be the return of the selected item. An even better version, would be if you use the array as parameter as well, because you could use the function for other arrays as well.

function getRandomItem(array) {
    return array[Math.floor(Math.random() * array.length)];
}

var words = ["Michael", "Simon", "Peter", "Mark"];

document.write(getRandomItem(words));

Bonus, add random name with interval.

function getRandomItem(array) {
    return array[Math.floor(Math.random() * array.length)];
}

var words = ["Michael", "Simon", "Peter", "Mark"];

setInterval(function () {
    document.getElementById('names').innerHTML += getRandomItem(words) + '<br>';
}, 2000);
<div id="names"></div>

Sign up to request clarification or add additional context in comments.

2 Comments

(Option 1) - Cool. I have one more question. If I put "document.write(randomWord);" into a "write()" funtion and add "setInterval(write, 2000);" at the end of the code it chooses random word from this four. But if I would like it to choose random name every time (every 2 sec in this case) what i suppose to do?
Thank you. Or maybe: Danke schön :)
3

The local variable randomWord in the random_word function context has nothing to do with the randomWord declared in the outer context. If you want to reset the value of the outer context randomWord variable then you should drop the var keyword in your function.

function random_word(){
    randomWord = word[Math.floor(Math.random()*word.length)]
}

But this is not a good way of programming. Ideally the function should return a value.

function random_word(){
  return word[Math.floor(Math.random()*word.length)];
}

var randomWord = random_word();
var anotherRandomWord = random_word();

Comments

0

You are using var keyword in your function which creates a local variable. When you get out of that function the global one is not set.

Use this instead:

function random_word(){  
    randomWord = word[Math.floor(Math.random()*word.length)]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.