Advertisement
eimkasp

Trečiadienio JS užduotys

Jul 20th, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Parašykite, funkcijų spausdinamas arba gražinamas reikšmes
  2.  
  3.  
  4. /*
  5.     Užduotis 1
  6. */
  7.  
  8. function test() {
  9.    console.log(a);
  10.    console.log(foo());
  11.    
  12.    var a = 1;
  13.    function foo() {
  14.       return 2;
  15.    }
  16. }
  17. test();
  18.  
  19.  
  20. /*
  21.     Užduotis 2
  22. */
  23. function darytiKazka(string) {
  24.     var newStr = "";
  25.     for (var i = string.length - 1; i >= 0; i--) {
  26.         newStr += string[i];
  27.     }
  28.     return newStr;
  29. }
  30.  
  31. var rezultatas = darytiKazka(“dog”);
  32. console.log(rezultatas);
  33.  
  34.  
  35.  
  36. /*
  37.     Užduotis 3
  38. */
  39. function geriausiaFunkcija(a) {
  40.     var geras = a[0];
  41.     for (var i = 1; i <  a.length; i += 1) {
  42.         if (a[i] > geras) {
  43.             geras = a[i];
  44.         }
  45.     }
  46.     return geras;
  47. };
  48.  
  49. geriausiaFunkcija([5,10, 100, 12, 2]);
  50.  
  51.  
  52.  
  53. /*
  54.     Užduotis 4
  55. */
  56.  
  57. function calculateSupply(age, numPerDay) {
  58.   var maxAge = 100;
  59.   var totalNeeded = (numPerDay * 365) * (maxAge - age);
  60.   var message = 'You will need ' + totalNeeded + ' cups of tea to last you until the ripe old age of ' + maxAge;
  61.  
  62.     console.log(message);
  63. }
  64.  
  65. calculateSupply(28, 36);
  66.  
  67. /*
  68.     Užduotis 5
  69. */
  70.  
  71. function amountTocoins(amount, coins)   
  72. {  
  73.  if (amount === 0)   
  74.   {  
  75.      return [];  
  76.    }   
  77.  else  
  78.    {  
  79.      if (amount >= coins[0])   
  80.        {  
  81.         left = (amount - coins[0]);  
  82.         return [coins[0]].concat( amountTocoins(left, coins) );  
  83.         }   
  84.       else  
  85.         {  
  86.          coins.shift();  
  87.          return amountTocoins(amount, coins);  
  88.         }  
  89.     }  
  90. }   
  91. console.log(amountTocoins(46, [25, 10, 5, 2,1])); 
  92.  
  93. http://www.w3resource.com/javascript-exercises/javascript-function-exercise-14.php
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement