-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path42Function.js
98 lines (78 loc) · 2.05 KB
/
42Function.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
// Function
// Function Declaration
// function singHappyBirthday(){
// console.log("happy birthday to you ......");
// }
// singHappyBirthday(); // Calling Function
// Function Expression
const singHappyBirthday = function(){
console.log("happy birthday to you ....");
}
singHappyBirthday();
// dry--> don't repeat yourself
// Two Number Sum
// function declaration
// function sumTwoNumbers(number1, number2){
// return number1+number2;
// }
// const returnedValue = sumTwoNumbers(4,6);
// console.log(returnedValue);
// Function Expresstion
const sumTwoNumbers = function(number1, number2){
return number1+number2;
}
const ans1 = sumTwoNumbers(9,7);
console.log(ans1);
// adding "undefined" to a number => NaN(Not a Number)
// 3 + 4 + undefined => NaN
// Check The Given No Is Even Or Not.
// function decraltion
// ##1 isEven
// function isEven(number){
// if(number%2===0){
// return true;
// }else{
// return false;
// }
// }
// console.log(isEven(5))
// ##2 Another Way
function isEven1(num1){
if(num1%2===0){
return true;
}
return false;
}
console.log(isEven1(8));
// ##3 shortest way of Checking A even No
function isEven2(nums){
return nums%2===0;
}
console.log(isEven2(9));
// expresstion
const isEven3 = function(no){
return no%2===0;
}
console.log(isEven3(10))
// create function which take string as input
// function firstChar(anyString){ // function decralation
const firstChar = function(anyString){ // function expresstion
return anyString[0];
}
console.log(firstChar("abc"));
// create function
// input: array, target(number)
//output: index of target if target present in array
// function findTarget(array, target){ // normal function declaration
// Binary Search
const findTarget = function(array, target){ // function expretion
for(let i=0; i<array.length; i++){
if(array[i]===target){
return i;
}
}
return -1;
}
const myArray = [1,3,8,6,45,90];
const ans = findTarget(myArray,45);
console.log(ans);