settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Binary Logarithm as JavaScript function

+3 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
edited by user golearnweb

1 Answer

+2 votes

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
...