Isabela Isabela - 3 months ago 8
Javascript Question

Using Math.Random and vars, in JS function

how are you?
I need help in JS code. I need to put random words within that function. Choose one of them.

This is the code:

// Plugin invoke
$(document).ready(function () {
$("#popup-1").slickModals({
(...)
windowLocation: "center";
(...)


How do I do this using a math.random words in "WindowLocation"?

The words are: "Center" or "TopRight" or "TopLeft".

something similar result:

// Plugin invoke
$(document).ready(function () {
$("#popup-1").slickModals({
(...)
windowLocation: "center","Topright","Topleft";
(...)


Thank You, sorry my english bad.

Answer

Try the following. Math.random() gives you a random number between 0 (inclusive) and 1 (exclusive). You want to scale it up and then get the integer.

function randomValue() {
    var words = ["center","Topright","Topleft"];
    var length = words.length;
    var i = Math.floor(Math.random()*length);
    return words[i];
}
alert(randomValue());