-2

im trying to Find the Smallest and Biggest Numbers. i can do this in for > let. but when i try to put this in a function it does not work.

const myNumbers = [1111, 245, 535 ,222, 221,12,233444];

function findnum(){
for (let i=0; i < myNumbers.length ; i++ ){
    const smallNum = Math.min(...myNumbers)
    const bigNum = Math.max(...myNumbers)
    break
}
}

result = findnum(smallNum,bigNum)
console.log(result)
Pedro
  • 3
  • 1
  • 5
    You're calling your function with variables that don't exist, you aren't returning from the function, and your loop is completely unnecessary. Perhaps clarify what it is you're trying to achieve? – pilchard Nov 06 '22 at 23:32
  • Does this answer your question? [How might I find the largest number contained in a JavaScript array?](https://stackoverflow.com/questions/1379553/how-might-i-find-the-largest-number-contained-in-a-javascript-array) – Mohamed EL-Gendy Nov 06 '22 at 23:33

1 Answers1

0

If you pass two variables to a function like this result = findnum(smallNum, bigNum), then you should have the function's signature with two arguments, like this: function findnum(smallNum, bigNum){

Pay attention that smallNum and bigNum only exist inside the function. As @pilchard mentioned, in your code you don't need arguments, the loop is unnecessary, and the function needs a return. This should work.

const myNumbers = [1111, 245, 535 ,222, 221, 12, 233444];

function findnum(){
    const smallNum = Math.min(...myNumbers)
    const bigNum = Math.max(...myNumbers)
    return [smallNum, bigNum]
}

result = findnum()
console.log(result)
Pablo
  • 643
  • 9
  • 12