Well, I like to not just memorize blocks of code, but to understand them, so that I can create them myself. Why does this work? I don't understand. If someone can explain it to me, I'd be most grateful. To me it looks like: "If the array index (position in the array) is greater than 0, 0 = the array index," and how that would magically tell me the highest number in the array I haven't a single clue. I've tried searching for an answer on this but without luck. Here's an example of the code, thanks in advance:
var array = [3, 4, 5, 21.15, 21, 9];
var largest = 0;
for (i = 0; i < array.length; i++) {
if (array[i] > largest) {
largest = array[i];
}
}
console.log(largest);
Well, I like to not just memorize blocks of code, but to understand them, so that I can create them myself. Why does this work? I don't understand. If someone can explain it to me, I'd be most grateful. To me it looks like: "If the array index (position in the array) is greater than 0, 0 = the array index," and how that would magically tell me the highest number in the array I haven't a single clue. I've tried searching for an answer on this but without luck. Here's an example of the code, thanks in advance:
var array = [3, 4, 5, 21.15, 21, 9];
var largest = 0;
for (i = 0; i < array.length; i++) {
if (array[i] > largest) {
largest = array[i];
}
}
console.log(largest);
Share
Improve this question
edited Oct 31, 2012 at 21:27
Salman Arshad
272k84 gold badges442 silver badges533 bronze badges
asked Oct 31, 2012 at 21:19
Miles BardanMiles Bardan
5233 gold badges8 silver badges17 bronze badges
2
|
6 Answers
Reset to default 6The line largest = array[i];
does NOT mean 0 = array[i]
as you believe. largest
is a variable, so each time you get to the line largest = array[i];
you are changing the value of largest
to be the current array[i]
.
This is why you get the max of the array at the end.
An example: a = [1, 3, 7, 2]
You initialize largest = 0
. The, for each element of the array you do the following:
largest < 1? yes, so largest = 1
largest < 3? yes, so largest = 3
largest < 7? yes, so largest = 7
largest < 2? no, so do nothing
var array = [3, 4, 5, 21.15, 21, 9];
var largest = 0;
for (i=0; i<array.length; i++) {
if (array[i]>largest) {
largest = array[i];
}
}
console.log(largest);
Iteration 1:
Check if array[0] i.e. 3 is greater than 0 => True : largest = 3
Iteration 2:
Check if array[1] i.e. 4 is greater than 3 => True : largest = 4
Iteration 3:
Check if array[2] i.e. 5 is greater than 4 => True : largest = 5
Iteration 4:
Check if array[3] i.e. 21.25 is greater than 5 => True : largest = 21.25
Iteration 5:
Check if array[4] i.e. 21 is greater than 21.25 => False: largest = 21.25
Iteration 6:
Check if array[5] i.e. 9 is greater than 21.25 => False: largest = 21.25
largest = array[i]; //Assign whatever is at the position 'i' in the array
//to the variable 'largest'
if (array[i]>largest) // See if the value at the position 'i' in the array
//is greater than what is contained in the variable 'largest'
Add a another console.log
statement and it will be clearer.
var array = [3, 4, 5, 21.15, 21, 9];
var largest = 0;
for (i=0; i<array.length; i++) {
if (array[i]>largest) {
console.log(largest + " " + array[i]);
largest = array[i];
}
}
console.log(largest);
Which gives the output
0 3
3 4
4 5
5 21.15
21.15
If array[i]
is greater than the current value of largest
, the value of largest
is overwritten by array[i]
. It loops through the array, making the comparison each time.
how that would magically tell me the highest number in the array
It won't. Because of
var largest = 0;
if the largest value in the array is negative, then it will spuriously report 0
as the largest value. For example, if var array = [-3, -4, -5, -21.15, -21, -9];
then largest
will be 0
at the end of the loop, not -3
.
The below should do better, and will return undefined
if there are no values that are meaningfully comparable to numbers in the array.
var greatestIndex = -1;
for (var i = 0, n = array.length; i < n; ++i) {
if (greatestIndex >= 0 ? array[i] > array[greatestIndex] : array[i] >= -Infinity) {
greatestIndex = i;
}
}
return greatestIndex >= 0 ? array[greatestIndex] : undefined;
If you set a variable "maxNumber" to be initially equal to the lowest possible number and run a "for loop" where you check if every next number in the string is higher than the initial value and then overright the current value of the input over the initial value it will eventually run through the array by checking every current number with the previous one and log the highest number.
function solve(input) {
let count = +input.shift();
let maxNumber = Number.MIN_SAFE_INTEGER;
for (let i = 0; i <= count; i++) {
let number = +input.shift();
if (number > minNumber) {
mmaxNumber = number;
}
}
console.log(maxNumber);
}
array[i]
returns the value at the positioni
in the array. So it compares the number at thei
position with the current largest number. Once the array is all over, the largest number is in the variable largest. – Hugo Dozois Commented Oct 31, 2012 at 21:31Math.max(...array)
it's a lot less code and it is a lot easier to understand to learn more about how to use the...
search up Spread Syntax. – user6913790 Commented Sep 8, 2019 at 10:12