0

Can anyone give me some thoughts as to why number1 is being logged as 0 even though it should be logged as 1(or at least I think so)? I'm new to Javascript.

var array = [3,1,2];
var array2 = []
var number1 = 0; 
var number2 = 0;
var number3 = 0;
for(var i = 0; i < array.length; i++) {
  if(array[i] > number3) {
   number3 = array[i]

  }
  else if(array[i] > number2) {
    number2 = array[i];
  }
  else if (array[i] > number1) {
     number1 = array[i];
  }
}
console.log(number1)
console.log(number2)
console.log(number3)
5
  • 3
    Pretty simple - CTRL-F for number1 = . It's only assigned once - as 0. Seems like a basic mistake - I think you have the third else the wrong way around. Commented Jan 23, 2017 at 19:57
  • 1
    I don't see anywhere that you reassign or increment number1. Commented Jan 23, 2017 at 19:57
  • number1 is assigned 0, and that's it. It's never assigned to again. Commented Jan 23, 2017 at 19:57
  • In programming, a = x is a <- x in pseudo-code. It's not a commutative operation as it is in Mathematics. So, number1 has its default value because you don't change it. Commented Jan 23, 2017 at 19:58
  • Use debug tools and start getting used to it, is my advice to you Commented Jan 23, 2017 at 19:58

8 Answers 8

4

In this code, the only assignment to number1 is the line that declares it, which assigns the value 0. Did you perhaps mean for this line:

array[i] = number1;

...to read:

number1 = array[i];

?

Sign up to request clarification or add additional context in comments.

2 Comments

See the comment by @TyAnthoney -- you only have 3 numbers in the array, and the way the loop is structured, it will write both the second and third numbers to number2.
I'm going to go out on a limb here and guess that you're looking to identify the largest 3 numbers in the array. Ignore this if that isn't the case, but by far the simplest and most intuitive way to do this is going to be to sort the entire array and then take the last 3 values: array.sort(); var largest3 = array.slice(-3);
1

The problem is the logic of your statements and the fact that you are performing the incorrect assignment. This is what is happening

First iteration: 3 > 0 so number3 = 3 Second iteration: 1 !> 3 so we move to the second conditional where 1 > 0 so number2 = 1 third iteration: 2 !> 3 and 2 > 1 so number2 = 2 then the loop is done

Once you fix the boolean login assigning array[index] to number1 will work

Comments

1

The code appears to be looking for the three highest, positive, different values in an an array of numbers. If so it suffers a logic flaw that the value overwritten in an existing highest value could be a candidate for the next highest value but does not get checked. Code that produces the kind of result apparently expected needs to perform such a check, as for example:

var array = [3,1,2];
var array2 = []
var number1 = 0; 
var number2 = 0;
var number3 = 0;
var number, temp;

for(var i = 0; i < array.length; i++) {
  number = array[i];
  if( number > number3) { // swap number with number3
    temp = number3;
    number3 = number;
    number = temp;
  }
  if( number > number2) { //swap number with number2
    temp = number2;
    number2 = number;
    number = temp;
  }
  if( number > number1) {
    number1 = number;
  }
}
console.log(number1)
console.log(number2)
console.log(number3)

If the three highest but not necessarily different values need to be found, change the > tests to >= versions.

Comments

1

I SEE YOU FIXED THE "array[i] = number1;" to be the right way. If still having trouble understanding issues i sometimes put in crude debugging using "alerts" - see bottom of my post .

Looks to me like you simply set it to 0 in line 3 (var number1 = 0; ) and never set it to anything else.

lets go loop by loop.

loop 1:

if(array[i] > number3) {
 number3 = array[i]
}

number3 = 0 on first loop and array[0] = 3, so value of array[0] will be assigned to number3, making number3 = 3

loop 2: will now skip to this else if

else if(array[i] > number2) {
 number2 = array[i];
}

number2 = 0 on second loop and array[1] = 1, so value of array[1] will be assigned to number2, making number2 = 1

loop 3: will also hit the first else if since number2 = 1 and array[i] = 2 now number2 will be assigned 2

else if(array[i] > number2) {
 number2 = array[i];
}

now your looping is done then with out hitting the last else if. so number1 still equals 0 besides that you are doing array[i] = number1; when you should do something like number1 = array[i] instead

else if (array[i] > number1) {
 array[i] = number1;
}

at least this is what it looks like it's doing to me.

Sample alerts to help debug.

<script>
var array = [3,1,2];
var array2 = []
var number1 = 0; 
var number2 = 0;
var number3 = 0;
for(var i = 0; i < array.length; i++) {

alert( 'array['+i+']='+array[i] + ', number3 ='+number3 + ',  number2 ='+number2 + ',  number1 ='+number1) 

  if(array[i] > number3) {
   number3 = array[i];
   alert (' got here A');

  }
  else if(array[i] > number2) {
    number2 = array[i];
    alert (' got here B');
  }
  else if (array[i] > number1) {
     number1 = array[i];
     alert (' got here C');
  }
}
console.log(number1)
console.log(number2)
console.log(number3)
</script>

hope that helps.

1 Comment

In summary, the data in he array dictates that number2 is set twice: 1 is greater than 0 sets number2, and 2 is greater than 1 sets number2 as well. number1 never gets looked at or set.
0

you have set array[i] = number1, so it will stay as 0. If you set number1 = array[i] it will set as 2

Comments

0

1) Your

else if (array[i] > number1) {
     array[i] = number1;
}

never is called. If you try to debug step by step, you will see the condition is not met.

2) you have your code inverted.

array[i] = number1;

Comments

0

number1 value will be 1 only if array = [3, 2, 1] and not array = [3, 1, 2].

Note that if array = [3, 1, 2], the value of number2 is 1 in second iteration of the for loop and 3 in third iteration. So the number3 value never changes as the control never goes to the 3rd if statement.

for loop breakdown with array = [3, 1, 2]

Iteration 1:
number1 = 3
number2 = 0
number3 = 0

Iteration 2:
number1 = 3
number2 = 1
number3 = 0

Iteration 3:
number1 = 3
number2 = 2
number3 = 0

FYI:

var array = [3, 2, 1];  // old value: [3, 1, 2]
var array2 = []
var number1 = 0;
var number2 = 0;
var number3 = 0;

for (var i = 0; i < array.length; i++) {
  if (array[i] > number3) {
    number3 = array[i]; // gets value 3

  } else if (array[i] > number2) {
    number2 = array[i]; // gets value 1 if array = [3, 1, 2] and later value 2
  } else if (array[i] > number1) { // condition never satisfied if array = [3, 1, 2]
    number1 = array[i];
  }
}

console.log(number1)
console.log(number2)
console.log(number3)

Comments

0

This loop doesn't work because a loop repeat everything inside the loop the number2 just got overitten.

Just do:

var array = [3,1,2]
var array2 = array.sort();

Comments

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.