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;
}
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 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]);
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 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]));
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
When I try to execute this it takes index from [1] position but not zero. How to take from zero.
function mul(){
var temp = 1;
for (i=1 ; i < arguments.length; i++){
temp = temp * arguments[i];
}
return temp;
}
var list = mul(2,2,2,2,2);
alert(list);
This is because var i is set to 1. arguments is an array-like object and it also start from 0 index. In your case your are starting from 1st index.
function mul() {
var temp = 1;
for (i = 0; i < arguments.length; i++) {
temp = temp * arguments[i];
}
return temp;
}
var list = mul(2, 2, 2, 2, 2);
console.log(list);
According to airbnb style it is better to use rest syntax instead of arguments object. Beside i in the for loop has not defined with var keyword. In that case it will be global variable & will be accessible from other function. So defining a variable with var keyword will bound its scope to current function only. You can also explore let
function mul(...args) {
var temp = 1;
for (var i = 0; i < args.length; i++) {
temp = temp * args[i];
}
return temp;
}
var list = mul(2, 2, 2, 2, 2);
console.log(list);
The line 3 for loop:
for (i=0 ; i < arguments.length; 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
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;
}