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]));
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 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++)
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)
}
}
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;
}
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;
}
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I've just been challenged with the following question by a friend:
var i, n = 20;
for(i = 0; i < n; i--) console.log("-");
Q) Change a single symbol (change,delete,add) to make the code print "-" 20 times.
I've reached the following answers:
var i, n = 20;
for(i = 0; -i < n; i--) console.log("-");
var i, n = 20;
for(i = 0; i < n; n--) console.log("-");
He tells me there are three answers, but I can only come up with two.
Any ideas? This is driving me mad.
for(i = 0; i + n; i--) console.log("-");
Make n MINUS 20
var i, n = -20;
for(i = 0; i < n; i--) console.log("-");