- 20th Mar 2020
- 03:35 am
- Adan Salman
JavaScript Assignment Question & Solution - These are all recursive functions that have to be written in javascript. The comments regarding the problem definition are written inside the function.
QUESTIONS
function recursiveBinarySearch(arr, element){ // search through the array recursively for the element // you may need to add more parameters to this function // if the element is not found, return -1 // if the element is found, return the index at which it was found
}
function quickSort(arr) { // QUICKSORT recursive function }
Function coinFlips(num){ // this function returns an array of all possible outcomes from flipping a coin N times // Input type: Integer // For example, coinFlips(2) would return the following: // H stands for Heads and T stands for tails // Represent the two outcomes of each flipo as “H” or “T” }
Function letterCombinations(arr, index = 0){ // This function returns an array of all combinations of the given letters // Input type: Array of single characters // For example, letterCombinations([“a”, “b”, “c”]) would return the following: // [“a”, “b”, “c”, “ab”, “ac”, “ba”, “bc”, “ca”, “cb”, “abc”, “acb”, “bac”, “bca”, “cab”, “cba”] }
SOLUTION
// Q1 solution
function recursiveBinarySearch(arr, element,start=0,end=arr.length-1){
if(end piv) {
j--;
}
if (i <= j) {
swap(arr, i, j);
i++;
j--;
}
}
return i;
}
function quickSort(arr, l=0, r=arr.length-1) {
var ind;
if (arr.length > 1) {
ind = partition(arr, l, r);
if (l < ind - 1) {
quickSort(arr, l, ind - 1);
}
if (ind < r) {
quickSort(arr, ind, r);
}
}
return arr;
}
var arr = [4,2,8,9,3,5,2,4];
console.log(quickSort(arr));
//Q3 solution
function coinFlips(n){
if(n==1){
return ['H','T'];
}
else{
var r=coinFlips(n-1);
var p=[];
for(var i=0;i
Get the best javascript assignment help from our qualified experts. Order now