I am struggling to workout how this code is working, how is the maxNum part working? maxNum[0] < numbers[i] how is it grabbing the last number from the array?
function sortThem(numbers) {
let minNum = numbers[0]
let maxNum = numbers[0]
for (let i = 0; i < numbers.length; i ) {
if (minNum > numbers[i]) {
minNum = numbers[i]
} else if (maxNum < numbers[i]) {
maxNum = numbers[i]
}
}
console.log(maxNum)
const minMax = [minNum, maxNum]
return minMax
}
const results = sortThem([2, 9, 10, 17, 45])
console.log(results)
CodePudding user response:
Both start at 2 in this example
let minNum = numbers[0]//equals first element in the numbers array and thats 2 in this example
let maxNum = numbers[0]// equals 2 as well
Then you start to iterate thru the numbers array
Lets cover how you get your lowest number first:
if(minNum > numbers[i]){
1st loop
minNum = 2 not greater than 2, nothing happens
2nd loop
minNum = 2 not greater than 9, nothing happens
3rd loop
minNum = 2 not greater than 10, nothing happens
4th loop
minNum = 2 not greater than 17, nothing happens
last loop
minNum = 2 not greater than 45, nothing happens
minNum = never changes during the entire iteration becuz no number lesser than 2 was found so it keeps the initial value
Now for the maxNum:
} else if (maxNum < numbers[i]){
1st loop
maxNum= 2, numbers[i]=2, numbers[i] not greater than maxNum, nothing happens
2nd loop
maxNum= 2, numbers[i]=9, numbers[i] is greater than maxNum, maxNum=9 now
3rd loop
maxNum= 9, numbers[i]=10, numbers[i] is greater than maxNum, maxNum=10 now
4th loop
maxNum= 10, numbers[i]=17, numbers[i] is greater than maxNum, maxNum=17 now
last loop
maxNum= 17, numbers[i]=45, numbers[i] is greater than maxNum, maxNum=45 now
maxNum will overwrite itself as long as you can find a number that's higher than the value previously stored. Hopefully that explains what you don't understand
CodePudding user response:
Given to a context I dont fully get I would estimate that you want to know how the comparison works.
function sortThem(numbers) {
let minNum = numbers[0]
let maxNum = numbers[0]
for (let i = 0; i < numbers.length; i ) { // you can start loop at index 1 since you have set default val to 0
if (minNum > numbers[i]) { // if my current number in array is smaller than the most recent minNum
minNum = numbers[i] // set minNum to current number Exit out of if/else structure since else will not be triggered
} else if (maxNum < numbers[i]) { // if currentNumber is greater than given maxNum
maxNum = numbers[i] //set maxnum to current number
}
}
console.log(maxNum)
const minMax = [minNum, maxNum] //useless
return minMax // return [minNum, maxNum];
}
const results = sortThem([2, 9, 10, 17, 45])
console.log(results)
No Idea if it answered your question, but I think thats the explanation of this function.
CodePudding user response:
You define minNum
and maxNum
, which start with being just some value from the array (in this case index 0 / the first one, which is 2).
for (let i = 0; i < numbers.length; i ) { /* ...*/ }
means everything inside {} will be repeated the same amount of times that the array has indexes, and every iteration i
will be increased by one (starting with 0, so it will loop over all indexes of numbers array)
so numbers[i]
gets value at i
th position (first time it will be 2
, second time 9
...)
The if conditions check whether the current minNum
/maxNum
is smaller/greater than value at i
th index of numbers array, and if it is, it sets minNum
/maxNum
to that value, so it is increased / decreased.
Basically the code goes trough every value in the array you defined, and everytime it finds bigger value, it sets it as value for minNum
/maxNum
.
In this case minNum
is set to 2, and because it is the smallest of all, it stays 2.
maxNum
starts at 2, is increased to 9, 10, 17 and then to 45.
CodePudding user response:
Although, its an easy implementation. It'll be better if you dry run the loop using a pen and paper. I'll write the algo you have used so that it becomes easier for you.
- You take an array as an input.
- You initialize 2 vars (namely minNum and maxNum) to the value of the First element from the array
- Run a loop over the array, to check and update the values of minNum and maxNum.
- After the loop ends, you return those numbers in an array. That's it!