Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- function filterArray(array) {
- let finalArray = [];
- while (array.length > 0) {
- let biggestNumber = Math.max(...array);
- let smallestNumber;
- if (array.length > 1) {
- smallestNumber = Math.min(...array);
- finalArray.push(biggestNumber, smallestNumber);
- } else {
- finalArray.push(biggestNumber);
- }
- array = array.filter(a => a != biggestNumber);
- array = array.filter(a => a != smallestNumber);
- }
- console.log(finalArray.join(' '));
- }
- let input = [94, 1, 21, 3, 52, 69, 63, 31, 2, 18, 94];
- let result = filterArray(input);
- console.log(result);
- function sorting(arr) {
- let highLowArr = [];
- let descendingArr = arr.sort((a, b) => b - a);
- let smallest = descendingArr.pop();
- let biggest = descendingArr.shift();
- while (smallest !== undefined || biggest !== undefined) {
- //Add to sorted array
- if (biggest !== undefined) {
- highLowArr.push(biggest);
- }
- if (smallest !== undefined) {
- highLowArr.push(smallest);
- }
- //Get new values from old arr
- smallest = descendingArr.pop();
- biggest = descendingArr.shift();
- }
- //Output
- console.log(highLowArr.join(' '));
- }
- let input2 = [94, 1, 21, 3, 52, 69, 63, 31, 2, 18, 94];
- let result2 = sorting(input2);
- console.log(result2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement