-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge-array-while.html
53 lines (48 loc) · 1.15 KB
/
merge-array-while.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Merge Two Array Using while loop</title>
<style>
h1 {
background-color: gold;
padding: 10px;
border-radius: 4px;
display: inline-block;
font-family: Arial, Helvetica, sans-serif;
}
</style>
</head>
<body>
<h1>Merge Two Array Using while loop</h1>
<!-- script tag here -->
<script>
// Create Three arrays
let arr1 = [1, 2, 3, 4, 5, 6, 7, 8];
let arr2 = ["a", "b", "c", "d", "e", "f", "g", "h", "i"];
let arr3 = [];
let a1 = 0;
let a2 = 0;
let a3 = 0;
while(a1 < arr1.length && a2 < arr2.length) {
if (arr1[a1] < arr1[a2]) {
arr3[a3] = arr1[a1];
a1++;
} else {
arr3[a3] = arr2[a2];
a2++;
}
a3++
}
// Copy any remaining elements from arr1 to arr3
while(a1 < arr1.length) {
arr3[a3] = arr1[a1];
a1++;
a3++
}
// Log the merged array to the console
console.log(arr3);
</script>
</body>
</html>