Advertisement
Frumkin

Untitled

Feb 27th, 2022
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let quickSort = (arr = [], left = [], right = []) => {
  2.     if (arr.length <= 1) return arr;
  3.    
  4.     let pivot = arr.pop();
  5.    
  6.     for (let i = 0; i < arr.length; i++)
  7.         (compare(arr[i], pivot) ? left : right).push(arr[i]);
  8.    
  9.     return [...quickSort(left), pivot, ...quickSort(right)];
  10. }
  11.  
  12. const prompt = require('prompt-sync')({ sigint: true });
  13.  
  14. const songs = [
  15.     'Wonderwall',
  16.     'Californication',
  17.     'Free Fallin\'',
  18.     'Beat It',
  19.     'Africa'
  20. ];
  21.  
  22. console.log(quickSort(songs));
  23.  
  24. function compare(left, right) {
  25.     while (true) {
  26.         let input = prompt(`Is "${left}" better than "${right}"? [y/n]`);
  27.         input = input.toLowerCase();
  28.         if (isValidInput(input)) return input == 'y';
  29.         console.log('Invalid input!');
  30.     }
  31. }
  32.  
  33. function isValidInput(input) {
  34.     return input == 'y' || input == 'n';
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement