Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Can anyone tell me how to generate random value between 0 and 100 in an interval of 5 seconds.
function randomIntFromInterval(min,max)
{
return Math.floor(Math.random()*(max-min+1)+min);
}
var randomNum = 0;
setInterval(function () {
randomNum = randomIntFromInterval(0, 100);
}, 5000)
With help from
Math.random(),
setInterval()
function setRandom() {
document.getElementById('out').innerHTML = Math.random() * 101 | 0;
}
setRandom();
setInterval(setRandom, 5000);
<div id="out"></div>
Well, Thanks for the repliy #nina-scholz #ilian6806 .
var random = 0;
random = randomizator(0,100);
function randomizator(a,b)
{
return Math.floor(Math.random()*b) + a;
}
I used another function to handle my Interval.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am trying to calculate the 10% from total array of values
for eg
perc = [56,50];
function percentage(perc) {
return (perc / 100) * 10;
}
You need to first sum the array, and then calculate 10% of that sum:
function percentage(perc) {
const sum = perc.reduce((a, b) => a + b, 0)
return sum * 0.1;
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I have the following code:
// Reference to the <div> which displays the random number:
var rndDiv = document.getElementById('rndNum')
// Reference to the <button> which generates the random number:
var rndBtn = document.getElementById('rnd')
// Generating the random number through 'click' eventlistener:
rndBtn.addEventListener('click', function intRnd() {
var n = Math.floor((Math.random() * 10) + 1);
console.log(n)
rndDiv.innerHTML = n
})
how could/should i write this code differently, how would you write it? Would you use, for example, arrow functions? let instead of var? I'm just curious. Also i'm the total opposite of a 'pro'-coder, just a beginner, and would like to read your code to this solution.
Thanks for taking your time and reading my post!
Here you go ... !
IIFE
Arrow Function
Let
(function() {
let rndBtn = document.getElementById('rnd');
rndBtn.addEventListener('click', () => {
let rndDiv = document.getElementById('rndNum');
rndDiv.innerHTML = Math.floor((Math.random() * 10) + 1);
});
})();
<button id="rnd">Click</button>
<div id="rndNum"></div>
Here is another way
const randomNumGenerator = () => Math.floor((Math.random() * 10) + 1);
const randomNumDiv = document.getElementById('rndNum');
document.getElementById('rnd').addEventListener('click', () => {
randomNumDiv.innerHTML = randomNumGenerator();
});
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I got an image of all number from 0 to 9. I want to count up from 0 to 1000000 with img tag. For example if number == 1 position of img tag would be top = 100px
var count = 0;
var counter = function() {
if (count == 1000) {
count = +1;
$(".number-inner").text(count);
}
count++;
$(".number-inner").text(count);
setTimeout(counter, 10)
}
setTimeout(counter, 1000)
Any ideas?
Hi you need to cut your image to 9 images, so it will be each image have a number then you can name the image with number, for example the image who had number 4 name it 4.png .... Here is my code and project folder to see what i did
code:
var i=0, j=0;
var count = setInterval(function(){
if(i!=9){
i++;
}
else{
j++;
i=0;
}
$('.stop').html(j+''+i).hide();
$('.counter').html('<img src='+j+'.png><img src='+i+'.png>');
if($('.stop').html()==14)
clearInterval(count);
},1000);
just remove 14 to 10000 ( or whatever you want )
image:
result ( the counter stop at number 14 ):
hope my answer usefull
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have a number for example 4, i want to get next number on multiples of 3
Multiples of three: [3,6,9,12,15,18,21,24,27,30,...]
the result must be 6
i'm looking for a javascript function
something like this:
function (myNum) { //myNum = 4;
var multiples = [3,6,9,12,15,18,21,24,27,30];
var result;
// do something!!
return result; // returns 6
}
thanks
I suggest another solution:
function getNext(num, dep){
return (((num % dep) ? dep:0) - num % dep) + num;
}
document.write(getNext(4, 3));//6
//document.write(getNext(200, 7));//203
Updated: You can use this method for finding next number on multiples of any number
There are a lot of ways you can achieve this. Here is an easy one. Increment the number until you get a multiple of three.
function multipleOfThree(num){
while(num % 3 != 0)
num++;
return num;
}
You must try before asking a question. If you stuck at somewhere then it is good to ask questions with problem. By the way here what you can try:
function multiple(number) {
return number % 3 === 0 ? ((number/3) * 3) : parseInt((number/3) + 1) * 3;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I have a game that gives a time limit and I need to display a countdown clock for the users and stop the game once the time is up such as 30 seconds. How can I do this in javascript?
Use setInterval to set up a timer. Within this timer, you can update some text in your page and when the time is up, you can call whatever function you want:
var timeLeft = 30;
var elem = document.getElementById('some_div');
var timerId = setInterval(countdown, 1000);
function countdown() {
if (timeLeft == -1) {
clearTimeout(timerId);
doSomething();
} else {
elem.innerHTML = timeLeft + ' seconds remaining';
timeLeft--;
}
}
<div id="some_div">
</div>
Check out setTimeout and setInterval:
http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/
You can use setTimeout() function for this like :-
let timeElm = document.getElementById('timeElm');
let timer = function(x) {
if(x === 0) {
return;
}
timeElm.innerHTML = x;
return setTimeout(() => {timer(--x)}, 1000)
}
timer(30);
In this, we use recursion and gave it a setTimeout() of 1000ms for reducing the time.