【One topic per day】Highest and Lowest

前端题库,Javascript题集

Posted by Jerry on March 26, 2019

Highest and Lowest

In this little assignment you are given a string of space separated numbers, and have to return the highest and lowest number.

Example:

highAndLow("1 2 3 4 5");  // return "5 1"
highAndLow("1 2 -3 4 5"); // return "5 -3"
highAndLow("1 9 3 4 -5"); // return "9 -5"

Reply

//method 1:
highAndLow = str => {
    const arr = str.split(' ');
    return `${Math.max(...arr)} ${Math.min(...arr)}`
}

//method 2:
highAndLow = str => {
    const arr = str.split(' ').map(Number);
    return `${Math.max.apply(null,arr)} ${Math.min.apply(null,arr)}`
}

同Github,欢迎star