CSS / JavaScript digit counter [closed] - javascript

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 8 years ago.
Improve this question
basically, what I am looking for is an animation for digits, a great example would be on this site:
http://www.arkanbuilds.com/
If you scroll down you'll see their 'Builds completed' etc counting up.
How do I do this? I have NO knowledge of Javascript neither jQuery if those are necessary, so help would be appreciated.
Also a small sub-question that links to this question; how can I make a number increase monthly / yearly etc, like on that website, the 'months online'.
I would appreciate the answers.

// this interval will repeat itself every 50 milliseconds
var handler = setInterval(function() {
// get the element that holds the number
var numberContainer = document.getElementById("number");
// get the number. it starts with 0
var number = parseInt(numberContainer.innerText, 10);
// increase it by 1, or 2 (number += 2;)
number++;
// update the number in the element
numberContainer.innerText = number;
// set a limit. if the number is greater than/equal to 50
// stop the interval
if (number >= 50) {
clearInterval(handler);
}
}, 50);
<div id="number">0</div>

Related

How to make a variable increase or decrease exponentially in a for loop [closed]

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 1 year ago.
Improve this question
I want to increase or decrease a variable in a exponential or speeding up manner in a piece of code that happens every frame.
This code gets executed every frame:
if (animationLenght > 1) {
animationLenght --
// implement speeding up value
...
}
Doesn't have to be exponential exactly but like that it accelerates (or decelerates)
The animation variable decreases linearly by one on every frame.
I want to increase or decrease a value in an non linear fashion, but like speeding up or slowing down smothly.
Any ideas?
You can check the Math.exp() function: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Math/exp
let expNum;
for(let i=0; i < length; i++){
expNum = Math.exp(i)
}

Select random element based on numeric attribute value [closed]

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 3 years ago.
Improve this question
I have an array of agents which each have the attribute rate which is their accepted calls minus their declined calls. I order these agents in the array based on the rate. I'd like to give agents with a better rate a higher chance to be selected for a call than other agents with a lower rate to make things fair. I understand how to select a random element from an array but I can't wrap my head around this problem.
In this code, there is a 2/15 chance to get 2, 3/15 chance to get 3, and so on.
15 is based on the total rate of all the agents.
var aoa = [{rate: 2},{rate: 3},{rate: 6},{rate: 4}];
var r = Math.random() * aoa.map(e => e.rate).reduce((ac,c) => ac+c,0);
var sa;
aoa.forEach((a,i) => {
var pt = aoa.slice(0,i).map(e => e.rate).reduce((ac,c) => ac+c,0);
if (r >= pt && r < pt+a.rate) sa = a;
console.log(`${r} has to be between ${pt} and ${pt+a.rate} if chosen rate = ${a.rate}`);
});
console.log(sa);

Is there any way to generate random numbers in javascript without using the math.random() function? [closed]

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.
Improve this question
I am trying to generate random numbers between 0 and 9 in JavaScript without using the builtin function math.random(). I need to find out a solution for this problem.
You could just use the mouse position and the time as random noise:
// Expose the current mouse position
let clientX = 0;
window.onmousemove = e => clientX = e.clientX;
// Generate random values and show them:
setInterval(() => {
document.body.textContent = (+new Date + clientX) % 10;
}, 100);
(Note that this is not random enough to be used in cryptographic / security relevant code)

Increase according Variable JavaScript [closed]

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 6 years ago.
Improve this question
First, thank you for the time you spend to help me.
Well my question is this: I have a form to make, depending on how you set the user agent to different results.
eg
1 Coin = 3 points,
I want that when the user put more than 10 coins, each coin is worth 4 points instead of 3, something like this:
10 coins = 40 points
Therefore it is a value that goes up depending on the amount
<10 Coins = 3 points
10 Coins = 4 points
50 coins = 5 Points
I have this code that it does is to add up to a fixed value, but I want that value is variable,
var conversions = {
'Monedas': {
'Puntos': 1,
}
};
How could did?
A greeting and thanks a lot.
Check this code:
function Coin()
{
var self = this;
self.points = 0;
function integerDivision(x, y){
return (x-x%y)/y
}
self.AddCoins = function(amount)
{
self.points = self.points+amount*(3+integerDivision(amount, 10));
}
}
var coin = new Coin();
coin.AddCoins(10);
alert(coin.points);
coin.AddCoins(20);
alert(coin.points);
Ok, first of all, your language attribute on your script tag should not be equal to javascript. Instead, use type="text/javascript"
Then, if I understand your question, you could use Math.floor for what you need.
var conversions = {
'Monedas': {
'Coins': ###, // I assume that you'll have a value of coins here
'Puntos': 1
},
};
var factor = Math.floor(conversions.monedas.coins / 10) + 2;
Then, you could set Puntos to factor times monedas.
conversions.monedas.puntos *= factor;
If you need to find the value of the from the form's inputs, you could use jQuery's .val or getElementsByClassName, or getElementById and then use .innerHTML to find the value, I think.
Also, what language are you coming from? Portuguese?

Adding objects into an HTML canvas with ja [closed]

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
I'm extremely new to javascript, and I'm trying to make a very simple canvas game. I want to add multiple enemies, but I want them to appear randomly, not all be hardcoded in. How would I create new objects/enemies every 5 seconds? Sorry for the probably very simple question, I have to learn this somehow, right?
Create enemies array and push to it:
var enemies = [];
setInterval(function(){
enemies.push({
x: Math.ceil(Math.random() * canvasWidth),
y: Math.ceil(Math.random() * canvasHeight)
});
}, 5000);
To do this, you can add a "set interval" function to JavaScript. This will allow you to run some code every five seconds.
setInterval(function(){
// your code to be executed every 5 seconds goes here.;
},5000);
You can also have random objects or enemies by using a random number generator. For example:
setInterval(function(){
//Math.random() returns a random number between 0 and 1.
intRandomNumber = Math.random() * 10;
//Math.round() returns the nearest whole number
intRandomNumber = Math.round(intRandomNumber);
if (intRandomNumber == 1){
//create some monster
}
else if (intRandomNumber == 2){
//create some object
}
},5000);

Categories