Infinite For Loop - Javascript - Whats wrong? [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
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;
}

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++)

Need to find out the average of an 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 2 years ago.
Improve this question
function average(arr) {
for (i = 0; i < arr.length; i++) {
var total = 0;
total += arr[i]
}
return total / arr.length;
}
console.log(average([74, 55, 6, 35]));
It gives back the answer 8.75?
i cant see what i'm doing wrong here.
Each time you iterate you are reassigning total to 0. Move it outside of the loop.
function average(arr) {
var total = 0;
for (i = 0; i < arr.length; i++) {
total += arr[i]
}
return total / arr.length;
}
Try this
const average = (arr) => {
let total = 0;
for (i = 0; i < arr.length; i++) {
total += arr[i]
}
return total / arr.length;
}
console.log(average([74, 55, 6, 35]));

How come my loop doesn't break? [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
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)
}
}

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

Categories