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]);
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 1 year ago.
Improve this question
I have a simple code, its essence is to remove duplicate numbers from a sheet. But on my server, the data received does not match the data on leetcode. For what reason could this be?
var deleteDuplicates = (head) => {
for(let i = 0; i < head.length; i++) {
for(let a = i + 1; a < head.length; a++) {
if(head[i] === head[a] ) {
head.splice([a], 1)
}
}
}
return head;
};
console.log(deleteDuplicates([1,1,2]));
Output: [1,2]
Leetcode:
Your input
[1,1,2]
Output
[1,1,2]
Expected
[1,2]
How about using a Set? It's simpler and you'll avoid modifying a list that you're iterating over.
var deleteDuplicates = (head) => [...new Set(head)];
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 1 year ago.
Improve this question
This is my code but when i run this code in console always giving me Congratulations.
Help me for solve this problem.
var val=Math.floor(Math.random() * 10) + 1;
console.log(val);
var Predict = Number(prompt("Prediction ?"));
for(var i=1 ; i <= 3; i++){
if(Predict<val){console.log("Up")};
if(Predict=val){console.log("Congratulations") };
if(Predict>val){console.log("Down")}
}
Equal operator assigns the right hand to the left hand and so the result is always true! To compare two values use double equals like this:
if (Predict==val){console.log("Congratulations") };
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 am doing a coding challenge where the letters in a string are replaced by their partner letters, I know that there is probably a better way to do this, but I just want to know why this doesn't work. Here is the code:
function DNAStrand(dna){
var strObject = {"A":"T", "T":"A", "C":"G", "G":"C"};
let newDna = "";
for (let i=0; i < dna.lenght; i++){
newDna += strObject[dna[i]];
}
return newDna
}
the function returns an empty string or "", the value of newDna before the loop, it doesn't change.
for (let i=0; i < dna.lenght; i++){
You made a typo. Change this to dna.length.
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 4 years ago.
Improve this question
I've written an extremely simple for loop in JS and the loop just doesn't run. My code is
for (i = 0; i >= 100; i++) {
console.log('boop');
};
and the loop doesn't even do anything. Does anybody know why the code just gave up?
Your start value does not match the condition, you need to check for smaller than the maximum value.
for (i = 0; i <= 100; i++) {
console.log(i, 'boop');
}
Because i is never greater than or equal to (>=) 100. Change the sign to <= and the loop will run.
The condition of the loop (i >= 100) isn't true even for the first iteration. The code within the loop will only execute while the condition is true.
Just remember that for is kind of a while..do loop
It validates the condition first, so, a for kind of says for (this variable, under this condition, that changes after each iteration this way) do
So, your condition is false from the beginning, it will never do anything.
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 6 years ago.
Improve this question
I'm creating a site and i needed to do some js. I'm not that good with it but tought i would figer it out. Not. I created a for loop but it does not run.
function order(user,product){
var index;
for(var i = 0; i<users.lenght; i++){
if(user == users[i]){
index = i;
break;
}
}
var budget = budgets[index];
alert(budget);
}
the creation of the users and budgets arrays are done with php and after checking with alert() it was how it should be.
Can anyone help me please?
lenght is spelt length. The misspelt property does not exist, so it undefined, which is equivalent to 0.