Why doesn't variable update in for loop JavaScript? [closed] - javascript

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.

Related

Push a random character from an array into another array [closed]

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 months ago.
Improve this question
var legalCharactersForSaveCode = ["B","C","D","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z", "+", "/",];
var saveCode = [];
function generateSaveCode() {
for(var c = 0; c > 30; c++){
v = Math.round(Math.random() * legalCharactersForSaveCode.length);
saveCode.push(legalCharactersForSaveCode[v]);
}
saveCode.push(store.get("money"));
alert(saveCode);
}
I'm trying to make it so it pushes a random character.
When I ask it to alert the saveCode array all it does is alert the money.
(I'm using store.js)
I was expecting for it to push a random characters from the legalCharacters array into the saveCode array.
Your "for" loop does not work as the condition is c > 30, and should be c < 30

Why is my answer in the compiler not the same, with the answer from leetcode compiler? [closed]

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)];

Number prediction from 1 to 10 from javascript [closed]

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") };

Issues with displaying certain array elements with a conditional in javascript [closed]

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]);

javascript for loop does not work [closed]

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.

Categories