Would you pass this JavaScript interview question?

preview_player
Показать описание
Recommended reading:

Рекомендации по теме
Комментарии
Автор

Very nice answer from the Google developer, thanks for sharing.

chems
Автор

How about this one?

function toBinary(num) {
if (num === 0) {
return "";
}

return toBinary(Math.floor(num / 2)) + (
num % 2 == 1 ? "1" : "0"
);
}

irealtr
Автор

Seems like a fairly straightforward solution to me, I also came up with a similar answer myself. If this seems like such a surprising solution then maybe you just haven't used the % operator much? Anyway, I'd alter the countdown reassignment to remove the Math.floor as you already know the remainder:
countdown = (countdown - oneOrZero) / 2;
because you subtract the remainder this will always return an integer

GeneralYouri
welcome to shbcf.ru