-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5_functions.js
229 lines (171 loc) · 7.05 KB
/
5_functions.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//* ===============================
//* Function in JavaScript
//* ==============================
//? In JavaScript, a function is a block of reusable code that performs a specific task or set of tasks. Functions are used to organize code into modular and manageable pieces, promote code reuse, and make programs more readable.
// 3 students at a same time wants to find the sum of two numbers
// 1st student
// var a = 5,
// b = 10;
// var sum1 = a + b;
// console.log(sum1);
// // 2nd student
// var a = 15,
// b = 15;
// var sum2 = a + b;
// console.log(sum2);
// // 3rd student
// var a = 55,
// b = 15;
// var sum3 = a + b;
// console.log(sum3);
// lets make a reusable code
// function sum(a, b) {
// return a + b;
// }
// console.log(sum(5, 5));
// console.log(sum(15, 50));
// console.log(sum(25, 750));
//* ===============================
//* Function Declaration:
//* ==============================
//? Declare a function using the function keyword, followed by the function name, parameters (if any), and the function body.
//? This step defines the function and specifies what code should be executed when the function is called.
// function greet() {
// console.log("Hello Guys, Welcome to Thapa Technical JS Course ");
// }
//* =================================================
//* Function Invocation (Calling a Function):
//* =================================================
//?After declaring a function, you can invoke or call it by using its name followed by parentheses.
//? If the function has parameters, provide values (arguments) for those parameters inside the parentheses.
//? How to call a function
// greet();
//! Practice Time
//! 1. Write a function to find the sum of two numbers.
//todo Tips "1st declare the function & then call it" In JavaScript, it's a good practice to declare (define) your functions before you call them. This ensures that the function is available for use when you try to call it.
// Function definition
// function sum(){
// var a=5, b=10;
// console.log(a+b);
// }
// sum();
// Calling the function
//* ==============================
//* Function Parameter:
//* ==============================
//? A function parameter is a variable that is listed as a part of a function declaration. It acts as a placeholder for a value that will be provided when the function is called. Parameters allow you to pass information into a function, making it more versatile and reusable.
// Syntax: function functionName(parameter1, parameter2, ...params) {
// // Function body
// // Code to be executed when the function is called
// }
//* ==============================
//* Function Argument:
//* ==============================
//? A function argument is a value that you provide when you call a function. Arguments are passed into a function to fill the parameters defined in the function declaration.
//? syntax:
//? functionName(argument1, argument2, ...);
//! Practice Time
//? Let's say we want to greet students with one same line
//! Write a JavaScript program that defines a function called greet to welcome individuals to the JS Course. The function should take a name parameter and output the message "Hello [name], Welcome to JS Course". Call the function twice, once with the argument "vinod" and once with the argument "ram".
// function greet(name){
// console.log("Hello " + name + ", Welcome to JS Course");
// }
// greet("Vanshika");
//! 1. Write a function to find the sum of two numbers with parameters.
// function sum(a,b){
// console.log(a+b);
// }
// sum(5,19);
//* ==============================
//* Function expressions
//* ==============================
//? A function expression is a way to define a function as part of an expression. It can be either named or anonymous. If it's named, it becomes a named function expression.
// var result = function sum(a,b){
// console.log(a+b);
// };
// result(10,15);
//* ==============================
//* Anonymous Function
//* =============================
//? An anonymous function is a function without a name. It can be created using either a function expression or a function declaration without a specified name.
// var result = function (a,b){
// console.log(a+b);
// };
// result(10,15);
//* ==============================
//* Return Keyword
//* =============================
//? Return Keyword: In JavaScript, the return statement is used within a function to specify the value that the function should produce or provide back to the code that called it. The return statement stops the execution of a function and sends a value back to the caller
//? Syntax
// return expression;
//! Example 1: Returning a Sum of two number
// function sum(a, b) {
// return a + b;
// }
// var result = sum(5,5);
// console.log(result);
//* ==============================
//* IIFE - immediately invoked function expression
//* =============================
//? An IIFE, or Immediately Invoked Function Expression, is a JavaScript function that is defined and executed immediately after its creation. It is a way to create a self-contained block of code that doesn't interfere with the surrounding code and executes immediately
// Syntax
// (function () {
// // code to be executed
// })();
// var result = (function (a,b){
// console.log(a+b);
// })(5,10);
// !Practice Time ( IIFE with Parameters)
//? Interview Questions
//! Question 1: Calculator Function
//! Write a JavaScript function calculator that takes two numbers and an operator as parameters and returns the result of the operation. The function should support addition, subtraction, multiplication, and division.
// const calculator = (num1, num2, operator) => {
// let result;
// switch (operator) {
// case "+":
// return num1 + num2;
// case "-":
// result = num1 - num2;
// return result;
// case "*":
// result = num1 * num2;
// return result;
// case "/":
// if (num2 === 0) {
// return "0 is not allowed";
// } else {
// result = num1 / num2;
// return result;
// }
// default:
// return "no operator found";
// }
// };
// console.log(calculator(5,2,"+"));
// console.log(calculator(5,2,"-"));
// console.log(calculator(5,0,"/"));
//! Reverse a String:
//! Write a function to reverse a given string without using built-in reverse methods.
// const isReverse = (str) => {
// let reverse = "";
// for (let char = str.length - 1; char >= 0; char--) {
// reverse = reverse + str[char];
// }
// return reverse;
// };
// console.log(isReverse("vanshika thesiya"));
//! Palindrome Check:
//! Create a function to determine if a given string is a palindrome (reads the same backward as forward).
// const isPalindrome = (str) => {
// let reverse = "";
// for (let char = str.length - 1; char >= 0; char--) {
// reverse = reverse + str[char];
// }
// // if (str === reverse) {
// // return true;
// // } else {
// // return false;
// // }
// return str === reverse ? true : false;
// };
// console.log(isPalindrome("level"));