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?
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 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);
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
The book that I'm studying says about iterating over arrays with every that:
The function these methods use must follow one rule—it must accept
three arguments like the following code:
function functionName(value, index, array) {
// do something here
}
Does that mean that I must always use 3 arguments? If so then why does this code work?
var numbers = [ 1, 2, 2 ];
function isLessThan3(value) {
var returnValue = false;
if (value < 3) {
returnValue = true;
}
return returnValue; }
document.write(numbers.every(isLessThan3));
There is no limitation on how many arrguments you can put in a function with Javascript.
you have a very good explenation about this topic in the next answer by #Niet the Dark Absol
https://stackoverflow.com/a/22747272/1283672
i believe that the book was reffering to something more specific within it's scope.
And just to be clear you can put no arrgs in a function either.
It's a bit ugly, the code, you have, but there is help. You might use the following without a temporary variable. Just return the result of the comparison.
function allLessThan3(value) {
return value < 3;
}
var numbers = [1, 2, 2];
console.log(numbers.every(allLessThan3));
No, you can use from 0 to 3 arguments
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
Practice Problem 1
Parameters: The function accepts two positive integers N and M.
Return Value: The function returns the product of N and M. For example, if the integers 5 and 8 are supplied to
that function, it should return the product of 5 and 8—it should return 40.
Additional
Requirements:
Do this without using the multiplication operator (*). Hint: Multiplication is just a series of addition
operations.
function mult(N, M) {
return N / (1 / M);
}
Since this is a basic excercise, I think that this answer is not expected (but maybe you'll get bonus points if you can explain it), even though it does the math without *.
function mult(N,M){
var a = new Array(N);
return a.join(""+M).split("").reduce((x,y)=>(parseInt(x)+parseInt(y)))+M
}
Note: This does not work for N < 3. No time to correct it.
OK. Look. It sounds like you're quite young so I think giving you the benefit of the doubt is alright here. So you know for the future: Stackoverflow isn't a site that you can just drop a homework question and expect people to do the work for you.
We sometimes do help with homework questions but only if it looks like you've at least attempted to answer the question yourself, by showing us some code that you've written. If you want to use SO in the future you might find the help section useful, particularly the section on how to write a good question.
OK, lecture over.
What the question is asking about is how to use a simple for loop to add some numbers together:
function getProduct(num1, num2) {
// set total to zero
// we'll be adding to this number in the loop
var total = 0;
// i is the index, l is the number of times we
// iterate over the loop, in this case 8 (num2)
for (var i = 0, l = num2; i < l; i++) {
// for each loop iteration, add 5 to the total
total += num1;
}
// finally return the total
return total;
}
getProduct(5, 8); // 40
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
How to use a while loop to generate random numbers until a certain number is reached? Help please
This code will generate integer numbers from 1 until 50.
var number = 50;
var x = 0;
while(x !== number){
x = Math.floor((Math.random() * 50) + 1);
console.log(x);
}
Considering there is a random number generator random you will just do as shown below
do
{
x=Math.random();
x=x*range;
if(x is desired number )
break;
else
{
print the number.
}
}while(1)
print -( desired number found).
NOTE: As you are beginner you can try the following when designing these codes..
First find what is the input and what is the output.
Then try to draw a flow chart of the whole thing ( You can try running it yourself as if you are the computer)
Then all the branch and loop are converted to if-else or for-while loop.
Check the code and match input and output.
This is a basic program structure..Instead of asking in forums about common structures try reading some books.
Hint 1: There is a function in javascript called Math.random() return a random number between 0 (inclusive) and 1 (exclusive):
Hint 2: check whether the values are equal. Use === in javascript.
Hint 3: check for if else, break and do while, while loop.
Hint 4: If you are given a random number 0<=x<1 then how do you generate a random number using in range [a,b) using the abobe geerated number? Ans: Use b*x.
hope this helps.
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;
}