Ad

I removed the need for the search function by using default parameters in the binarySearch function.

Code
Diff
  • function binarySearch(numbers, element, left = 0, right = numbers.length-1) {
    //time complexity is log(n) it excecutes very fast than linear search  n=arraylength;
      if(left>right) return -1;
      
      let pivot =Math.floor((left+right)/2)
      
      if(numbers[pivot]===element) return pivot
      
       if(numbers[pivot]>element) return binarySearch(numbers,element,left,pivot-1)
      
        return binarySearch(numbers,element,pivot+1,right)
    }
    • function binarySearch(numbers, element) {
    • function binarySearch(numbers, element, left = 0, right = numbers.length-1) {
    • //time complexity is log(n) it excecutes very fast than linear search n=arraylength;
    • return search(numbers,element,0,numbers.length-1)}
    • function search(numbers,element,left,right){
    • if(left>right) return -1;
    • let pivot =Math.floor((left+right)/2)
    • if(numbers[pivot]===element) return pivot
    • if(numbers[pivot]>element) return search(numbers,element,left,pivot-1)
    • return search(numbers,element,pivot+1,right)
    • if(numbers[pivot]>element) return binarySearch(numbers,element,left,pivot-1)
    • return binarySearch(numbers,element,pivot+1,right)
    • }

I simplified the process of adding the monthly costs by putting all of the variables in an object and using the reduce method.

Code
Diff
  • const yearlyElectricCosts = () => {
      // Create 11 more variables with the correct values
      const monthlyCosts = {
       januaryBill : 43.11,
       februaryBill : 50.21,
       marchBill : 48.92,
       aprilBill : 62.36,
       mayBill : 54.64,
       juneBill : 49.30,
       julyBill : 61.93,
       augustBill : 68.54,
       septemberBill : 71.77,
       octoberBill : 70.28,
       novemberBill : 59.56,
       decemberBill : 62.04
      }
      // Decide on a variable name and add all the monthly charges
      const totalCharges = Object.values(monthlyCosts).reduce((acc, curr) => acc + curr);
      
      // Return the value of the variable by typing it in after the `return` keyword below
      return +totalCharges.toFixed(2);
    }
    • const yearlyElectricCosts = () => {
    • // Create 11 more variables with the correct values
    • let januaryBill = 43.11
    • let februaryBill = 50.21
    • let marchBill = 48.92
    • let aprilBill = 62.36
    • let mayBill = 54.64
    • let juneBill = 49.30
    • let julyBill = 61.93
    • let augustBill = 68.54
    • let septemberBill = 71.77
    • let octoberBill = 70.28
    • let novemberBill = 59.56
    • let decemberBill = 62.04
    • const monthlyCosts = {
    • januaryBill : 43.11,
    • februaryBill : 50.21,
    • marchBill : 48.92,
    • aprilBill : 62.36,
    • mayBill : 54.64,
    • juneBill : 49.30,
    • julyBill : 61.93,
    • augustBill : 68.54,
    • septemberBill : 71.77,
    • octoberBill : 70.28,
    • novemberBill : 59.56,
    • decemberBill : 62.04
    • }
    • // Decide on a variable name and add all the monthly charges
    • const totalCharges = januaryBill + februaryBill + marchBill + aprilBill + mayBill + juneBill + julyBill + augustBill + septemberBill + octoberBill + novemberBill + decemberBill
    • const totalCharges = Object.values(monthlyCosts).reduce((acc, curr) => acc + curr);
    • // Return the value of the variable by typing it in after the `return` keyword below
    • return +totalCharges.toFixed(2)
    • return +totalCharges.toFixed(2);
    • }