i want to create an empty array to fill it later [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
hi guys i tryed to create an empty array to fill it later. I post my code so you can help me. XD
"use strict"
var array = new array(6);
for(var i = 0; i <= 5; i++){
do {
var number = prompt("Put the element" + (i+1), 0);
}while(isNaN(number));
arr.push(number);
};
write.data(array);

It appears you may have copy and pasted some code without understanding what it is doing.
You are trying to push to an array, but you've declared your array as array, but trying to push to arr. Which is why it's not working.
var arr = new Array();
for(var i = 0; i <= 5; i++){
arr.push(i + 1);
}
console.log(arr);
alert(arr);
EDIT * You actually don't even need to declare the new Array(6), you can just use new Array() to push. However, if you would like to declare the size, you can do this instead.
var arr = new Array(6);
for(var i = 0; i <= 5; i++){
arr[i] = i + 1;
}
console.log(arr);
alert(arr);

You can also use this:
var arr = []; // create an empty object
for(var i = 0; i <= 5; i++){
arr.push(i + 1); // fill the object
}
console.log(arr);
alert(arr); // shows 1,2,3,4,5,6
This does what you are trying to achieve.

You can do it like
var a = new Array(6);
for(var i = 0;i < 6;i++){
a.push(i+1);
}
console.log(a);

Related

"X" is not defined - using "For loop" 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 1 year ago.
Improve this question
I'm currently learning javascript loop and right now i'm trying to make an exemple using the "For" loop but as i'm trying it keep telling me "longString is not defined".
the result i'm trying to have is to print 6 A like this "AAAAAA"
here is the code:
var longString = A;
for (var longString = A; longstring < 6; longString = longString + A) {
console.log(longString);
}
i'm sure the solution is simple but as a totally beginner i can't figure out how to solve it.
You have a typo in your break condition:
var longString = "";
for (longString = "A"; longstring < 6; longString = longString + "A") {
// Note the lowercase 's' ^
}
console.log(longString);
Also, if you want to get the length of a string, you can use string.length:
longString.length < 6;
Does this what you want?
var str = ""
for (i = 0; i < 6; i++) {
str += "A"
}
console.log(str);
Try this, the below code prints "AAAAAA" in one line.
var longString = 'A'
var str = ''
for (var i = 0; i<6; i+=1){
str = longString + str
}
console.log(str)

I am not getting proper output with nested for-loop 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 3 years ago.
Improve this question
I'm supposed to get the multiplication value of the multi-dimensional array. but I am getting '1' as output whatever values being changed in array.
function arrayMultiplyer(arr){
var multi = 1;
for(var i=0;i < arr.length; i++){
for(var j =0; j<arr[i];j++){
multi *= arr[i][j];
}
}
return multi;
}
var multi = arrayMultiplyer([[2,33],[33,2],[5,6,9]]);
console.log(multi);
You need to check arr[i].length in the j loop.
function arrayMultiplyer(arr){
var multi = 1;
for(var i=0;i < arr.length; i++){
for(var j =0; j<arr[i].length;j++){ // you need to check arr[i].length here
multi *= arr[i][j];
}
}
return multi;
}
var multi = arrayMultiplyer([[2,33],[33,2],[5,6,9]]);
console.log(multi);
You can simply do this in two lines.
function arrayMultiplyer(arr){
let flattenedArray = arr.flat();
return flattenedArray.reduce((x, y) => x * y);
}
var multi = arrayMultiplyer([[2,33],[33,2],[5,6,9]]);
console.log(multi);
An alternative solution using .reduce()
function arrayMultiplier(arr) {
return arr.reduce((tot,arr2) =>
arr2.reduce((subTot, n) =>
subTot * n
, tot)
, 1);
}

Javascript Question: Sum of all odd numbers to 100 [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
I am trying to find out the sum off all odd numbers from 1 to 100 using a for loop. Here is my code so far:
var sum = 0;
for (var i = 1; i <= 100; i++) {
if (i % 2 == 1);
sum += i;
}
console.log(sum);
I thought maybe using a while loop maybe the solution, but whenever I try it, I always get an infinite loop error. So I tried switching back to a for loop, but the answer does not come out right. I believe I am looping through each number instead of every odd number. Is my "if" condition not correct? Any help is appreciated.
Thank You
You have a ; just after your if statement, so the next line is not going to get executed.
Just removing the ; should work:
var sum = 0;
for (var i = 1; i <= 100; i++) {
if (i % 2 == 1)
sum += i;
}
console.log(sum);
use step 2
var sum = 0;
for (var i = 1; i < 100; i+=2) {
sum += i;
}
console.log(sum);

In JS how to print string in while loop multiple times? [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 9 years ago.
Improve this question
var loop = function(){
while(loop<3){
console.log("I'm looping!");
loop++;
}
};
loop();
I tried in this way... i need to print above message 3 times... how to do it ??
Your function definition is correct.
You could also self-invoke your function like this
var func = function() {
}(); // notice () right here, without another call like func();
What about loops, there are several. Take a look.
var times = 4;
for (var i = 0; i < times; i += 1) {
console.log('I\'m looping! #'+i);
}
// or
console.log("\n");
var o = 0;
while (o < times) {
o += 1;
console.log('I\'m looping! #'+o);
}
// or
console.log("\n");
var u = 0;
do
{
u += 1;
console.log('I\'m looping! #'+u);
}
while (u < times)
Change the variable name , is the same of your function
var loop = function(){
var l = 0
while(l<3){
console.log("I'm looping!");
l++;
}
};
loop();

why does this loop only work once [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 can't work out why this only loops through the array once in JavaScript. It should log the second nested array as well.
JSFiddle below and code below;
http://jsfiddle.net/HJfbT/
b = [["one", "is"],
["two", "is"]];
for (var i = 0; i < b.length; i++) {
for (var x = 0; x < b[x].length; x++) {
console.log(b[i][x]);
}
}
Because you have a typo:
// --- should be i ---v
for (var x = 0; x < b[x].length; x++) {
DEMO: http://jsfiddle.net/HJfbT/1/
Use b[i].length in the second loop.
I think is because the inner loop have the running condition with an error.
Is:
for (var x = 0; x < b[x].length; x++)
but must be:
for (var x = 0; x < b[i].length; x++)

Categories

Resources