-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfunction.html
More file actions
166 lines (135 loc) · 4.59 KB
/
function.html
File metadata and controls
166 lines (135 loc) · 4.59 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<!-- in this lesson: -->
<!-- ------------------- -->
<!--
Lesson 7 : Functions
1. Functions= let us reuse code
2. Return = get a value out of a function
3. Parameters = put value into a function
4. improved the code for Rock, Paper, Scissors
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Functions</title>
</head>
<body>
<script>
//🟨Function : let us reuse code
function function1() {
console.log("hello") //Create function
console.log(2 + 2)
}
function1() //Running function
function1()
//🟨Rules for Function names = camelCase
//🟦1. Cant use special words Example : function
//🟦2. Cant start with a number Example : 1function
//🟦3. Cant use special characters except $ _
//🟨Return Statement : lets us get a value out of a function
//🟨Parameter : puts a value into a function.
//🟨Syntax parameter names: = camelCase
//🟦1. Cant use special words Example : function
//🟦2. Cant start with a number Example : 1function
//🟦3. Cant use special characters except $ _
/*
function calculation (parameter1)
{
console.log(parameter1 * 0.1);
}
*/
function calculateTax(cost, taxPercent = 0.1) {
console.log(cost * taxPercent)
}
calculateTax(2000, 0.2)
calculateTax(5000)
//🟨Write a function that takes two numbers as parameters and returns their sum.
var x = 5
var y = 5
function addNumbers() {
var sum = x + y
return sum
}
var result = addNumbers()
console.log(result)
//🟨 Create a function that calculates the area of a rectangle. It should take the width and height
// as parameters and return the area.
var height = 500
var width = 750
function rectangleAreaCalculation() {
var area = height * width
return area
}
var rectangleArea = rectangleAreaCalculation()
console.log(rectangleArea)
//🟨 Write a function that checks if a given number is even or odd. The function should return "Even" or
// "Odd" based on the input.
function checkEvenOdd(number) {
if (number % 2 === 0) {
return "Even"
} else {
return "Odd"
}
}
console.log(checkEvenOdd(5))
//🟨 Develop a function that finds the maximum of three numbers. The function should take three
// parameters and return the largest number.
num1 = 5
num2 = 7
num3 = 6
function biggest_in_3num() {
if (num1 > num2 && num1 > num3) {
return `${num1} is Biggest`
} else if (num2 > num1 && num2 > num3) {
return `${num2} is Biggest`
} else {
return `${num3} is Biggest`
}
}
console.log(biggest_in_3num())
//🟨 Create a function that takes a string as input and returns the number of vowels (a, e, i, o, u)
// in the string.
function countVowels(inputString) {
let vowelCount = 0
const vowels = "aeiou"
for (i = 0; i < inputString.length; i++) {
if (vowels.includes(inputString[i].toLowerCase())) {
vowelCount++
}
}
return vowelCount
}
console.log(countVowels(`Hello Deep!`))
//🟨 Write a function that calculates the factorial of a positive integer. The function should take an integer
// as a parameter and return its factorial.
function factorial(n) {
if (n === 0 || n === 1) {
return 1
}
return n * factorial(n - 1)
}
//🟨 Develop a function that checks if a given string is a palindrome. The function should return true if
// the string is a palindrome and false otherwise.
function isPalindrome(str) {
str = str.toLowerCase().replace(/[^a-zA-Z0-9]/g, "")
return str === str.split("").reverse().join("")
}
//🟨 Create a function that takes an array of numbers as input and returns the sum of all the numbers in
// the array.
function sumArray(arr) {
return arr.reduce((acc, current) => acc + current, 0)
}
//🟨 Write a function that generates and returns a random integer between a specified minimum and maximum
// value. The function should take the minimum and maximum values as parameters.
function randomInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min
}
//🟨 Develop a function that converts a temperature in degrees Celsius to Fahrenheit. The function should
// take the temperature in Celsius as a parameter and return the equivalent temperature in Fahrenheit.
function celsiusToFahrenheit(celsius) {
return (celsius * 9) / 5 + 32
}
</script>
</body>
</html>