1

I am working on the following problem:

Write a function with an object parameter such that it returns an array of the smallest and largest integer.

The input is: { 2: 'a', 7: 'b', 1: 'c', 10: 'd', 4: 'e' }

Here is my solution:

var minMaxKeyInObject = function(obj) {
  let sortedStrArr = Object.keys(obj);
  let sortedNumArr = [];
  sortedStrArr.forEach(i => sortedNumArr.push(parseInt(i)));
  return [sortedNumArr[0], sortedNumArr[sortedNumArr.length - 1]]

}

Though, it worked! I am not sure why strings (integer) are sorted. Any help is much appreciated!

Thank you!

}

Eric Lin
  • 11
  • 2

0 Answers0