Created
May 26, 2020 14:43
-
-
Save quannt/c19a4f00b4def91af8ca8fb4222c94bf to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/togezul
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
// https://www.interviewcake.com/question/javascript/merge-sorted-arrays?course=fc1§ion=array-and-string-manipulation | |
"use strict"; | |
var myArray = [3, 4, 6, 10, 11, 15, 16, 18]; | |
var alicesArray = [1, 5, 8, 12, 14, 16, 18, 19, 21, 22, 23, 78, 89, 90, 93, 94, 95]; | |
function mergeArrays(myArray, alicesArray) { | |
var headOfMyArray = myArray[0]; | |
var headOfAlicesArray = alicesArray[0]; | |
var sortedArray = []; | |
var myArrayCursor = 0; | |
var alicesArrayCursor = 0; | |
var totalLength = alicesArray.length + myArray.length; | |
for (var i = 0; i < totalLength; i++) { | |
if (myArray[myArrayCursor] <= alicesArray[alicesArrayCursor]) { | |
if (!myArray[myArrayCursor]) continue; | |
sortedArray.push(myArray[myArrayCursor]); | |
myArrayCursor++; | |
} else { | |
if (!alicesArray[alicesArrayCursor]) continue; | |
sortedArray.push(alicesArray[alicesArrayCursor]); | |
alicesArrayCursor++; | |
} | |
} | |
return sortedArray; | |
} | |
var result = mergeArrays(myArray, alicesArray); | |
console.log(result); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">// https://www.interviewcake.com/question/javascript/merge-sorted-arrays?course=fc1§ion=array-and-string-manipulation | |
const myArray = [3, 4, 6, 10, 11, 15, 16, 18]; | |
const alicesArray = [1, 5, 8, 12, 14, 16, 18, 19, 21,22,23,78,89,90,93,94,95]; | |
function mergeArrays(myArray, alicesArray) { | |
const headOfMyArray = myArray[0] | |
const headOfAlicesArray = alicesArray[0] | |
const sortedArray = [] | |
let myArrayCursor = 0 | |
let alicesArrayCursor = 0 | |
const totalLength = alicesArray.length + myArray.length | |
for (let i = 0; i < totalLength; i++) { | |
if (myArray[myArrayCursor] <= alicesArray[alicesArrayCursor]) { | |
if (!myArray[myArrayCursor]) continue | |
sortedArray.push(myArray[myArrayCursor]) | |
myArrayCursor++ | |
} else { | |
if (!alicesArray[alicesArrayCursor]) continue | |
sortedArray.push(alicesArray[alicesArrayCursor]) | |
alicesArrayCursor++ | |
} | |
} | |
return sortedArray | |
} | |
const result = mergeArrays(myArray, alicesArray) | |
console.log(result) | |
</script></body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.interviewcake.com/question/javascript/merge-sorted-arrays?course=fc1§ion=array-and-string-manipulation | |
"use strict"; | |
var myArray = [3, 4, 6, 10, 11, 15, 16, 18]; | |
var alicesArray = [1, 5, 8, 12, 14, 16, 18, 19, 21, 22, 23, 78, 89, 90, 93, 94, 95]; | |
function mergeArrays(myArray, alicesArray) { | |
var headOfMyArray = myArray[0]; | |
var headOfAlicesArray = alicesArray[0]; | |
var sortedArray = []; | |
var myArrayCursor = 0; | |
var alicesArrayCursor = 0; | |
var totalLength = alicesArray.length + myArray.length; | |
for (var i = 0; i < totalLength; i++) { | |
if (myArray[myArrayCursor] <= alicesArray[alicesArrayCursor]) { | |
if (!myArray[myArrayCursor]) continue; | |
sortedArray.push(myArray[myArrayCursor]); | |
myArrayCursor++; | |
} else { | |
if (!alicesArray[alicesArrayCursor]) continue; | |
sortedArray.push(alicesArray[alicesArrayCursor]); | |
alicesArrayCursor++; | |
} | |
} | |
return sortedArray; | |
} | |
var result = mergeArrays(myArray, alicesArray); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment