settingsAccountsettings
Menusettings

Q: Binary Logarithm as JavaScript function

+2 votes

I need to know how to write JavaScript (JS) function that prints the binary logarithm for each number in the input.
The input comes as an array of number elements.

Here is what is binary logarithm: https://en.wikipedia.org/wiki/Binary_logarithm

Examples:

Input:
1024
1048576
256
1
2

Output:
10
20
8
0
1


The output should be printed to the console, on a new line for each number.

asked in JavaScript category by user nikole

1 Answer

+1 vote

Interesting js task! Here's how I solved it:

function binaryLogarithm(input) {
    for (let x of input) {
        console.log(Math.log2(x));
    }
}

console.log(binaryLogarithm(["1024", "1048576", "256","1","2"]));
answered by user icabe
...