@@ -13,72 +13,218 @@ currDate();
13
13
14
14
document . write ( `<h1> Greets to User </h1>` ) ;
15
15
16
- // function greet (firstName , lastName){
17
- // firstName = prompt("Enter First Name");
18
- // lastName= prompt("Enter Last Name");
16
+ function greet ( firstName , lastName ) {
17
+ firstName = prompt ( "Enter First Name" ) ;
18
+ lastName = prompt ( "Enter Last Name" ) ;
19
19
20
- // document.write(`Good morning ${firstName} ${lastName}`)
21
- // }
22
- // greet();
20
+ document . write ( `Good morning ${ firstName } ${ lastName } ` ) ;
21
+ }
22
+ greet ( ) ;
23
23
24
24
// Write a function that adds two numbers (input by user)
25
25
// and returns the sum of two numbers.
26
26
document . write ( `<h1> Adds two numbers </h1>` ) ;
27
- // function sum(num1, num2) {
28
- // num1 = Number(prompt("Enter First Number"));
29
- // num2 = Number(prompt("Enter Second Number"));
30
- // document.write(`Sum of two numbers: ${num1 + num2}`);
31
- // }
32
- // sum();
27
+ function sum ( num1 , num2 ) {
28
+ num1 = Number ( prompt ( "Enter First Number" ) ) ;
29
+ num2 = Number ( prompt ( "Enter Second Number" ) ) ;
30
+ document . write ( `Sum of two numbers: ${ num1 + num2 } ` ) ;
31
+ }
32
+ sum ( ) ;
33
33
34
34
// Write a function that takes three arguments num1, num2
35
35
// & operator & compute the desired operation. Return and
36
36
// show the desired result in your browser.
37
37
38
38
document . write ( `<h1> Calculator </h1>` ) ;
39
39
40
- // function calculator (numb1, numb2, operator){
41
- // numb1 = Number(prompt("Enter First Number"));
42
- // numb2 = Number(prompt("Enter Second Number"));
43
- // operator = prompt("Enter the operator");
44
-
45
- // if(operator=== "+"){
46
- // document.write(`Addition: ${numb1+numb2}`);
47
- // }
48
- // else if(operator=== "-"){
49
- // document.write(`Subtarction: ${numb1-numb2}`);
50
- // }
51
- // else if(operator=== "*"){
52
- // document.write(`Multiplication: ${numb1*numb2}`);
53
- // }
54
- // else if(operator=== "/"){
55
- // document.write(`Division: ${numb1/numb2}`);
56
- // }
57
- // }
58
- // calculator();
40
+ function calculator ( numb1 , numb2 , operator ) {
41
+ numb1 = Number ( prompt ( "Enter First Number" ) ) ;
42
+ numb2 = Number ( prompt ( "Enter Second Number" ) ) ;
43
+ operator = prompt ( "Enter the operator" ) ;
44
+
45
+ if ( operator === "+" ) {
46
+ document . write ( `Addition: ${ numb1 + numb2 } ` ) ;
47
+ } else if ( operator === "-" ) {
48
+ document . write ( `Subtarction: ${ numb1 - numb2 } ` ) ;
49
+ } else if ( operator === "*" ) {
50
+ document . write ( `Multiplication: ${ numb1 * numb2 } ` ) ;
51
+ } else if ( operator === "/" ) {
52
+ document . write ( `Division: ${ numb1 / numb2 } ` ) ;
53
+ }
54
+ }
55
+ calculator ( ) ;
59
56
60
57
// Write a function that squares its argument.
61
58
document . write ( `<h1> Squares </h1>` ) ;
62
- // function square(num) {
63
- // num = prompt("Enter a number");
64
- // let square = num ** 2;
65
- // document.write(`Square of ${num} is ${square}`);
66
- // }
67
- // square();
59
+ function square ( num ) {
60
+ num = prompt ( "Enter a number" ) ;
61
+ let square = num ** 2 ;
62
+ document . write ( `Square of ${ num } is ${ square } ` ) ;
63
+ }
64
+ square ( ) ;
68
65
69
- document . write ( `<h1> Factorial of a Number </h1>` ) ;
70
66
// Write a function that computes factorial of a number.
67
+ document . write ( `<h1> Factorial of a Number </h1>` ) ;
68
+
69
+ function factorial ( num ) {
70
+ // num = prompt("Enter a number");
71
+ if ( num === 0 || num === 1 ) {
72
+ // document.write(1)
73
+ return 1 ;
74
+ } else {
75
+ // document.write(num * factorial(num - 1))
76
+ return num * factorial ( num - 1 ) ;
77
+ }
78
+ }
79
+ document . write ( factorial ( 5 ) ) ;
80
+ console . log ( factorial ( 5 ) ) ;
71
81
72
82
// Write a function that take start and end number as inputs
73
83
// & display counting in your browser.
74
84
document . write ( `<h1> Counting </h1>` ) ;
75
85
function counting ( start , end ) {
76
86
start = Number ( prompt ( "Enter start number" ) ) ;
77
87
end = Number ( prompt ( "Enter end number" ) ) ;
78
- for ( let i = start ; i <= end ; i ++ ) {
79
- let count = "" ;
80
- count += i ;
81
- document . write ( `${ count } </br>` ) ;
88
+ for ( let i = start ; i <= end ; i ++ ) {
89
+ // let count ="";
90
+ // count += i;
91
+ document . write ( `${ i } </br>` ) ;
82
92
}
83
93
}
84
- counting ( )
94
+ counting ( ) ;
95
+
96
+ // Write a nested function that computes hypotenuse of a
97
+ // right angle triangle.
98
+ // Hypotenuse2 = Base2 + Perpendicular2
99
+ // Take base and perpendicular as inputs.
100
+ // Outer function : calculateHypotenuse()
101
+ // Inner function: calculateSquare()
102
+
103
+ document . write ( `<h1> Calculate Hypotenuse </h1>` ) ;
104
+
105
+ function calculateHypotenuse ( base , perpendicular ) {
106
+ function calcSquare ( num ) {
107
+ return num * num ;
108
+ }
109
+ let baseSquare = calcSquare ( base ) ;
110
+ let perpendicularSquare = calcSquare ( perpendicular ) ;
111
+ let hypotenuse = Math . sqrt ( baseSquare + perpendicularSquare ) ;
112
+ return hypotenuse ;
113
+ }
114
+ document . write ( calculateHypotenuse ( 5 , 6 ) ) ;
115
+
116
+ // Write a function that calculates the area of a rectangle.
117
+ // A = width * height
118
+ // Pass width and height in following manner:
119
+ // i. Arguments as value
120
+ // ii. Arguments as variables
121
+
122
+ document . write ( `<h1> Area of a Rectangle (Arguments as value) </h1>` ) ;
123
+
124
+ function calcAreaOfRectangle ( width , height ) {
125
+ return width * height ;
126
+ }
127
+ document . write ( calcAreaOfRectangle ( 8 , 10 ) ) ;
128
+
129
+ document . write ( `<h1> Area of a Rectangle (Arguments as variables) </h1>` ) ;
130
+ let width = 8 ;
131
+ let height = 15 ;
132
+ document . write ( calcAreaOfRectangle ( width , height ) ) ;
133
+
134
+ // Write a JavaScript function that checks whether a passed
135
+ // string is palindrome or not?
136
+ // A palindrome is word, phrase, or sequence that reads the same backward as
137
+ // forward, e.g., madam.
138
+ document . write ( `<h1> String is Palindrome or not? </h1>` ) ;
139
+ function isPalindrome ( str ) {
140
+ let reversed = str . split ( "" ) . reverse ( ) . join ( "" ) ;
141
+ return str === reversed ;
142
+ }
143
+ document . write ( `${ isPalindrome ( "madam" ) } <br>` ) ;
144
+
145
+ document . write ( `${ isPalindrome ( "eye" ) } <br>` ) ;
146
+ document . write ( `${ isPalindrome ( "ear" ) } <br>` ) ;
147
+ document . write ( `${ isPalindrome ( "son" ) } <br>` ) ;
148
+ document . write ( `${ isPalindrome ( "teeth" ) } <br>` ) ;
149
+ document . write ( `${ isPalindrome ( "teeth" ) } <br>` ) ;
150
+
151
+ // Write a JavaScript function that accepts a string as a
152
+ // parameter and converts the first letter of each word of the
153
+ // string in upper case.
154
+ // EXAMPLE STRING : 'the quick brown fox'
155
+ // EXPECTED OUTPUT : 'The Quick Brown Fox'
156
+
157
+ document . write ( `<h1> Capitalize </h1>` ) ;
158
+ function capitalizeWord ( words ) {
159
+ return words
160
+ . split ( " " )
161
+ . map ( ( word ) => word . charAt ( 0 ) . toUpperCase ( ) + word . slice ( 1 ) )
162
+ . join ( " " ) ;
163
+ }
164
+ document . write ( capitalizeWord ( `sadaf shahab <br>` ) ) ;
165
+ document . write ( capitalizeWord ( `the quick brown fox <br>` ) ) ;
166
+
167
+ // Write a JavaScript function that accepts a string as a
168
+ // parameter and find the longest word within the string.
169
+ // EXAMPLE STRING : 'Web Development Tutorial'
170
+ // EXPECTED OUTPUT : 'Development'
171
+ document . write ( `<h1> Largest Word </h1>` ) ;
172
+
173
+ function findLargestWord ( largeWord ) {
174
+ let words = largeWord . split ( " " ) ;
175
+ let longestWord = words . reduce ( ( longest , current ) => {
176
+ return current . length > longest . length ? current : longest ;
177
+ } , "" ) ;
178
+ return longestWord ;
179
+ }
180
+ document . write ( findLargestWord ( "Web Development Tutorial" ) ) ;
181
+
182
+ // Write a JavaScript function that accepts two arguments, a
183
+ // string and a letter and the function will count the number of
184
+ // occurrences of the specified letter within the string.
185
+ // Sample arguments : 'JSResourceS.com', 'o'
186
+
187
+ document . write ( `<h1> Count occurrences of a specific letter </h1>` ) ;
188
+
189
+ function countLetterOccurr ( word , letter ) {
190
+ let count = 0 ;
191
+ for ( let i = 0 ; i < word . length ; i ++ ) {
192
+ if ( word [ i ] === letter ) {
193
+ count ++ ;
194
+ }
195
+ }
196
+ return count ;
197
+ }
198
+ document . write ( countLetterOccurr ( "Circumference" , "e" ) ) ;
199
+
200
+ // 14. The Geometrizer
201
+ // Create 2 functions that calculate properties of a circle, using
202
+ // the definitions here.
203
+ // Create a function called calcCircumference:
204
+ // • Pass the radius to the function.
205
+ // • Calculate the circumference based on the radius, and output
206
+ // "The circumference is NN".
207
+ // Create a function called calcArea:
208
+ // • Pass the radius to the function.
209
+ // • Calculate the area based on the radius, and output "The area
210
+ // is NN".
211
+ // Circumference of circle = 2πr
212
+ // Area of circle
213
+ // =
214
+ // πr2
215
+
216
+ document . write ( `<h1> Properties of a Circle </h1>` ) ;
217
+
218
+ document . write ( `<h4> Circumference of a Circle </h4>` ) ;
219
+
220
+ function circumference ( radius ) {
221
+ return `The Circumference is ${ 2 * Math . PI * radius } ` ;
222
+ }
223
+ document . write ( `${ circumference ( 7 ) } ` ) ;
224
+
225
+ document . write ( `<h4> Area of a Circle </h4>` ) ;
226
+
227
+ function area ( radius ) {
228
+ return `The Area is ${ Math . PI * radius * radius } ` ;
229
+ }
230
+ document . write ( `${ area ( 2 ) } ` ) ;
0 commit comments