2

This is what I came up with but it doesn't work. I don't get an output.

Javascript.js

function randomnamegen() {
    var names = ["Mango", "Pul", "Bat", "Tim", "Mh", "Hei"];
    var namein = names(Math.floor((Math.random() * 5) + 0));
    document.getElementById("name").innerHTML = namein;

}

index.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Cherry Cheese Danish</title>

</head>
<body>
<button type="button" onclick="randomnamegen()">Click Me!</button>
    <p id="name"></p>
    <script src="JavaScript.js"></script>
</body>
</html>

I get an Uncaught TypeError: object is not a function JavaScript.js:3 in google chrome console.

1
  • 2
    Use square brackets to access array items: names[...]; Commented Oct 2, 2014 at 16:42

4 Answers 4

1

Use [] square brackets for Arrays:

var namein = names[ Math.floor((Math.random() * 5) + 0) ];

Also instead of using strictly 5 (while you have 6) you can let JS do the count:

Math.random() * names.length

function randomnamegen() {
    var names = ["Mango", "Pul", "Bat", "Tim", "Mh", "Hei"];
    var namein = names[ Math.floor(Math.random() * names.length) ];
    document.getElementById("name").innerHTML = namein;
}
<button type="button" onclick="randomnamegen()">Click Me!</button>
    <p id="name"></p>

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

Comments

0

Your code is working pretty well. You have to change the () by [] in:

var namein = names(Math.floor((Math.random() * 5) + 0));

The correct one is:

var namein = names[Math.floor((Math.random() * 5) + 0)];

The console was giving the following error: "Object is not a function".

Check this link jsfiddle to see a working example.

Hope it's useful!

Comments

0
  • Use square brackets
  • There's no need to add 0

var namein = names[Math.floor(Math.random() * 5)];

or use ^ 0 for the rounding

var namein = names[Math.random() * 5 ^ 0];

Comments

0

This is a Fisher-Yates implementation intended to shuffle arrays.

function shuffle(array) {
    var currentIndex = array.length,
        temporaryValue,
        randomIndex;

    while (0 !== currentIndex) {
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;

        temporaryValue = array[currentIndex];
        array[currentIndex] = array[randomIndex];
        array[randomIndex] = temporaryValue;
    }

    return array;
}

After shuffling the array you can retrieve values like this:

var array = [1, 2, 3, 4, 5];

function retrieve() {
    return shuffle(array)[0];
}

In case you don't want your items to repeat:

var array = [1, 2, 3, 4, 5],
    temp = [];

function retrieve() {
    if(temp.length > 0) {
        return temp.pop();
    } else {
        temp = shuffle(array.slice());
        return temp.pop();
    }
}

Not sure if this is the optimal answer though.

function shuffle(array) {
    var currentIndex = array.length,
        temporaryValue,
        randomIndex;

    while (0 !== currentIndex) {
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;

        temporaryValue = array[currentIndex];
        array[currentIndex] = array[randomIndex];
        array[randomIndex] = temporaryValue;
    }

    return array;
}

var array = ["Mango", "Pul", "Bat", "Tim", "Mh", "Hei"];

function retrieve() {
    document.getElementById("name").innerHTML = shuffle(array)[0];
}
<button type="button" onclick="retrieve()">Click Me!</button>
    <p id="name"></p>

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.