-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.js
More file actions
117 lines (108 loc) · 3.53 KB
/
functions.js
File metadata and controls
117 lines (108 loc) · 3.53 KB
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
"use strict";
/**
* TODO:
* Create a function called 'sayHello' that takes a parameter 'name'.
* When called, the function should return a message that says hello to the passed in name.
*
* Example
* > sayHello("codeup") // returns "Hello, codeup!"
*/
function sayHello(name) {
return "Hello, " + name + "!";
}
/**
* TODO:
* Call the function 'sayHello' and pass your name as a string literal argument.
* Store the result of the function call in a variable named 'helloMessage'.
*
* console.log 'helloMessage' to check your work
*/
var helloMessage = sayHello("Lili");
console.log(helloMessage);
/**
* TODO:
* Store your name as a string in a variable named 'myName', and pass that
* variable to the 'sayHello' function. You should see the same output in the
* console.
*/
var myName = "Lili";
sayHello(myName);
console.log(sayHello(myName));
console.log(sayHello("Lucy"));
// Don't modify the following line, it generates a random number between 1 and 3
// and stores it in a variable named random
var random = Math.floor((Math.random() * 3) + 1);
/**
* TODO:
* Create a function called 'isTwo' that takes a number as a parameter.
* The function should return a boolean value based on whether or not the passed
* number is the number 2.
*
* Example
* > isTwo(1) // returns false
* > isTwo(2) // returns true
* > isTwo(3) // returns false
*
* Call the function 'isTwo' passing the variable 'random' as a argument.
*
* console.log *outside of the function* to check your work (you should see a
* different result everytime you refresh the page if you are using the random
* number)
*/
function isTwo(a) {
return a === 2;
}
console.log(isTwo(random));
console.log(random);
/**
* TODO:
* Create a function named 'calculateTip' to calculate a tip on a bill at a
* restaurant. The function should accept a tip percentage and the total of the
* bill, and return the amount to tip
*
* Examples:
* > calculateTip(0.20, 20) // returns 4
* > calculateTip(0.25, 25.50) // returns 6.375
* > calculateTip(0.15, 33.42) // returns 5.013
*/
function calculateTip(a, b) {
return a * b;
}
calculateTip(0.20, 20);
console.log(calculateTip(0.20, 20));
calculateTip(0.25, 25.50);
console.log(calculateTip(0.25, 25.50));
calculateTip(0.15, 33.42);
console.log(calculateTip(0.15, 33.42));
/**
* TODO:
* Use prompt and alert in combination with your calculateTip function to
* prompt the user for the bill total and a percentage they would like to tip,
* then display the dollar amount they should tip
*/
var percentage = Number(prompt("What percentage do you want to tip?"));
var totalBill = Number(prompt("How much is the total bill?"));
alert("you should tip $" + calculateTip(percentage, totalBill));
console.log("you should tip $" + calculateTip(percentage, totalBill));
// alert("you should tip $" + calculateTip(Number(prompt("Tip percentage")), Number(prompt("total bill?"))));
/**
* TODO:
* Create a function named `applyDiscount`. This function should accept a price
* (before a discount is applied), and a discount percentage (a number between 0
* and 1). It should return the result of applying the discount to the original
* price.
*
* Example:
* > var originalPrice = 100;
* > var dicountPercent = .2; // 20%
* > applyDiscount(originalPrice, dicountPercent) // 80
*
* > applyDiscount(45.99, 0.12) // 40.4712
*/
function applyDiscount(price, discount) {
if (discount > 0 && discount < 1) {
return price - price * discount;
}
}
applyDiscount(45.99, 0.12);
console.log("price after discount: $" + applyDiscount(45.99, 0.12));