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;
}
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 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 6 years ago.
Improve this question
I have a sorted array of objects, each with start and end coordinates, stated relative to a larger range that encompasses all of these coordinates. I would like to create new objects and put them into the array.
JsFiddle link is at the bottom.
Here's a visual representation:
before:
|---------------------------------------|
|-----| |----------| |------|
after:
|---------------------------------------|
|---|-----|-----|----------|---|------|-|
I was trying to use a for loop to find the missing ranges and then splice in the appropriate object as I found them. This creates an infinite loop.
I think that I could feasibly create a temporary array with the filled-in objects, then concatenate that with my original array and sort by start coordinate, but I'd like to do it without having to sort the array again.
Here is a link to a jsFiddle
I think your problem is, you are using splice to "modify" the subfeatures[] array, (adding a new element) but at the same time you are looping over that array, that causes infinite loop, I think your logic is good, instead of using splice, it may be easier to just construct a new array
the commented line are the only modification you have to do.(you also have to consider if the last element not ending at the upper limit)
//var newArr=[];
for (i = 0; i < subfeatures.length - 1; i++) {
//newArr.push(subfeatures[i]);
if ( subfeatures[i].end != subfeatures[i+1].start) {
var feat = {start: subfeatures[i].end, end: subfeatures[i+1].start, type: null};
console.log("A feature should be placed after the current index: "+i+". This feature would have the starting point: "+subfeatures[i].end+" and the ending point: "+subfeatures[i+1].start);
//newArr.push(feat);
}
}
//return newArr;
Splice() is not optimal because you have to move all the later elements in the array every time you find a gap. Here's the solution with a new array that Kossel is suggesting:
var newArray = [];
for (i = 0; i < subfeatures.length - 1; i++) {
newArray.push(subfeatures[i]);
if ( subfeatures[i].end != subfeatures[i+1].start) {
var feat = {start: subfeatures[i].end, end: subfeatures[i+1].start, type: null};
newArray.push(feat);
}
}
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 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
If I have phone number ranges such as 5555555555-5599 5555550000-0003 5555550005-0007, my mission is to have it return all results without using a server. Is it possible for it to return without a server a result that would look like:
5555555555
5555555556
5555555557
/* etc */
My previous post about javascript has helped me up to this point but I wanted to rehaul the whole site.
Javascript dashes in phone number
If you could point me in the right direction, I would really appreciate it. I'm just having a mind block right now if this is even possible.
Given a single phone range in the form of "xxxxxxyyyy-zzzz", split the whole string on the dash and the first part of the string at the 6th index. This yields three strings "xxxxxx", "yyyy", and "zzzz". Using a for loop, you can create an array of phone numbers by concatenating the prefix "xxxxxx" onto the range "yyyy"-"zzzz":
// Get an array from a given range "xxxxxxyyyy-zzzz"
function fromRange(range) {
var phoneNumArray = [];
var prefix = range.substring(0,5);
var suffixRange = range.split("-");
for (var suffix = suffixRange[0].substring(4, -1);suffix < suffixRange[1];suffix++) {
phoneNumArray.push(prefix + suffix);
}
return phoneNumArray;
}
Try it in JSFiddle.
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