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.
Related
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 21 days ago.
Improve this question
const propCSS = resposta['propCSS'];
const listData = getList(cssList);
listData.lista = propCSS;
for (let i = 1; i <= list.length; i--) {
list.push(` "$ {listData}" `);
console.log(lista);
}
I'm trying to add an item to an array in a JSON file, but the result only returns me undefined at position 10.
It seems like an error in your for loop implementation.
You are loopping trough an array starting from the position 1 (let i = 1) till the end of the array, but you are descreasing the iterator (i--).
So your loop goes like: list[1] then list[0] then list[-1]... and an array can't have negative indexes.
You need to increase you iterator (i++) or start your loop from the end of the array till the begining: for (let i = list.length-1; i === 0; i--) { //push() }
Also, is this all of your code? If it is, then list.length and list.push() won't work because there is no list declared. Same thing for the console.log(lista), there's no lista declared.
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 1 year ago.
Improve this question
a function - 2 arrays : costPrice & productAndQty - trying to search all the product codes of same type in productAndQty and return a list of cost prices.
/**
* productAndQty : subscription-id, productcode, s,m,l,xl
* costprice: productcode, cp-s, cp-m ....... soo on!
*/
function getSupply(productAndQty,costPrice){
/**totalQtyAsProduct is the final array */
var totalQtyAsProduct = [];
/**co[0] is product code */
costPrice.forEach(function(co){
var tempPQ = productAndQty.filter(function(po){
return po[1]==co[0];
});
c(tempPQ);
c(tempPQ!=[]);
if(tempPQ!=[]){
totalQtyAsProduct.push(calcSupply(tempPQ,costPrice));
}
});
return totalQtyAsProduct;
}
but if(tempPQ!=[]) always return true;
when I console.log(tempPQ), there are many [] that i dont want to pass to next function!
Just check length of array returned by filter(). An empty array has zero length which is falsy
if(tempPQ.length)
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 6 years ago.
Improve this question
If you had an array with 5 items, what would be the best way to check if those items are in order? Say 1-5 or 6-11?
EDIT
Knowing a loop will work, I'm wondering if there might be an alternative solution.
Loop through, if the current is less than the previous return false.
function consecutive(arr)
{
for(var i = 1; i < arr.length; i++){
if(arr[i] < arr[i-1]) return false;
}
return true;
}
console.log(consecutive([10,11,45,100]));
console.log(consecutive([10,3,11,35]));
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 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;
}