-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge-2_array-simple.html
44 lines (42 loc) · 1.18 KB
/
merge-2_array-simple.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
<!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</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</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 = [];
function mergeArrays(arr1, arr2, arr3) {
// Using Two loop for merge arrays
for (let i = 0; i < arr1.length; i++) {
arr3[i] = arr1[i];
}
for (let i = 0; i < arr2.length; i++) {
arr3[arr1.length + i] = arr2[i];
}
console.log(arr3);
}
// Call the function to test it works correctly with arrays
mergeArrays(arr1, arr2, arr3);
// shortcut function merge two arrays
const arrThree = [...arr1, ...arr2];
console.log(arrThree);
</script>
</body>
</html>