Practice Exercise #36

Hello,

By testing the code given in the solutions the console in web browser returns Uncaught SyntaxError: Unexpected token ‘)’. By looking at the code the unwanted ´)´ token can be found in the code given in the solution c) in the second line “for (num in list)) {”

Also i experimented with the code a little bit further and changed the values that should be given to a list, to see if the code works with anygiven list of numbers. It turns out that the code doesnt perform well when it is given a different set of values in variable list. For example, when the list is described as: const list = [120, 4, 9, 7, 2, 8, 3, 1, 1100]; The returned values in the console for the smallest value is 2 and for the largest value is 120. I dont know if this is rellevant, but just wanted to share my findings with you.

Code, that is working with lists containing anygiven values looks like this:

const listValues = [120, 4, 9, 7, 2, 8, 3, 1, 1100]
var  lowestValue = listValues[0]
var  highestValue = listValues[0]

for (num in listValues){
      if (listValues[num] < lowestValue){
        lowestValue = listValues[num]
      }

}
console.log(lowestValue)

for (num in listValues){
    if (listValues[num] > highestValue){
      highestValue = listValues[num]
    }

}
console.log(highestValue)

Best,
Peter

Hi @peros369,
There is one small change we need to make to fix the issue. The array index is num which is outputting as a string. i.e. it returns "2" instead of 2.

       const listValues = [120, 4, 9, 7, 2, 8, 3, 1, 1100];
        var lowestValue = listValues[0];
        var highestValue = listValues[0];

        for (num in listValues) {
            num = Number.parseInt(num) //the num variable comes out as a string , we need to change it to integer for properly indexing the array below
            if (listValues[num] < lowestValue) {
                lowestValue = listValues[num]
            }
        }
        console.log(lowestValue)

        for (num in listValues) {
            num = Number.parseInt(num)//the num variable comes out as a string , we need to change it to integer for properly indexing the array below
            if (listValues[num] > highestValue) {
                highestValue = listValues[num]
            }

        }
        console.log(highestValue)

Hope this helps.

Happy Learning!

1 Like

Thanks for pointing that out, i didnt know that the num value is recorded as a string, but somehow Javascript doesnt seem to care if the value is given as a string or number, as the results turn out as a number.

Could you please explain what is the difference between num = parseInt(num) and num = Number.parseInt(num) ?

Thanks,
Peter

Hi @peros369,

Both of them are the same. One is defined globally and the other is part of the Number object.
You can find more details here – https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/parseInt

Happy Learning! :smiley:

2 Likes

Thank you for your guidance,

Best,
Peter