-1

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.

  • 1
    Is there a list of words to choose from? `Math.random()` returns a random number between 0 and 1, and then you could multiply that by the length of the array of words to pick a random word out of it. But you need some base list, and javascript does not come with one. (example: `["Center","TopRight","TopLeft"][Math.floor(Math.random() * 3]` will return one of those three words randomly) – somethinghere Aug 10 '16 at 14:17
  • 1
    The `;` in that line can’t be correct. If that’s an object property, you need to sparate it with `,`. – Sebastian Simon Aug 10 '16 at 14:18
  • Yes, i have a list of words. my problem is put the random within this function. Example: windowLocation: ["Center","TopRight","TopLeft"][Math.floor(Math.random() * 3], – Isabela Cristina Aug 10 '16 at 14:30

2 Answers2

0

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());
AgataB
  • 647
  • 2
  • 10
  • 18
0

You have to define an array with your words:

var positions = ["center","Topright","Topleft"];

Than you can do:

$(document).ready(function () {
    $("#popup-1").slickModals({
      (...)
      windowLocation: positions[Math.floor(Math.random()*(positions.length-1))],
      (...)
   });
 });

You should use it so:

Math.random()*(positions.length)

This give you a range of random number that are fitted with the array size.

But are still float values, so you have to convert to integers to have a valid index:

Math.floor(Math.random()*(positions.length))

Now you have a range from 0 to position.length.

Mario Santini
  • 2,905
  • 2
  • 20
  • 27
  • 1
    Uhm, actually you will see that `Math.floor(Math.random()*(positions.length-1))` _never returns the last item_. Thats because random is always between 0 and 1 and therefor will never be flatly 1, so what you want is `Math.floor(Math.random()*positions.length)` - since your flooring will round any number very close to 1 down to `positions.length - 1`; – somethinghere Aug 10 '16 at 14:35
  • Thank you for explanation :D – Isabela Cristina Aug 10 '16 at 14:49
  • 1
    @somethinghere thanks I update my post. :) – Mario Santini Aug 10 '16 at 15:30
  • Btw, you'd be right if you used `Math.round`. And corrected my accidental woopsie of my previous comment -_-. – somethinghere Aug 10 '16 at 16:28