-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWhile-Do-While-Loop.js
More file actions
63 lines (51 loc) · 1.18 KB
/
While-Do-While-Loop.js
File metadata and controls
63 lines (51 loc) · 1.18 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
// While loop
let var1=0
while(var1<=10){
console.log("current value of var1 is ",var1)
var1=var1+2
}
console.log("\n")
let arr=["string1","string2","string3","string4"]
let index=0;
while(index<arr.length){
console.log("element at index ",index," is ",arr[index])
index++;
}
// Do-While loop
console.log("\n")
let i=0
do{
console.log("val of i is",i)
i++
}while(i<=5);
// binary search using while loop in JS
console.log("\n")
const random=[2,3,5,8,9,12,15,20,23,26,27,30]
let low=0
let high=random.length-1
const search_element=9
while(low<=high){
let middle=Math.floor(low+(high-low)/2)
if(random[middle]==search_element){
console.log("search_element" ,search_element,"found at index ",middle," in random array",random," using binary search")
break
}
else if(random[middle]<search_element){
low=middle+1
}
else{
high=middle-1
}
}
//Linear search using while loop
console.log("\n")
let arr1=[2,1,4,7,5,9,10,8]
const element=10
let j=0
while(j<arr1.length){
if(arr1[j]==element){
console.log("element",element," found at index ",j," in arr1 ",arr1," using linear search")
break
}
j++
}