highAndLow()


#1
function highAndLow(numbers){
  let numberToArray = numbers.split(' ');
  let highNumber = numberToArray[0];
  let lowNumber = numberToArray[0];
  for (let i = 1; i < numberToArray.length; i++) {
    if (numberToArray[i] > highNumber) {
      highNumber = numberToArray[i]
    } 
    if (numberToArray[i] < lowNumber) {
      lowNumber = numberToArray[i]
    }
  }
  return `${highNumber} ${lowNumber}`;
}

here’s my code, and it sometimes works, but sometimes doesn’t. For example, say I wrote 'let numbers = “4 5 29 54 4 0 -214 542 -64 1 -3 6 -6”. It gives me ‘6, -214’.
What’s the problem?


#2

the problem is that your array contains string. If you want to compare the value of numbers, the numbers should be of data type integer.


#3

So I should put int() in front of numberToArray[i], highNumber, and lowNumber?


#4

i would simply use map() to create a new array with the elements as integer values, i think that is the shortest approach: