-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4_FunctionsExercise.js
132 lines (108 loc) · 5.21 KB
/
4_FunctionsExercise.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Week 1 - Functions
// In the following exercises, you will need to place your code or answer underneath each exercise prompt.
// First try answering these without using references or looking up any information.
// Then, check your answer by using references and/or running your code.
// You can run your JS code using the Chrome or Firefox Developer tools, or by using Node.js.
// Feel free to update your answers if you got them wrong at first -- this exercise is for your own learning.
// But make sure you understand why the correct answer is right.
// Exercise 1. Define a function called logGreeting() that prints the string “Hello!” (use console.log).
// Underneath the function, write the line of code that runs the function.
function logGreeting(){
console.log ("Hello!")
}
console.log(logGreeting())
// Exercise 2. Define a function called getName() that *returns* a string that is your name.
// Remember, this function should return the string -- not print it.
// Then print your name in the console by passing getName() into the console.log() function.
function getName(name){
return name.toString()
}
console.log(getName("Peter"))
// Exercise 3. Call a function within a function -- Write a function called logGreeting2()
// that prints a full sentence that contains your name: "Hello! My name is <name>."
// logGreeting2 should call myName() to get your name.
// Then print your greeting to the console by calling logGreeting2().
function logGreeting2(){
function myName(){
let myName = "Mai"
return myName
}
console.log (`Hello! My name is ${myName()}.`)
}
console.log(logGreeting2())
// Exercise 4. Write a function that takes 3 parameters that are all numbers.
// The function should return the sum of the 3 numbers.
// Then write some function calls you would use to test your function.
function exercise4(arg1, arg2, arg3){
return arg1 + arg2 + arg3;
}
console.log(exercise4(3, 4, 5));
console.log(exercise4(5, -6, 9));
console.log(exercise4(5, 60, 88));
// Exercise 5. Let's say a museum gives a discount for children ages 14 or under, and seniors 65 or older.
// Write a function that takes in a person's age and returns true if they should get a discount.
// Otherwise it should return false.
// Then write some function calls you would use to test your function.
function exercise5 (age){
if (age <= 14 || age >= 65) {
return true;
} else {
return false;
}
}
console.log(exercise5 (45));
console.log(exercise5 (66));
console.log(exercise5 (6));
// Exercise 6. Write a function that takes 2 parameters -- one number and one string.
// The function should print the string the given number of times.
// Then write some function calls you would use to test your function.
// Exercise 7. Read the following code (don't run it yet)
function mysteryFunction1(p1) {
return p1 * 2;
}
const y = 4;
const z = mysteryFunction1(y);
console.log("The value of y is " + y);
console.log("The value of z is " + z);
// Without running the code, write down in a comment:
// 1. What mysteryFunction1 does? The function takes an integar and multiplies by 2;
// 2. What prints out for the value of y? The value is 4;
// 3. What prints out for the value of z? The value is 8;
// Now run the code and see if you're correct.
// Were you correct? If not, what did you learn? Yes.
// Exercise 8. Read the following code (don't run it yet)
function mysteryFunction2(p1, p2) {
const x = p1 - p2;
return x;
}
const a = mysteryFunction2(10, 4);
const b = mysteryFunction2(a, 1);
console.log("The value of a is " + a);
console.log("The value of b is " + b);
// Without running the code, write down in a comment:
// 1. What mysteryFunction2 does? It takes two parameters (both integars) and substrate the first parameter over the second; and returns x which holds an integar;
// 2. What prints out for the value of a? The value is 6;
// 3. What prints out for the value of b? The value is 5;
// Now run the code and see if you're correct.Yes.
// Were you correct? If not, what did you learn?
// Exercise 9. This exercise is to practice reading the documentation for functions.
// String.split() is a function in the JavaScript standard library that you can use in your code.
// Read about the split() function here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
// Try to use it to solve the following challenges:
// Here's a string I made representing my grocery list.
// Using split() and groceryList, make an array of my grocery list items
const groceryList = "eggs,carrots,orange juice";
newGroceryListArr = groceryList.split(",");
console.log(newGroceryListArr);
// Here's a string I made representing my morning schedule.
// Using split() and mySchedule, make an array of the *first 2* things I do in the morning
// There are multiple ways to do this, but try doing it using only the split() function.
const mySchedule = "wake up--->brush teeth--->eat breakfast--->go to work";
newMyScheduleArr = mySchedule.split("--->", 2);
console.log(newMyScheduleArr);
// Congrats, you made it to the end!
// Did you find this easy or hard? If you used references, which ones helped you?
// Please answer in a comment below.
//
// Email your file to the course staff,
// or commit your file to GitHub and email us a link.