1

I am getting a wrong ouput, i am storing a value greater than 400 in an array arr[] I want to store the values 450, 650 in my arr[] and my arr[] length should be 2(two) since there are two values greater than 400, but I am getting a wrong output.

array length value inside array

var total = [300, 350, 450, 650];
var arr = [];
for (var i = 0; i < total.length; i++) {
  if (parseInt(total[i]) >= 400) {
    arr[i] = total[i];
  }

}
alert(arr.length);
alert(arr);

2
  • Try pushing to the arr array instead of assigning via the arr[i] Commented Jan 11, 2017 at 10:14
  • Don't alert it, console.log it, it will show proper output Commented Jan 11, 2017 at 10:15

5 Answers 5

2

You are defining array elements by an index which leads to the array with some undefined values(undefined index within the larger index defined). Instead use Array#push to add array elements in the proper way.

arr.push(total[i]);
Sign up to request clarification or add additional context in comments.

5 Comments

Not completely true. What actually happens is that the length of an array is the index of the last element + 1. There won't be any undefined values in the array. The length property just won't be in sync with the actual number of elements in it
@devqon : are you sure ? even after looking into this statement if (parseInt(total[i]) >= 400) { arr[i] = total[i]; }
Yes, if you console.log the incorrect arr from OP, you can see it contains 2 elements (450 and 650). No undefined values, only missing elements and a length proprety that is not in sync. See this jsfiddle
@devqon : what about that 2 , at beginning :)
@devqon : it's showing as an object like structure in console since index 0 n 1 is not defined....
2

You add the total value at the index "i" to the arr index "i". But the arr has not the same index as total.

so you need to do this:

var total = [300, 350, 450, 650];
var arr = [];
for (var i = 0; i < total.length; i++) {
  if (parseInt(total[i]) >= 400) {
    arr.push(total[i]);
  }

}
alert(arr.length);
alert(arr);

Comments

0

Your code is a bit wrong , I am correcting it:

var total = [300, 350, 450, 650];
var arr = [];
for (var i = 0; i < total.length; i++) {
  if (parseInt(total[i]) >= 400) {
    arr.push(total[i]);
  }

}
alert(arr.length);
alert(arr);

Comments

0

Use ECMA6, and it is a lot easier to read

var total = [300, 350, 450, 650];
var arr = total.filter(value => value >= 400);
console.log(arr.length);
console.log(arr);

and otherwise

var total = [300, 350, 450, 650];
var arr = [];
for (var i = 0; i < total.length; i++) {
  if (parseInt(total[i]) >= 400) {
    arr.push(total[i]);
  }

}
alert(arr.length);
alert(arr);

With this part of code arr[i] = total[i]; you add each time the value to the same position as the original position. The other values lower than 400 will not be added but will be mapped as empty values

Comments

0

You should have a counter to accomplish your task

var total = [300, 350, 450, 650];
var arr = [];
var counter=0;
for (var i = 0; i < total.length; i++) {
  if (parseInt(total[i]) >= 400) {
    arr.push(total[i]); //to save into an array
  }

}
   console.log(arr.length);

1 Comment

Quote from OP: " i want to store 450,650 value in my arr[]", so he does not just want the counter of those values

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.