In this little assignment you are given a string of space separated numbers, and have to return the highest and lowest number.
Notes
All numbers are valid Int32, no need to validate them.
There will always be at least one number in the input string.
Output string must be two numbers separated by a single space, and highest number is first.
Examples
highAndLow("1 9 3 4 -5"); // return "9 -5"
function highAndLow(numbers){
let res = numbers.split(' ').map(num => parseInt(num)).sort(function(a, b) { return a - b });
return `${res[res.length - 1].toString()} ${res[0]}`;
}
function highAndLow(numbers){
numbers = numbers.split(' ').map(Number);
return Math.max.apply(0, numbers) + ' ' + Math.min.apply(0, numbers);
}
function highAndLow(numbers){
numbers = numbers.split(' ');
return `${Math.max(...numbers)} ${Math.min(...numbers)}`;
}
array.sort([compareFunction])
If compareFunction is not supplied, all non-undefined array elements are sorted by converting them to strings and comparing strings in UTF-16 code units order. For example, "banana" comes before "cherry". In a numeric sort, 9 comes before 80, but because numbers are converted to strings, "80" comes before "9" in the Unicode order. All undefined elements are sorted to the end of the array.
If compareFunction is supplied,
compareFunction(a,b) return value > 0 -> sort b before a
compareFunction(a,b) return value < 0 -> sort a before b
compareFunction(a,b) return value === 0 -> keep original order of a and b
let stringArray = ['Blue', 'Humpback', 'Beluga'];
let numberArray = [40, 1, 5, 200];
let numericStringArray = ['80', '9', '700'];
let mixedNumericArray = ['80', '9', '700', 40, 1, 5, 200];
function compareNumbers(a, b) {
return a - b;
}
stringArray.join(); // 'Blue,Humpback,Beluga'
stringArray.sort(); // ['Beluga', 'Blue', 'Humpback']
numberArray.join(); // '40,1,5,200'
numberArray.sort(); // [1, 200, 40, 5]
numberArray.sort(compareNumbers); // [1, 5, 40, 200]
numericStringArray.join(); // '80,9,700'
numericStringArray.sort(); // ['700', '80', '9']
numericStringArray.sort(compareNumbers); // ['9', '80', '700']
mixedNumericArray.join(); // '80,9,700,40,1,5,200'
mixedNumericArray.sort(); // [1, 200, 40, 5, '700', '80', '9']
mixedNumericArray.sort(compareNumbers); // [1, 5, '9', 40, '80', 200, '700']
func.apply(thisArg, [argsArray])
The value of this provided for the call to func.
An array-like object, specifying the arguments with which func should be called, or null or undefined if no arguments should be provided to the function.
const array = ['a', 'b'];
const elements = [0, 1, 2];
array.push(array, elements);
console.info(array); // ["a", "b", [0, 1, 2]]
// apply function
const array = ['a', 'b'];
const elements = [0, 1, 2];
array.push.apply(array, elements);
console.info(array); // ["a", "b", 0, 1, 2]
// min/max number in an array
const numbers = [5, 6, 2, 3, 7];
// using Math.min/Math.max apply
let max = Math.max.apply(null, numbers);
// This about equal to Math.max(numbers[0], ...)
// or Math.max(5, 6, ...)
let min = Math.min.apply(null, numbers);
// vs. simple loop based algorithm
max = -Infinity, min = +Infinity;
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
if (numbers[i] < min) {
min = numbers[i];
}
}
source: mdn web docs/Array.prototype.sort(), mdn web docs/Function.prototype.apply()
Make a program that filters a list of strings and returns a list with only your friends name in it.
If a name has exactly 4 letters in it, you can be sure that it has to be a friend of yours! Otherwise, you can be sure he's not...
Note: keep the original order of the names in the output.
function friend(friends){
return friends.filter(friend => friend.length === 4);
}
Write a function that takes in a string of one or more words, and returns the same string, but with all five or more letter words reversed (Just like the name of this Kata). Strings passed in will consist of only letters and spaces. Spaces will be included only when more than one word is present.
Examples:
spinWords( "Hey fellow warriors" ) => returns "Hey wollef sroirraw"
spinWords( "This is a test") => returns "This is a test"
spinWords( "This is another test" )=> returns "This is rehtona test"
function spinWords(string){
let result = string.split(' ').map(function(word){
if (word.length >= 5) {
return word.split("").reverse().join('');
} else { return word }
});
return result.join(' ');
}
function spinWords(words){
return words.split(' ').map(function (word) {
return (word.length > 4) ? word.split('').reverse().join('') : word;
}).join(' ');
}
function spinWords(str){
return str.split(' ').map( w => w.length<5 ? w : w.split('').reverse().join('') ).join(' ');
}