Home > Mobile >  Error when trying to convert binary to decimal (no output)
Error when trying to convert binary to decimal (no output)

Time:09-18

I am trying to write a function that must convert a decimal number to binary and vice versa.

The function receives two arguments:

  • number, either binary/decimal
  • conversion to perform

Works fine when I pass binaryDecimal(5, 2); (// prints 101) for decimal to binary conversation.

When I pass the arguments to the function to convert binary to decimal, it does not print anything.

const binarioDecimal = (number = 0, base = 0) => { // 0 by default if the user does not pass any value
  if (number === 0 || base === 0) {
    console.log(0);
  } else if (typeof number === "number" && typeof base === "number") {
    if (base === 2) {
      let num = number;
      let binary = (num % 2).toString();

      for (; num > 1; ) {
        num = parseInt(num / 2);
        binary = (num % 2)   binary;
      }
      console.log(binary);
    }
  } else if (typeof number === "number" && typeof base === "number") {
    //this is where i think the function fails
    if (base === 10) {
      var decimal = 0,
        i = 0,
        resto;
      while (number !== 0) {
        resto = number % 10;
        number = Number.parseInt(number / 10);
        decimal = decimal   resto * Math.pow(2, i);
          i;
      }
      console.log(decimal);
    }
  }
};
binarioDecimal(); // 0
binarioDecimal(23, 2); // 10111
binarioDecimal(101, 10); //does not print anything :(

CodePudding user response:

What if you split the checks into two separate conditions?

const binarioDecimal = (number = 0, base = 0) => { 

  if (number === 0 || base === 0) {
    console.log(0);
  } 

  if (base === 2) {
    var num = number;
    var binary = (num % 2).toString();

    for (; num > 1; ) {
      num = parseInt(num / 2);
      binary = (num % 2)   binary; 
    }
    console.log(binary); 
  }

  if (base === 10) {
    var decimal = 0,
        i = 0,
        resto;
    while (number !== 0) {
      resto = number % 10;
      number = Number.parseInt(number / 10);
      decimal = decimal   resto * Math.pow(2, i);
        i;
    }
    console.log(decimal); 
  }
}
binarioDecimal(); // 0
binarioDecimal(23, 2); // 10111
binarioDecimal(101, 10); // 5

CodePudding user response:

The second

else if (typeof number === "number" && typeof base === "number")

is never executed.

Maybe try something like:

else if (typeof number === "number" && typeof base === "number" && base === 2)
{
    ...
}
else if (typeof number === "number" && typeof base === "number" && base === 10)
{
   ...
}

if you see what I mean!

CodePudding user response:

The problem appears to be in your outermost if() statement. You have the following:

if(number === 0 || base === 0) {
 /* your code */
} else if(typeof number === "number" && typeof base === "number") {
 if(base === 2) { /* your code */ }
} else if(typeof number === "number" && typeof base === "number") {
 if(base === 10) { /* your code */ }
}

Using this, if you call binarioDecimal(101, 10);:

if(number === 0 || base === 0)

is false

else if(typeof number === "number" && typeof base === "number")

is true

then if(base === 2)

is false

It then exits the whole statement, assuming it has fulfilled its purpose, never reaching the third else if(...) because it's the same as the previous one.

Putting the if(base === 10) with the if(base === 2) statement, that should resolve the issue.

if(number === 0 || base === 0) {
 /* your code */
} else if(typeof number === "number" && typeof base === "number") {
 if(base === 2) {
  /* your code */
 } else if(base === 10) {
  /* your code */
 }
}

That should solve why your code is never reached when the base is 10. Alternatively, outside of doing so for a coding exercise, you may want to look at Number.prototype.toString(radix); and Number.parseInt(string, radix); to convert between number bases. Hopefully this information is useful!

CodePudding user response:

Hi please find the code snippet as below

const binarioDecimal = (number = 0, base = 0) => {
  // 0 by default if the user does not pass any value
  if (number === 0 || typeof number === number) {
    if (base === 0) {
    }
    return 0;
  } else if (base === 2) {
    let num = number;
    let binary = (num % 2).toString();

    for (; num > 1; ) {
      num = parseInt(num / 2);
      binary = (num % 2)   binary;
    }
    console.log(binary);
  } else if (base === 10) {
    var decimal = 0,
      i = 0,
      resto;
    while (number !== 0) {
      resto = number % 10;
      number = Number.parseInt(number / 10);
      decimal = decimal   resto * Math.pow(2, i);
        i;
    }
    console.log(decimal);
  }
};

binarioDecimal(); // 0
binarioDecimal(23, 2); // 10111
binarioDecimal(101, 10); //does not print

  • Related