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
My code is not working so I went to the Chrome console to try and fix it and now that is just returning undefined when I try and run my while loop.
This is the code I entered into the console:
let total = 0;
let playerTotal = 0;
while(total >= 10){
if (total >= 10){
if (total > playerTotal){
console.log('total wins')
} else if(total == playerTotal){
console.log('tie')
} else {
console.log('player wins')
}
}
total += 1
}
Your condition is while (total >= 10). total starts out at 0. 0 is not >= 10 and so your loop's body is never executed.
You're seeing undefined because when you enter code in the console, the final result of that code is displayed. The result of a while loop is the value of the last statement evaluated within it (weird but true); a loop that never executes its body therefore results in undefined..
while(total >= 10){
should be changed to
while(total <= 10){
Thats logic issue which trigger infinite loop because total number would not increment since total is 0 and condition of loop only start when total >= 10.
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 last year.
Improve this question
var amount = Number(window.prompt("number of elements"));
var list = []
for (var i = 0; i < amount; i++); {
list.push(window.prompt("enter elements of the list"));
}
list.reverse();
document.write(list);
I tried to reverse a list but whatever amount I enter my code only runs trough for loop once, asks me for element of the list and ends the for loop and prints out my one and only entry since the for loop doesn't ask me for elements multiple times. I'm a beginner it's a silly mistake probably but I just can't figure it out.
As #Barmar pointed out the problem is due to a logic error.
/* Read the item from the prompt until you and write it to the list container. */
function read(list, size){
for(let i = 0 ; i < size ; ++i)
list.push(window.prompt());
}
/* Print a list container */
function print(list){
for(let i = 0 ; i < list.length ; ++i)
console.log(list[i]);
}
var list = []
read(list, Number(window.prompt("number of elements")));
list.reverse();
print(list);
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 trying to solve challenge on one website, and my code behaves strange... It returns undefined... What is interesting: if I print value of "sum" just before return statement, it is correct value in the console... Any help would be awesome
function digital_root(n) {
let text = String(n);
let sum = 0;
for (let i = 0; i < text.length; i++) {
sum += Number(text[i]);
}
if (String(sum).length > 1) {
digital_root(sum);
} else {
return sum;
}
}
let score = digital_root(456);
Make sure to propagate the recursive return value upwards. If you call digital_root() without returning the child's result then the parent call returns undefined.
if (String(sum).length > 1) {
return digital_root(sum);
}
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
Im trying to make a little program in js that overturns the text you typed in. The code runs fine, but the for loop gets completely ignored, it doesnt do even one block of code from inside of it.
document.getElementById("StartButtonText").addEventListener("click",function(){
var text_value = document.getElementById('TextValue').value;
console.log(text_value);
var text_length = text_value.length;
console.log(text_length);
var final_text;
var order;
for (order = 1; order >= text_length; order ++) {
final_text[order] = text_value[text_length - order + 1];
console.log('for loop log ',order);
final_text = final_text + final_text[poradie];
}
console.log('after loop log ',order);
document.getElementById("TextTurningResult").innerHTML = final_text;
console.log(final_text);
});
Any ideas why it doesnt run?
Your loop will run as long as order is greater or equals than text_length.
I guess you wanted to write:
for (order = 0; order < text_length; order ++) { ... }
You should change the condition in your for loop to
for(order = 1; order <= text_length; order++){
...
}
In your case, the condition is false at the first iteration only.
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 3 years ago.
Improve this question
I am learning for loops in javascript and trying some things I wrote the following basic code.
This code generates an infinite loop that only prints the number 4 indefinitely in the console.
The problem is that i don't understand the logic behind this and the exact reason why it happens
for(let i = 0; i = 4; i++){
console.log(i);
}
In your loop, the condition part uses an assignment operator =, you should use a comparison operator such as !==. Note also that the condition should be false in order to exit the for loop. So !== it is (or <=, which I dislike for personal reasons), instead of ===.
for(let i = 0; i !== 4; i++){
console.log(i);
}
So your code runs indefinitely because i = 4 is truthy. And it prints 4 because i = 4 assigns 4 to i after increment
i = 4 is an expression which would return 4 and 4 is trucy value so the condition never gets false and the loop never ends
console.log(i=4); //4
console.log(Boolean(i=4)); //true
You should use less than operator
for(let i = 0; i <= 4; i++){
console.log(i);
}
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 5 years ago.
Improve this question
I have a carousel with chevrons and pagination. When the carousel left chevron is clicked, a var counter is set to --, and when the right chevron is clicked, the counter is set to ++.
I can't tie the pagination to the chevron animation as different events occur.
I have the below code for my pagination. When all events have completed, I set the following:
if (pagCount === 1) {
counter = 0;
} else if (pagCount === 2) {
counter = -1;
} else if (pagCount === 3) {
counter = -2;
} else if (pagCount === 4) {
counter = -3;
} else if (pagCount === 5) {
counter = -4;
}
so that if the user clicks on either chevron, they will be in the correct position in the slides already.
Is there a way to make this dynamic? At the moment I need to know the number of slides (5 currently) and must update this manually as more slides are added.
EDIT: Joschi's comment below resolved this issue.
You can do something like this
counter = -(pagCount - 1)
So if pagCount = 10
then -(pagCount - 1) = -9
#Joschi helped with resolving this, the solution was to replace my if statements with counter = 1 - pagCount;