I was trying to create a binary to decimal converter without using parseInt ()
Heres my code :
var inp = `110010`;
var len = inp.length;
var string = inp.split("");
var counter = string.map(function(val,i){
return i;
}).reverse();
var storeBin = string.map(function(val,i){
let x ="";
if(val>0){
x += 2;
}else if(val===0){
x += 0;
}
return Math.pow(string[i]*x,counter[i]);
});
var dec=0; /* output */
for(i=0;i<len;i++){
dec += storeBin[i]
}
console.log("Entered binary "+inp);
console.log("Decimal "+dec);
When I run it:
Input: 1010
Output: Entered binary 1010
Decimal 11
But the output of binary 1010 should be 10 now 11 can someone please help me find the issue in this code