-3
var y;
function deci(y) {
  var res = "";

  while (y != 1) {
    res = res + y % 2;
    y = y / 2;
  }

  return (1 + res);
}
Ele
  • 33,468
  • 7
  • 37
  • 75
  • 1
    Please post a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Ele Mar 17 '18 at 19:30
  • This to me looks like an infinite loop. Try to modify the condition to y >= 1 (edited recursion to loop) – balsick Mar 17 '18 at 19:30
  • 2
    Yeah, there's an obvious infinite loop here for where `y` is anything other than a power of two. – lonesomeday Mar 17 '18 at 19:32
  • and it happens when i call function deci(y) with argument y=3, i.e, deci(3) – shahbaz ansari Mar 17 '18 at 19:32
  • I don't want to have a go at fixing this, as I can't even work out what it's *trying* to do. You need to give an example of expected input and the expected output. – lonesomeday Mar 17 '18 at 19:34
  • @balsick thank you I got it. It's a infinite loop because javascript takes number variables as float not integer. – shahbaz ansari Mar 17 '18 at 19:37
  • 1
    Infinite loop . It works properly only when the input of function deci() must be (2 pow x) where x > 0 like 1,2,3...n – Sudarshan Mar 17 '18 at 19:38

4 Answers4

0

What you could do

  • change the loop to a do ... while loop, because it loops at least one time, for zero values,

  • switch the assignment of res to res = y % 2 + res;, because it has the right order now,

  • take the integer value of the divided value for the next loop,

  • check the value and go on with non zero values.

function deci(y) {
    var res = "";
    do {
        res = y % 2 + res;
        y = Math.floor(y / 2);
    } while (y)
    return res;
}

console.log (deci(0));
console.log (deci(1));
console.log (deci(2));
console.log (deci(3));
console.log (deci(4));
console.log (deci(5));
console.log (deci(13));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
-1

This to me looks like an endless loop. Try to modify the condition to y >= 1

var y;
function deci(y) {
  var res = "";

  while (y >= 1) {
    res = res + y % 2;
    y = y / 2;
  }

  return (1 + res);
}
balsick
  • 1,099
  • 1
  • 10
  • 23
  • [this is completely broken](https://repl.it/repls/AvariciousVastSoftwareengineer) – Mulan Mar 17 '18 at 19:56
  • @naomik I don't care that the algorithm doesn't return the proper result. Who posted the question asked how to make it work: I helped him to make it work, now it's his responsibility to make it work as expected. – balsick Mar 18 '18 at 11:50
-1

Three problems

  • global y is bad practice – you're shadowing it with a local y so I'm guessing you didn't intend to do this
  • Your while condition is broken – repeatedly dividing a number by 2 will not always reach 1
  • JavaScript performs floating point division, so you need to truncate the decimal

This is probably what you meant to write

const deci = y =>
{
  let res = ''
  while (y >= 2)
  {
    res = res + String (y % 2)
    y = y / 2 >> 0
  }
  return res + String (y)
}
    
console.log (deci (0))
console.log (deci (1))
console.log (deci (2))
console.log (deci (3))
console.log (deci (4))
console.log (deci (5))

Here's another way you can write the program

const deci = y =>
  y < 2
    ? String (y)
    : deci (y / 2 >> 0) + String (y % 2)
    
console.log (deci (0))
console.log (deci (1))
console.log (deci (2))
console.log (deci (3))
console.log (deci (4))
console.log (deci (5))

A default parameter value and better name make this function a little nicer to read, imo

const decimalToBinary = (n = 0) =>
  n < 2
    ? String (n)
    : decimalToBinary (n / 2 >> 0) + String (n % 2)

As @NinaScholz points out, you can perform n / 2 >> 0 in a single operation n >> 1 - the function is further improved

const decimalToBinary = (n = 0) =>
  n < 2
    ? String (n)
    : decimalToBinary (n >> 1) + String (n % 2)

Alternatively, we could make 2 a parameter of our function to create a generic base converter

const decimalToBase = (n = 0, base = 2) =>
  n < base
    ? String (n)
    : decimalToBase (n / base >> 0) + String (n % base)
    
const decimalToBinary = (n = 0) =>
  decimalToBase (n, 2)
  
const decimalToOctal = (n = 0) =>
  decimalToBase (n, 8)
  
console.log (decimalToBinary (0), decimalToOctal (0)) // 0     0
console.log (decimalToBinary (1), decimalToOctal (1)) // 0     1
console.log (decimalToBinary (2), decimalToOctal (2)) // 10    2
console.log (decimalToBinary (7), decimalToOctal (7)) // 111   7
console.log (decimalToBinary (8), decimalToOctal (8)) // 1000  10
console.log (decimalToBinary (9), decimalToOctal (9)) // 1001  11
Mulan
  • 129,518
  • 31
  • 228
  • 259
  • 1
    This answer doesn't explain anything! – Ele Mar 17 '18 at 19:36
  • 3
    This is a very confusing answer, introducing all kinds of syntax that the poster is unlikely to understand and which you haven't explained. And it omits the vital information that the correct answer is really `return y.toString(2)`. – lonesomeday Mar 17 '18 at 19:42
  • @lonesomeday I like how you point out the "correct" answer after admitting you couldn't make out what the asker was trying to do, while simultaneously admitting this answer confuses you. Maybe you need to take some time to read? – I think the question is less about number conversion and more about how to write your own loops; `y.toString(2)` teaches the learner nothing in this context – Mulan Mar 17 '18 at 20:03
  • why not take a right shift with one? the number is already converted to 32 bit by using right shift with zero for getting an integer. – Nina Scholz Mar 17 '18 at 20:09
  • @NinaScholz I was leaving the `/ 2` in place because it was a little more explicit, and also leaves the possibility for `2` to be a variable - `n >> 1` only works in this particular case. Thanks for comment as always :D – Mulan Mar 17 '18 at 20:14
  • @naomik I meant that it was the correct way of doing what you have the loop doing. I think you're correct, but I must admit I wouldn't have worked it out for myself. The syntax in your answer doesn't confuse *me*, but I'm pretty familiar with JS syntax. – lonesomeday Mar 17 '18 at 21:07
-1

When you call deci(3), y will never become 1, so you will get an infinite loop. The only inputs that does not result in an infinite loop are powers of 2 (1, 2, 4, 8, 16, ...)

To figure out what's going on, add a console.log call inside the while loop.

function deci(y) {
  console.log('converting', y, 'to binary')
  var res = '1'
  while (y != 1) {
    res += y % 2
    y = y / 2
    console.log('y is', y)
    // let's throw an error after a while to avoid freezing the computer
    if (y < 0.001) throw "infinite loop"
  }
  console.log('the result is', res)
}
deci(64)  // this works fine
deci(3)   // this ends up in an infinite loop

In general do not use =! and == with floating point numbers. Since even when your logic seems correct, there might be rounding errors that you would not get with decimal numbers. Is floating point math broken?

To convert a number to base 2, you can use number.toString(2)

function deci(n) {
  return n.toString(2)
}
console.log(deci(3)) // -> '11'
Håken Lid
  • 22,318
  • 9
  • 52
  • 67