CAPTN_BLCKA
CAPTN_BLCKA

Reputation: 65

How to get random value from an array?

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.

Upvotes: 2

Views: 354

Answers (4)

Renato Gama
Renato Gama

Reputation: 16519

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>

Upvotes: 0

Roko C. Buljan
Roko C. Buljan

Reputation: 206092

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>

Upvotes: 1

GOTO 0
GOTO 0

Reputation: 47662

  • 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];

Upvotes: 0

Academia
Academia

Reputation: 4124

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!

Upvotes: 0

Related Questions