I removed the need for the search function by using default parameters in the binarySearch function.
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.
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.11let februaryBill = 50.21let marchBill = 48.92let aprilBill = 62.36let mayBill = 54.64let juneBill = 49.30let julyBill = 61.93let augustBill = 68.54let septemberBill = 71.77let octoberBill = 70.28let novemberBill = 59.56let 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);
- }