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 months ago.
Improve this question
I am auto incrementing a number from 0 - infinite (or whatever number), when a user refresh thier page, the newly incremented number goes back to default 0, i want to set a localstorage on the increment function, so that the user wouold still have thier new number whenever they refresh the page or visit again. This is what i have tried
<script>
var i = {{total_investment}};
function increment() {
i++;
document.getElementById('money-generated').innerHTML = `₦` + Number(i).toLocaleString('en') + `.00`;
localStorage.setItem("mining","mining");
}
setInterval('increment()', 6400);
</script>
but it does work and that is because i am doing it wrong way, i really don't know how to fix this? What would be the best way to do this?
See comments inline:
// Get the element reference just once
const money = document.getElementById('money-generated');
// Whatever total_investment is, it needs to be converted to a number
// Prepending a + to it, does that. You must retrieve the last stored
// value in localStorage first and, if it's not there, then use your
// desired value.
var i = localStorage.getItem("counter") || +{{total_investment}};
function increment() {
i++;
money.textContent = `₦` + i.toLocaleString('en') + `.00`;
localStorage.setItem("counter",i);
}
// If you want the counter to continue where it left off,
// you shouln't set it to 64000, you should set it to the
// last counter
setInterval(increment, i);
You have to set the value in localStorage and get it from localStorage to initialize the i variable.
var num = localStorage.getItem("aNumber");
var i = parseInt( num != null ? num : 0);
function increment() {
i++;
console.log(i);
localStorage.setItem("aNumber", i);
}
setInterval('increment()', 6400);
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
var robotLeaves = "The robot has left to get milk!"
var byeRobot = return robotLeaves;
I'm trying to display what's assigned to the robotLeaves variable by using a return statement. Additionally, I'm trying to assign that return statement to a variable so I can reuse it.
Which websites || resources should I check out in order to find the solution for how to accomplish this?
edit:
Here's the full code I'm experimenting with:
function getMilk() {
var shop = 10;
var robotLeaves = "The robot has left to get milk!"
var robotReturns = "The robot has come back with 1 bottle of milk!"
var byeRobot = return robotLeaves;
byeRobot;
if(byeRobot) {
--shop;
return robotReturns;
return shop;
};
}
getMilk();
2nd edit:
It turns out I was using the return statement incorrectly! Thank you to all who posted!
No offense but what you're trying to do is pretty weird. return isn't like a function or a variable that gives you a value, it only takes values. You put it in a function, and whatever you return is what you get when you use the function later on. It's always the last thing a function does, when it returns, it stops executing. So return doesn't display anything, but you can pass whatever value you return to a function that DOES.
function getRobotState() {
return "The robot has left to get milk!";
}
var robotLeaves = getRobotState();//Run the function, retrieve whatever it returns, put it in a variable so you can reuse it.
//Display value
console.log(robotLeaves)
//You can use robotLeaves as many times as you want from here downwards.
postOnTwitter("Where is the robot? " + robotLeaves)
I think you're trying to do something like this?
var shop = 10;
function getMilk() {
var robotLeaves = "The robot has left to get milk!"
var robotReturns = "The robot has come back with 1 bottle of milk!"
--shop;
console.log(robotReturns);
console.log(shop);
}
getMilk();
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?
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 8 years ago.
Improve this question
I wish to print elements of the array below
var array = [1,2,3,4,5];
However, only 3 digits must be printed at a time, e.g,
step 1: should print 1,2,3
step 2: should print 2,3,4
step 3: should print 3,4,5
step 4: should print 1,2,3
step 5: should print 2,3,4
and the loop continues .....
I wish to display the thumbnails of this slider in a manner that they will fit a small screen.
http://www.tympanus.net/Tutorials/FullscreenSlideshowAudio
That means only a few thumbnails must appear at a time, while the rest are invisible for a short while.
Thanks..
As you wanted to display 3 elements from the array each time and when the last element is displayed you want to repeat the process again you are suppose to use setInterval() to call the function after a specified time continuously and reset the index to 0 immediately after the last element is displayed. Else increment the initial or counter value. Try this way,
var array = [1,2,3,4,5];
var initVal = 0;
setInterval(function(){
console.log(array[initVal] + "," + array[initVal+1] + "," + array[initVal+2]);
initVal = initVal == 2 ? 0 : initVal+1;
}, 500);
jsFiddle
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 8 years ago.
Improve this question
I am wondering how i would make a function that will record the 10 most recent Date.now commands and then turn them into an average. I then want to put it into a side bar and make it sort of like a scoreboard. http://jsfiddle.net/eh5dxyp4/ . Thanks in advance
clickedTime=Date.now();
reactionTime=(clickedTime-createdTime)/1000;
document.getElementById("time").innerHTML=reactionTime;
this.style.display="none";
makeBox();
}
makeBox();`
You've shown quite a bit of code that doesn't seem relevant to the actual maths part of your question. I'm going to provide one way to do this part:
record the 10 most recent Date.now commands and then turn them into an average
Create an array:
var recent = [];
And a function that adds a value to that array but also ensures there will only be at most ten items in it:
var recentLimit = 10;
function addRecentItem(item) {
recent.push(item); // add to end of array
if (recent.length > recentLimit)
recent.shift(); // remove from start of array
}
Then you just need a function to calculate the average:
function getRecentAverage() {
var i,
sum = 0;
for (i = 0; i < recent.length; i++)
sum += recent[i];
return sum / recent.length;
}
So then wherever in your code you produce one of the Date.now objects you can simply add it to the recent list:
addRecentItem(yourValueHere);
And then get the current average:
console.log( getRecentAverage() );
As far as your scoreboard concept goes, you just need a function that loops through whatever is in the recent array and creates appropriate html elements (li elements, for example).
Add var reactionTimes=[]; somewhere outside the function that calulates it then use
var reactionTime = (clickedTime-createdTime)/1000;
reactionTimes.push(reactionTime);
document.getElementById("time").innerHTML=reactionTime;
if (reactionTimes.length==10) {
var average = reactionTimes.reduce(function(sum, a) { return sum + a },0)/(reactionTimes.length!=0?reactionTimes.length:1);
reactionTimes.pop(); // make ready for a new 10th value
document.getElementById("average").innerHTML=average;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a function called BiggerOf(array,value) i need a javascript code.the function calculate how many array element Bigger than that value and print these element?
-Array=[2,3,5,7,9,11,13,15,17,19].
-value=8
Try this
var noArray = [2,3,5,7,9,11,13,15,17,19];
function BiggerOf(arrayVal, val){
for (i = 0; i < arrayVal.length; i++) {
if(arrayVal[i] > val ){
alert(arrayVal[i]);
}
}
}
BiggerOf(noArray, 8);
Is this what you want?
function BiggerOf( array, value ){
var result = 0; // a variable to store the result, it starts at 0
for ( var i in array ){ // iterates the array
if ( array[i] > value ) { // checks if the current array item is bigger then value
result++; // if so, result is incremented
}
}
return result; // returns the result
}
Note: for future questions, please provide the code of what you tried, what problem did you encounter and state your problem as clearly as you can. We are not here to do your homework or guess your problem.