Why can't I Import the math.js file?

1k Views Asked by At

I tried to write binary search in my code but it seems that everytime I tried to run it. It shows an error message (Uncaught ReferenceError: math is not defined) eventhough I clearly wrote import math.js file. I'm pretty sure that something went wrong somewhere in my code. Can anyone please explain to me that would be really helpful!

By the way, here's the code:

import math.js

var num = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
bilnum = num.length,
target = 13;
mid = math.floor((left + right)/2);

function search(){
    left = 0;
    right = num.length - 1;
    while(left <= right){
        if(num[mid] == target){
            return mid;
        }
        else if(target > mid){
            right = num[mid] - 1;
        }
        else{
            left = num[mid] + 1;
        }
        if(target === null){
            document.write("Number doesn't exist")
        }
        document.write("The target index is" + num[target])
    }
}

2

There are 2 best solutions below

0
Rohit On BEST ANSWER

two issues:

bilnum = num.length,

to

bilnum = num.length;

and

mid = math.floor((left + right)/2);

to:

mid = Math.floor((left + right)/2);
0
Aziz Bhiri On

Try Math.js (with uppercase M)