Advertisement
3vo

Problem 7. Sort 3 Numbers with Nested Ifs

3vo
Oct 25th, 2022
620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Write a program that enters 3 real numbers and prints them sorted in descending order.
  2. // * Use nested if statements.
  3. // Note: Don’t use arrays and the built-in sorting functionality.
  4. // Examples:
  5. //  a    b     c           result
  6. //  5    1     2           521
  7. // -2   -2     1           1 -2 -2
  8. // -2    4     3           4 3 -2
  9. //  0   -2.5   5           5 0 -2.5
  10. // -1.1 -0.5  -0.1        -0.1 -0.5 -1.1
  11. //  10   20    30          30 20 10
  12. //   1    1    1           1 1 1
  13. let input = ['5', '1', '2'];
  14. let print = this.print || console.log;
  15. let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  16. //Get the input and convert it to numbers:
  17. let a = +gets();
  18. let b = +gets();
  19. let c = +gets();
  20. //Sort the numbers descending order:
  21. if (a < b) {
  22.     if (b < c) {
  23.         print(c, b, a);
  24.     } else if (a < c) {
  25.         print(b, c, a);
  26.     } else {
  27.         print(b, a, c);
  28.     }
  29. } else if (c < a) {
  30.     if (b < c) {
  31.         print(a, c, b);
  32.     } else {
  33.         print(a, b, c);
  34.     }
  35. } else {
  36.     print(c, a, b);
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement