Skip to content

Calvinjwala/functions_lab

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Functions Lab

  1. Write a function that prints "Hello, World!" to the console when called.

  2. Write a function that takes the name of a person as an argument, and says "Hello" to that person in the console.

  3. If your previous function did not use the return keyword, modify it so that it does. It should return the message to be logged instead of logging it directly. Store the result in a variable and log that instead.

    Bonus: Appreciate the difference between logging from within the function and returning a value to be logged outside the function.

  4. Write a function add() that takes two numbers as arguments and returns the sum.

    • Write a function sub() take takes two numbers as arguments and returns the difference.

    • Write a function combine() that takes three parameters. The first two are numbers and the last is a boolean. If the boolean is true, return the sum of the first two arguments, otherwise, return the difference. Hint: use add() and sub() from within combine().

  5. Write a function that prints out how many times it has been called. For example:

var funCounter = function () {
    console.log("I've been called " + x + " times");
};

funCounter();
funCounter();
funCounter();

// Should print out:
// => "I've been called 1 times"
// => "I've been called 2 times"
// => "I've been called 3 times"

Hint: Can you do this using only local variables? Think about what we talked about regarding variable scope. Can you rely on the help of a global variable?

Bonus:

  1. Write a function to return true or false if a number passed in is a prime number.

  2. Write a function called merge. The function should take two sorted arrays of numbers as input and return a merged array of the sorted numbers from the input. For example, if the input arrays were var arr1 = [3,6,11]; var arr2 = [2,4,5,8,9]; Then the returned array would be: [2,3,4,5,6,8,9,11]. Avoid using any built-in sorting methods.

  3. Write a function called letterCount that takes a string and finds out how many times a character occurs. For example, if the input was "apple", the output should inform you that "a" occurred once, "p" occurred twice, and "l" and "e" each occurred once. You have some flexibility in how you want to output this data.

About

Functions Lab

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%