How come my loop doesn't break? [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 8 years ago.
Improve this question
This is a simplified version of my actual code. I would expect the outer loop to break after 7 loops and the inner one to break after five, but neither one does.
var f = 0;
for (var i = 0; 7; i++) {
console.log('I:', i)
f++
for (var x = f+1; 5; x++) {
console.log('X:', x)
}
}

5 is an expression that doesn't do anything. The loop needs a Boolean expression that will be true of false -- 5 is always true. You need to check if i < 5.
var f = 0;
for (var i = 0; i < 7; i++) {
console.log('I:', i)
f++
for (var x = f+1; i < 5; x++) {
console.log('X:', x)
}
}

Related

Create a function that takes a word and returns true if the word has two consecutive identical letters [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
// Create a function that takes a word and returns true if the word has two consecutive identical letters.
What am I doing wrong?
module.exports = (word) => {
for (let i = 0; i <= word.length; i++) {
for (let j = i + 1; j <= word.length; j++) {
if (word[j] == word[i]) {
return true;
}
} return false;
}
};
You can do this with only 1 loop.
function hasConsecutiveIdenticalLetters(word){
for (let i = 1; i < word.length; i++) {
if (word[i-1] === word[i]) {
return true;
}
}
return false;
}
You can also achieve this like below using some
const hasConsecutiveIdenticalLetters = (word: string) => (word).split('').some((letter, index) => letter === word[index + 1]);

Uncaught SyntaxError: missing ; after for-loop initializer, but I can't see where it's missing [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 just started learning javascript, and I want to make an online lcm hcf interactive teaching tool, but I keep getting this error pointed out below, and I can't see where it's missing
$("document").ready(function(){
function start() {
var firstNum = document.getElementById("first-num");
var secondNum = document.getElementById("second-num");
var primeList1 = [];
var primeList2 = [];
var primes = [];
var maxPrime = math.max(firstNum, secondNum) / 2 + 1;
**for (int num = 2; num < maxPrime; num++) {** <--- this line has the error
for (int i = 2; num < i; i++)
if (num % i == 0) {
break;
} else {
primes.add(num);
};
};
};
There is no int keyword in JavaScript.
You need to use var or let to declare and initialize your num and i variables
for (let num = 2; num < maxPrime; num++) {
for (let i = 2; num < i; i++)

Simple for loop is going infinite and I can't tell why [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 years ago.
Improve this question
for (var i = 10; i < 41; i+2) {
console.log(i);
}
I am learning basic javascript and when I ran this on chrome, it crahsed. I think the loop going infinite but I don't understand why.
But when I change i + 2 to i++, it works fine.
I am trying to print out even numbers between 10 and 40 that's why I changed i + 2 to i++.
Am I not allowed to make i increment by 2?
You never change the value of i. Your expression is not an assignment of a value to i.
You need an addition assignment +=
i += 2
for (var i = 10; i < 41; i += 2) {
console.log(i);
}
Try this:
for (var i = 10; i < 41; i = i+2) {
console.log(i);
}
or
for (var i = 10; i < 41; i +=2) {
console.log(i);
}
Just writing i+2 calculates the new value, but it doesn't store it back in the variable.
To increment i by 2, you need to write:
i = i + 2
or the shorthand:
i += 2
It works when you write i++ because that's short for
i = i + 1
You need to store the value back into the variable.
↓↓
for (var i = 10; i < 41; i=i+2) {
console.log(i);
}

For-loop is never entered [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 8 years ago.
Improve this question
I have a function meant to construct an array full of objects using a for-loop. However, my debugger jumps over the for-loop for some reason, and i don't know why. here is the function:
function objArrCon() { //object array constructor
var arr = [];
var len = 9;
for (var i = 2; i === len; i++) {
arr.push({
name: i,
count: 0
});
}
return arr;
}
This line
for (var i = 2; i === len; i++) {
Should be
for (var i = 2; i <= len; i++) {
change you for loop like this. this my helps you
function objArrCon() { //object array constructor
var arr = [];
var len = 9;
for (var i = 2; i <= len; i++) { // change this line
arr.push({
name: i,
count: 0
});
}
return arr;
}

Infinite For Loop - Javascript - Whats wrong? [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 8 years ago.
Improve this question
function sortArray(array) {
var tempArray = [];
var tempNum;
var tempPos;
for (var i = 0; i < array.length; i+= 1) {
if (i = 0) {
tempNum = parseInt(array[i]);
tempPos = 0;
}
else if (parseInt(array[i]) <= tempNum) {
tempNum = parseInt(array[i]);
tempPos = i;
}
console.log(i);
}
}
It's supposed to take an array and order it from least to greatest but didn't even get that far as the for loop becomes infinite. What did i do wrong?
You're setting i = 0 in the first if, which means you will never have i large enough to exit the for loop. You want to use == instead
ie:
if (i = 0) {
tempNum = parseInt(array[i]);
tempPos = 0;
}
should be
if (i == 0) {
tempNum = parseInt(array[i]);
tempPos = 0;
}

Categories