-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.js
More file actions
113 lines (98 loc) · 3.08 KB
/
functions.js
File metadata and controls
113 lines (98 loc) · 3.08 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
"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 + '!');
}
sayHello('rocket');
/**
* 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 = "rocket";
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 = 'rocket';
sayHello(myName)
// 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(numb) {
return numb === 2;
}
console.log(isTwo(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(tipPercent, totalBill) {
return(tipPercent * totalBill);
}
console.log(calculateTip(0.20, 20));
/**
* 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 userTip = prompt('how much do you want to tip?');
var userBill = prompt('how much was your bill?');
var userTipResult = calculateTip(userTip, userBill);
alert('your tip should be ' + userTipResult);
/**
* 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 discountPercent = .2; // 20%
* > applyDiscount(originalPrice, discountPercent) // 80
*
* > applyDiscount(45.99, 0.12) // 40.4712
*/
function applyDiscount(price, discount) {
return console.log(price - (price * discount));
}
applyDiscount(45.99, 0.12);