-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay04_FunctionsIntro.js
131 lines (70 loc) · 2.48 KB
/
Day04_FunctionsIntro.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
// functions have return types as well as non return types
// function greet(){
// console.log("Hi everyone");
// console.log("Welcome to JavaScript");
// }
// greet();
function greetPeople(person){
console.log(`${person} is greeting you`)
}
greetPeople("Ibrahim");
function greetPeople(person){
console.log(`${person} is greeting you`)
}
greetPeople("Ibrahim");
greetPeople(5);
greetPeople(true);
//When we use function, we can just give the variable names and no need for data type
//For function parameters, we can also use default values
function personInfo(firstName, lastName, location ="VA", isWorking){
console.log(`${firstName}, ${lastName},${location},${isWorking}, `);
}
// personInfo("Ibrahim", "Settar", "North Carolina", true);
personInfo("Ibrahim", "Settar", "North Carolina");
//ask user to provide 2 numbers and get their addition, subtraction, division and multiplicatio
let x = parseInt( prompt('type a number'));
let y = parseInt( prompt('type a number'));
function add(x,y){
console.log(x+y);
}
function subtract(x,y){
console.log(x-y);
}
function divide(x,y){
console.log(x/y);
}
function multiply(x,y){
console.log(x*y);
}
add(x, y);
multiply(x, y);
subtract(x, y);
divide(x, y);
var total = function (num1, num2,num3){
console.log(num1+num2+num3);
}
total(3, 4, 5);
function sum (){
let num1 = 5;
let num2 = 8;
let num3 = 3;
return num1+num2+num3;
}
// console.log( sum());
function square(v){
return v * v;
}
console.log(square(5));
//use 3 params, you need to add them with a function, but the default values should be 3, 2, 1
function addNumbers(n1=3, n2=2,n3=1){
return n1+n2+n3;//6
}
console.log(addNumbers(23, 2, 4));
console.log(addNumbers());
//ask user to give 2 numbers, generate a function and assign them to a variable, then print on the console
let n1 = parseInt(prompt('type a number'));
let n2 = parseInt(prompt('type a number'));
let total = function (n1, n2){
return n1+n2;
}
console.log(total(n1, n2));