link🔗: https://codepen.io/someone_49/pen/PoJgQrJ?editors=1010
Can I have an detailed explanation of what sort()
does, in variables (small & large), What do (a, b) refer to?
And thanks in advance🌹
link🔗: https://codepen.io/someone_49/pen/PoJgQrJ?editors=1010
Can I have an detailed explanation of what sort()
does, in variables (small & large), What do (a, b) refer to?
And thanks in advance🌹
Hello @albaraa_1949
function printNums(userInput) {
let arr = userInput.split("-"); // this will return an array from the string userInput by split it by - so for 10-20 it will return an array of 2 element 10 and 20
let small = arr.sort((a, b) => a - b)[0].trim();
arr.sort((a, b) => a - b) will sort that array by the arrow function
(a,b)=>a-b which mean in function style
function (a,b){
return a-b;
}
it check if a-b > 0 then b come before a in the order
a-b <0 then a come before b in the order
a-b = 0 then both a = b
then we use the first item of that array by using [0] and trim it
let large = arr.sort((a, b) => b - a)[0].trim(); // this the reverse order of the previous one
for (let i = small; i <= large; i++) {
console.log(Number(i));
}
}
check this for extra details about sort function
hope that help and have a nice day
This makes it easy, thanks
you welcome and glad to hear that