JavaScript For loop ignored [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
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.

Related

My for loop runs only once while trying to reverse an array 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 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);

Fatal error occurring in Javascript forming 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 1 year ago.
Improve this question
I want to make a function that forms an array based on the user input so I write the javascript as below but it only returns a fatal error. What is wrong with this code? I try to match with the book's code but I don't find anything particularly different so I came to the StackOverflow. The code is as follows
function arrayForm(start, limit)
{
let array = [];
for (start <= limit; start++;)
{
array.push(start);
}
return array;
}
console.log(arrayForm(1,10));
try
{
let array = [];
for (let i = start; i <= limit; i++)
{
array.push(i);
}
return array;
}
console.log(arrayForm(1,10));

Why does a for loop behave oddly in a JavaScript constructor? [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 new to constructors - I'm trying to use one to create a customizable object, with this sort of code:
class test{
constructor(range) {
var start;
if(range==="a"){
start = 56;
}
else if(range==="b"){
start = 53;
}
for(var i=start; i<(start+5); i++); {
console.log(i);
//construct an array here
}
}
}
const myTest = new test("a");
But only the last loop seems to execute!
The log shows just the value 61.
You have an semicolon to early. The result is an empty statement and an additional block statement outside of the loop.
Finally you get the last value of i.
for (var i = start; i < (start + 5); i++); {
// ^

Store values inside array jquery [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 3 years ago.
Improve this question
I have multiple inputs, for example:
<input id=xxx_1>
<input id=xxx_2>
I want the values from input to be stored inside an array. At the moment what I have is this but it doesn't work. I have tried to Google it and I can't find any solution. I can't understand why this does not work.
var array = [];
for (var i = 1; i >= 14; i++) {
array[i] = $(this).find("#xxx_" + i + "").val();
}
What I what is to have an array like this:
[ value, value, value,... ]
Thanks for the help
You could make use of attributs starts with selector to select all elements who id starts with xxx, then you can iterate over these elements using .each()
var array = [];
$( "[id^='xxx']" ).each(function() {
array.push(this.value);
});
console.log(array);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="xxx_1" value="123">
<input id="xxx_2" value="456">
Your comparison operator in your for loop is the wrong way round, and so your code never enters it.
You probably want to look for when i is less than or equal to 14, using <= .
var array = [];
for (var i = 1; i <= 14; i++) {
array[i] = $(this).find("#xxx_" + i + "").val();
}

Create a JavaScript function writing odd numbers between 0 and 15001 [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 5 years ago.
Improve this question
I am trying to improve my ability to create efficient algorithms everyday and I am facing an issue with this one... I want to create a JavaScript function loop to write only odd numbers between 0 and 15000:
function Nowork() {
for(x = 1; x < 15001; x+2) {
document.write(x);
}
}
Nowork();
This one doesn't work (also I know document.write should be written only for testing and debugging), instead I know that one works but it only write the even numbers:
function Works() {
for(x = 1; x < 15001; x++) {
document.write(x);
}
}
Works();
Does anyone have an idea how to do that and also explain to me why my first function doesn't work?
use this:
function Nowork() {
for(x = 1; x < 15001; x=x+2) {
document.write(x);
}
}
Nowork();
you cant use +2 like that.

Categories

Resources