Login
Order Now
Support
theprogrammingassignmenthelp

RECURSIVE FUNCTIONS TO BE WRITTEN IN JAVASCRIPT

  • 20th Mar, 2020
  • 16:05 PM

// THESE ARE ALL RECURSIVE FUNCTIONS THAT HAVE TO BE WRITTEN IN JAVASCRIPT // 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<start){
    return -1;
  }
  var k=Math.floor((start+end)/2);
  if(arr[k]==element){
    return k;
  }
  if(element<arr[k]){
    return recursiveBinarySearch(arr,element,start,k-1);
  }
  else{
    return recursiveBinarySearch(arr,element,k+1,end);
  }
}
let arr = [5,2,6,9,10,46,89,357]; 
let x = 6; 
console.log(recursiveBinarySearch(arr,x));

//Q2 solution
function swap(arr, l, r){
    var temp = arr[l];
    arr[l] = arr[r];
    arr[r] = temp;
}
function partition(arr, l, r) {
    var piv   = arr[Math.floor((r + l) / 2)]; 
        i       = l;
        j       = r;
    while (i <= j) {
        while (arr[i] < piv) {
            i++;
        }
        while (arr[j] > 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<r.length;i++){
      if(p.indexOf(r[i]+"H")==-1){
        p.push(r[i]+"H");
      }
      if(p.indexOf(r[i]+"T")==-1){
        p.push(r[i]+"T");
      }
      if(p.indexOf("H"+r[i])==-1){
        p.push("H"+r[i]);
      }
      if(p.indexOf("T"+r[i])==-1){
        p.push("T"+r[i]);
      }
    }
    return p;
  }
}
console.log(coinFlips(3))

//Q4 solution
function letterCombinations(arr,index=0){
  if(index==arr.length-1){
    var t=[];
    for(var i=0;i<arr.length;i++){
      t.push(arr[i]);
    }
    return t;
  }
  let r=letterCombinations(arr,index+1);
  let p=r[r.length-1].length;
  let k=r.length-1;
  while(true){
    if(k<0){
      break;
    }
    if(r[k].length!=p){
      break;
    }
    for(var j=0;j<arr.length;j++){

      if(r[k].indexOf(arr[j])==-1){
        r.push(r[k]+arr[j]);
      }
    }
    k=k-1;
  }
  return r;
}
var arr=['a','b','c','d']
console.log(letterCombinations(arr))

 

Get the best javascript assignment help from our qualified experts. Order now

Share this post