Javascript Question: Sum of all odd numbers to 100 [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 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);

Related

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

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

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

find missing element in a javascript object array? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
how would one write a function to find the missing element of an numeric array for example:
getMissingElement( [0, 5, 1, 3, 2, 9, 7, 6, 4] ) // returns 8
Is there just one element missing, and the others are for sure non-repeating?
Then recall that the formula to compute the sum of 0 + 1 + ... + (N-1) is (N-1)*N/2, and the difference from this to the sum in your array is the missing element:
function getMissingElement(array) {
var sum = 0;
var N = array.length + 1;
for(i = 0; i < N-1; ++i) {
sum += array[i];
}
return (N-1)*N/2 - sum;
}
function getMissingElement(myArray) {
myArray.sort();
myAray.reverse()
for(var i = 1; i < myArray.length; i++) {
if(myArray[i] - myArray[i-1] != 1) {
//log your numbers or print them or whatever you like
}
}
}
This is assuming the most basic definition of "missing item", where the missing item is between existing values on either side.
Here's way to do it:
arr.sort(function(x, y){return x - y})
.map(function(x, i, me){return me[i+1]-x > 1 && x+1})
.filter(Number)
This will give you an array with missing numbers, for [0,2,4] it will give you [1,3]

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