Guest User

Untitled

a guest
Jul 12th, 2018
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. // 1. Write a function called helloWorld that returns the string 'Hello World!'.
  2.  
  3. function helloWorld() {
  4. return "Hello World!";
  5. }
  6.  
  7. // helloWorld();
  8.  
  9. /*
  10. 2. Write a function called lambdaSchool that has a single parameter called num.
  11. num will be a positive integer.
  12. If num is divisible by 3 return the string 'Lambda'
  13. If num is divisible by 5 return the string 'School'
  14. If num is divisible by 3 AND 5 return the string 'Lambda School' (notice the space)
  15. If num is NOT divisible by 3 or 5 then return num.
  16. Example:
  17. lambdaSchool(15); // returns 'Lambda School'
  18. lambdaSchool(8); // returns 8
  19. */
  20.  
  21.  
  22. function lambdaSchool(num) {
  23.  
  24.  
  25. if (num % 3 === 0 && num % 5 === 0)
  26. return 'Lambda School';
  27.  
  28. else if (num % 3 === 0)
  29. return 'Lambda';
  30.  
  31. else if (num % 5 === 0)
  32. return 'School';
  33.  
  34. else
  35. return num;
  36.  
  37. }
  38.  
  39. // lambdaSchool (15) // returns "Lambda School"!
  40.  
  41. /*
  42. 3. Write a function called longestString that has a single parameter called strs.
  43. strs will be an array of strings.
  44. Return the longest string that is in strs.
  45. If there is a tie for the longest string then return the one that occurs first.
  46. Example:
  47. longestString(['hi', 'hello', 'ni hao', 'guten tag']); // returns 'guten tag'
  48. longestString(['JavaScript', 'HTML', 'CSS']); // returns 'JavaScript'
  49. */
  50.  
  51.  
  52. function longestString( strs) {
  53.  
  54. var longestWord = '';
  55.  
  56. for(var i = 0; i < strs.length; i++){
  57. if (strs[i].length > longestWord.length){
  58. longestWord = strs[i];
  59.  
  60.  
  61. }
  62. }
  63. return longestWord
  64. }
  65.  
  66. // longestString(['JavaScript', 'HTML', 'CSS']); // Returns JS!
  67.  
  68.  
  69. /*
  70. 4. Write a function called computeUserAverageAge that has a single parameter called users
  71. users is an array of user objects.
  72. Each user object has a property called age that is a number.
  73. Compute the average age of all user objects in the users array.
  74. Round off the decimals if needed and return the number.
  75. Example:
  76. const users = [{
  77. name: 'Brendan Eich',
  78. age: 56,
  79. }, {
  80. name: 'Linus Torvalds',
  81. age: 48,
  82. }, {
  83. name: 'Margaret Hamilton',
  84. age: 81,
  85. }];
  86. computeUserAverageAge(users); // returns 62 (This number is rounded up from 61.6666)
  87. */
  88.  
  89.  
  90. const user = [{
  91. name: 'Patrick',
  92. age: 29,
  93. }, {
  94. name: 'John',
  95. age: 27,
  96. }, {
  97. name: 'Tommy',
  98. age: 24,
  99. }];
  100.  
  101. function computeUserAverageAge(user) {
  102. let userAge = 0; // initial expression
  103. let count = user.length
  104. for (let i = 0; i < user.length; i++) {
  105. userAge = userAge + user[i].age
  106. }
  107. return Math.round (userAge / count)
  108. }
  109.  
  110. // computeUserAverageAge(user) // output 27
Advertisement
Add Comment
Please, Sign In to add comment