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]));
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to use a for loop to display all the elements in my array that are greater than 5. I am using an if-statement to determine the values greater than five, but when I run the code, I get values printed to the console that are both below and over five. I have tried creating variables to store the index values and tried using the && operator, but none of these have worked.
Here is the code for reference:
var myArray = [];
appendItem(myArray, randomNumber(1,10));
appendItem(myArray, randomNumber(1,10));
appendItem(myArray, randomNumber(1,10));
console.log("Original: " + myArray);
console.log("Values greater than 5");
// the part I have issues with
for (var i = 0; i < myArray.length; i++) {
if (myArray[i] > 5) {
console.log(i);
}
}
You are printing i not the array element.
try
console.log(myArray[i]);
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 4 years ago.
Improve this question
I wrote a for loop in JavaScript which is simple and straight forward. But, it is skipping the 2nd index. I don't understand why?
Below is my code snippet :
if($scope.usersChoice.length == $scope.correctAnswers.length){
for(var p=0;p<$scope.usersChoice.length;p++) {
if($scope.usersChoice[p] == $scope.correctAnswers[p]){
$scope.score++;
}
}
}
Here the length is 10.
How do you know it is skipping the 2nd index? Because it should'nt.
Can you show us your arrays ?
By the way, you can use forEach instead of for loop in this case, since you don't seem to need to break your loop:
if($scope.usersChoice.length == $scope.correctAnswers.length){
$scope.usersChoics.forEach($choice,$index=>{
if($choice === $scope.correctAnswers[$index]) $scope.score++;
});
}
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 7 years ago.
Improve this question
function first(array){
return first(0);
var first = ["gold","brown","green"21,1998];
console.log(first[0]);
trying to create a function that will return the first object of a given array, and am getting an error self[e.data.invoke.func].apply is not a function
not sure what I'm missing?
I tried this and it worked
function firstObject(array){
return array[0];
}
var first = ["Weresquirrel", "Werebear", "Werepanda", "Weremonkey"];
console.log(firstObject(first));
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 created a simple fuzzbuzz in javascript (see below). I however would like to include the following:
if the number starts with a 1 (so fe 11) "ping" should be added. So 15 should be FizzBuzzPing etc...
Any thoughts?
function fizzBuzz() {
for (var i=1; i <= 20; i++)
{
if (i % 15 == 0)
alert("FizzBuzz");
else if (i % 3 == 0)
alert("Fizz");
else if (i % 5 == 0)
alert("Buzz");
else
alert(i);
}
}
$(document).ready(function(){
$('#clickMe').click(function(){
fizzBuzz();
});
});
Convert the number to a string and take the first index of the string:
var digit = (''+i)[0];
Or, the alternative
var digit = i.toString()[0];
Then check if digit is equal to 1 or not and add things or not accordingly.
For a future reference: Spend some time searching for a solution to your problems, don't ask questions unless you've spent some time making sure an answer does not exist to your question. A similar question has been answered many times before. Maybe it's not about fuzzbuzz but you should be able to find two different answers to two different questions and be able to combine then into your solution.
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.