Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1. Write a function called helloWorld that returns the string 'Hello World!'.
- function helloWorld() {
- return "Hello World!";
- }
- // helloWorld();
- /*
- 2. Write a function called lambdaSchool that has a single parameter called num.
- num will be a positive integer.
- If num is divisible by 3 return the string 'Lambda'
- If num is divisible by 5 return the string 'School'
- If num is divisible by 3 AND 5 return the string 'Lambda School' (notice the space)
- If num is NOT divisible by 3 or 5 then return num.
- Example:
- lambdaSchool(15); // returns 'Lambda School'
- lambdaSchool(8); // returns 8
- */
- function lambdaSchool(num) {
- if (num % 3 === 0 && num % 5 === 0)
- return 'Lambda School';
- else if (num % 3 === 0)
- return 'Lambda';
- else if (num % 5 === 0)
- return 'School';
- else
- return num;
- }
- // lambdaSchool (15) // returns "Lambda School"!
- /*
- 3. Write a function called longestString that has a single parameter called strs.
- strs will be an array of strings.
- Return the longest string that is in strs.
- If there is a tie for the longest string then return the one that occurs first.
- Example:
- longestString(['hi', 'hello', 'ni hao', 'guten tag']); // returns 'guten tag'
- longestString(['JavaScript', 'HTML', 'CSS']); // returns 'JavaScript'
- */
- function longestString( strs) {
- var longestWord = '';
- for(var i = 0; i < strs.length; i++){
- if (strs[i].length > longestWord.length){
- longestWord = strs[i];
- }
- }
- return longestWord
- }
- // longestString(['JavaScript', 'HTML', 'CSS']); // Returns JS!
- /*
- 4. Write a function called computeUserAverageAge that has a single parameter called users
- users is an array of user objects.
- Each user object has a property called age that is a number.
- Compute the average age of all user objects in the users array.
- Round off the decimals if needed and return the number.
- Example:
- const users = [{
- name: 'Brendan Eich',
- age: 56,
- }, {
- name: 'Linus Torvalds',
- age: 48,
- }, {
- name: 'Margaret Hamilton',
- age: 81,
- }];
- computeUserAverageAge(users); // returns 62 (This number is rounded up from 61.6666)
- */
- const user = [{
- name: 'Patrick',
- age: 29,
- }, {
- name: 'John',
- age: 27,
- }, {
- name: 'Tommy',
- age: 24,
- }];
- function computeUserAverageAge(user) {
- let userAge = 0; // initial expression
- let count = user.length
- for (let i = 0; i < user.length; i++) {
- userAge = userAge + user[i].age
- }
- return Math.round (userAge / count)
- }
- // computeUserAverageAge(user) // output 27
Advertisement
Add Comment
Please, Sign In to add comment