I am using a random function to randomly slide between 4 swiper sliders, here is my code :
var swipers = [swiper1, swiper2, swiper3, swiper4];
setInterval(function(){
var rand = Math.floor(Math.random() * 4);
swipers[rand].slideNext();
}, 3000);
}
How should i proceed if i wanted to randomize between slideNext() and slidePrev() ? I tried many things like this :
var direction = ['slideNext()', 'slidePrev()'];
var swipers = [swiper1, swiper2, swiper3, swiper4];
setInterval(function(){
var randx = Math.floor(Math.random() * 4);
var randy = Math.floor(Math.random() * 2);
swipers[randx].direction[randy];
}, 3000);
But it's not working, probably for obvious reasons, but please help me understand why it's not working. And how i could possibly achieve that?
Just put the method name in the arrays, without parentheses. Then use it to index the object, and call the result of that.
You should also use <arrayname>.length rather than hard-coding the lengths when you get the random index, so you don't have to change it if you add more swipers or directions.
var direction = ['slideNext', 'slidePrev'];
var swipers = [swiper1, swiper2, swiper3, swiper4];
setInterval(function(){
var randx = Math.floor(Math.random() * swipers.length);
var randy = Math.floor(Math.random() * direction.length);
swipers[randx][direction[randy]]();
}, 3000);
You're pretty close! Just change how you're setting the direction and I think you got it:
swipers[randx][direction[randy]]();
Also remove the parenthesis from your array and just leave the function names there.
Related
I created an automatic slideshow in a html webpage, i want to display some text outside the images. This is the script I'm using currently, I had asked on quora they gave me this script
var words = ['beautiful', 'cool', 'amazing'];
var t = setInterval(function() {
var randomNumber = Math.round( Math.random() * (words.length-1) );
$('#changing').html( words[ randomNumber ] );
}, 2000);
But this is randomizing the caption, and I want it in sequence so that it displays it to the approp image. Can someone please help me?
This sounds like you want to keep track of the previous index used in the array, maybe try something like this?
var words = ['beautiful', 'cool', 'amazing'];
var i = 0;
var t = setInterval(function() {
$('#changing').html( words[i] );
i++
if (i >= words.length) i=0;
}, 2000);
I've set up a simple matching game using JavaScript to gather the cards in an array and then randomly repopulate the page. Everything seems to be working great except for one thing; the last image always stays the same. I can't seem to figure out why.
Here's a link to the game: http://hdesigns.x10.mx/matching/
Any help would be greatly appreciated.
Math.random returns a number between 0 and .9 repeating. Never 1.
Change
var idx = Math.floor((Math.random() * (deck.length-1)));
to
var idx = Math.floor(Math.random() * deck.length);
on line 98 of main.js.
Try:
var idx = Math.floor(Math.random() * deck.length);
Instead of:
var idx = Math.floor((Math.random() * (deck.length - 1))));
I am trying to set a variable random number of dots. I can generate random numbers using Math.random(). I tried this without any luck:
function generate() {
Math.floor(Math.random() * 500)
}
var randomdots = generate();
What is the correct approach to set a variable random number of dots?
This method does nothing useful, it throws away the result before using it. Maybe you want:
function generate() {
var count = Math.floor(Math.random() * 500);
var result = '';
for (i = 0; i < count; ++i) {
result = result + '.';
}
return result;
}
document.write(generate());
Remember that functions in JavaScript must have a return if you want to get a value from them.
You can also use this
function generate() {
var index = Math.floor(Math.random() * 500);
return new Array(index).join(".");
}
var randomdots = generate();
You could have a JavaScript that writes bullets on page load. Do it like this:
var number = Math.ceil((Math.random() * 500)) + 1;
for (i=0;i<number;i++){
document.getElementById('output').innerHTML += '• ';
}
<p id="output"></p>
Let me know if you need additional help!
Although caslaner's answer seems to be the easiest way to achieve this, for educational purposes, here's a recursive function that does the same.
function generate(str,rm) {
if(rm === undefined) rm = Math.floor(Math.random() * 500);
return rm ? generate((str||'') + '.',rm-1) : str;
}
document.write(generate());
I'd like to show a random numbers within my list. Im grabbing the list from the backend, and im just using these numbers as examples. This is my code, I'm not quite sure why its not filling in the text.
setInterval(function() {
var number = 1 + Math.floor(Math.random() * 6);
$('.num-gen').text(number);
},
Made a fiddle here to show you what I'm working with. Thanks in advance.
You had an error in your code, you were missing the finishing , 1000/*time in ms*/);!
Also, jQuery is not required here, you may do this in Javascript like this.
setInterval(function() {
var number = 1 + Math.floor(Math.random() * 6);
document.getElementsByClass('num-gen')[0].innerHtml = number;
}, 1000);
EDIT:
setInterval(function() {
jQuery.each(jQuery('.num-gen'),function(){
var number = 1 + Math.floor(Math.random() * 6);
jQuery(this).text(number);
});
}, 1000);
you have syntax error in you code, change it to:
setInterval(function() {
var number = 1 + Math.floor(Math.random() * 6);
$('.num-gen').text(number);
}, 10);
I have a var..
var random = Math.ceil(Math.random() * 8.8);
and I have a click function
$('.passShort').bind('click', function() {
// do something here and get new random number
});
I'm trying to change the global random var not just inside this particular function.
I like to strictly define global variables when they need to be truly global, and I avoid repetitive code when possible:
setRandom();
$('.passShort').bind('click', setRandom);
function setRandom() { window.random = Math.ceil( Math.random() * 8.8 ); };
Setting the variable on the window object ensures it's truly global. You can refere to it as random anywhere and it will give you window.random but using window.random assures you are setting the value of the global random variable.
Use var outside the function, but not inside it:
var random = Math.ceil(Math.random() * 8.8);
$('.passShort').bind('click', function() {
random = Math.ceil(Math.random() * 8.8);
});
Depending on where you declared the random variable will determine it's scope. If you want to make it global, just declare it without the var keyword.
random = Math.ceil(Math.random() * 8.8);
Really, it would nicer if you could combine the functionality you're looking for into some re-usable object, a Random Number Generator? An example might be:
var RNG = {
get randInt() { return Math.ceil(Math.random() * 8.8); },
get randFloat() { return Math.random() * 8.8; },
randRange: function(min, max) {
return min + Math.floor(Math.random() * (max - min + 1));
}
};
console.log(RNG.randInt);
console.log(RNG.randFloat);
console.log(RNG.randRange(5,10));
$('.passShort').bind('click', function() {
console.log(RNG.randInt); // Whatever you want here.
});