-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4.html
More file actions
42 lines (42 loc) · 1.19 KB
/
4.html
File metadata and controls
42 lines (42 loc) · 1.19 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function findMedianSortedArrays(nums1, nums2) {
let i = 0;
let j = 0;
let nums3 = [];
while (i < nums1.length && j < nums2.length) {
if(nums1[i] <= nums2[j]) {
nums3.push(nums1[i]);
i++;
} else if(nums1[i] > nums2[j]) {
nums3.push(nums2[j]);
j++;
}
}
if(i === nums1.length && j < nums2.length) {
nums3.push(...nums2.slice(j, nums2.length))
}
if(i < nums1.length && j === nums2.length) {
nums3.push(...nums1.slice(i, nums1.length))
}
let average;
if(nums3.length % 2 === 0) {
average = (nums3[(nums3.length / 2) - 1] + nums3[nums3.length / 2]) / 2
} else {
average = nums3[((nums3.length + 1) / 2) - 1]
}
return average.toFixed(1)
}
// console.log(findMedianSortedArrays([5, 6, 7], [1, 2, 3, 4]))
// console.log(findMedianSortedArrays([1, 3], [2]))
console.log(findMedianSortedArrays([1, 2], [3, 4]))
</script>
</body>
</html>