LoganBlackisle

Dice

Jun 22nd, 2020
1,378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Make a function diceThrow, that returns a Promise. After a random duration of up to 2 seconds, this Promise should print a random integer between 1 and 6, and then resolve with this integer as result. */
  2.  
  3. function diceThrow() {
  4.      return new Promise(function (res, rej) {
  5.           let dice = Math.random() * (6 - 1) + 1;
  6.           function throw() {
  7.                if (dice > 0 && dice < 7) res(dice);
  8.                else if (dice === 0 || dice > 6) rej('Invalid!');
  9.           }
  10.      });
  11. }
  12.  
  13. diceThrow(1)
  14.      .then((resultat) => {
  15.           console.log('Resultat: ' + resultat); return terningKast(2);
  16.      })
  17.      .then((resultat) => console.log('Resultat: ' + resultat))
  18.      .catch((fejl) => console.log('Fejl: ' + fejl));
  19.  
  20. /* Make a function throwTwoDice, that uses diceThrow to throw two dice. If the two dice are the same, it should print 'won!', otherwise it should print 'lost!' */
  21.  
  22. function throwTwoDice1() {
  23.      const dice1 = diceThrow();
  24.      const dice2 = diceThrow();
  25.      if (dice1 === dice2) return 'Won!';
  26.      else return 'Lost!';
  27. }
  28.  
  29. /* The function throwTwoDice must be implemented in two ways - one with, and the other without, use of async and await. */
  30.  
  31. async function throwTwoDice2() {
  32.      let dice1 = await diceThrow();
  33.      let dice2 = await diceThrow();
  34.      if (dice1 === dice2) return 'Won!';
  35.      else return 'Lost!';
  36. }
  37.  
  38. /* ------------------- */
  39.  
  40. /* Make a function lessThan(a,b) with two parameters. The function must return a boolean, that tells whether or not a is less than b. The function must be able to compare to numbers or two strings. The function must throw an exception, if the two parameters are different from the above. */
  41.  
  42. function lessThan(a, b) {
  43.      if (typeof (a) == Number && typeof (b) == Number || typeof (a) == String && typeof (b) == String) {
  44.           return a < b;
  45.      } else {
  46.           throw 'a & b must be the same!';
  47.      }
  48. }
  49.  
  50. /* Test the function. */
  51.  
  52. console.log(lessThan(3, 7));
  53.  
  54. console.log(lessThan("a", "b"));
  55.  
  56. console.log(lessThan(9, 7));
  57.  
  58. console.log(lessThan("f", "b"));
  59.  
  60. console.log(lessThan(17, "b"));
  61.  
  62. /* Make an array with some numbers, and use the function lessThan, and a for-loop, to find the smallest number in the array. */
  63.  
  64. let arr = [2, 7, 3, 17, 42];
  65.  
  66. let x = Math.max
  67.  
  68. for (let i = 0; i < arr.length - 1; i++) {
  69.      if (arr[i] < x) {
  70.           x = arr[i];
  71.      }
  72. }
  73.  
  74. /* Then make an array with some strings and use the function lessThan, and the method reduce, on the array to find the smallest string in the array. */
  75.  
  76. let strArr = ['abc', 'ghi', 'def', 'jkl'];
  77.  
  78. let toReturn = strArr.reduce(function (strs, val, index) {
  79.      let sep = strArr.length ? ", " : "";
  80.      return strs + sep + val;
  81. }, '');
Advertisement
Add Comment
Please, Sign In to add comment