Home > Software design >  How do I reverse a number in javascript using a for loop?
How do I reverse a number in javascript using a for loop?

Time:10-03

For a project, I am supposed to write a function in javascript that takes a value and converts it to binary (from decimal) and I need a step to be able to reverse the digits of a number (for the remainders) and I am unsure of how to do it, within the restrictions of using a loop.

(if theres any questions to explain my question, please let me know)

CodePudding user response:

Try this method ,

function DecToBinWithRev(num) {
  let result = Number(num).toString(2).split('').reverse().join('')
  return result
}


console.log(DecToBinWithRev(10))

CodePudding user response:

I m not sure if this will work or not..

var splitString = str.split(“”);
var revArray = splitString.reverse();
var joinArray = revArray.join(“”);

if u want to loop it over loop it with splitString.length & then followed by remainder logic inside loop

  • Related