Tell us what’s happening:
Describe your issue in detail here.
**Your code so far**
function binaryAgent(str) {
const arrBasica = str.split(" ");
// console.log(arrBasica);
let arrValor = [];
let arrLetra = [];
for (var i = 0; i < arrBasica.length; i++)
{
let x = convierte(arrBasica[i]);
arrValor.push(x);
arrLetra.push(String.fromCharCode(x));
}
//console.log("punto 1 " + arrValor + " " + arrLetra);
let str1 = arrLetra.join("");
console.log(str1);
return str;
}
function convierte(numeraco) {
//console.log(numeraco);
let resulta = 0;
let arr = [];
for (var x = numeraco.length; x > 0; x--)
{ arr.push(numeraco.substr(x - 1,1)); }
// console.log(arr);
for (var i = 0; i < arr.length; i++)
{
if (arr[i] === '1') {resulta += Math.pow(2,i); }
}
// console.log(arr);
// console.log(resulta);
return resulta;
}
binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111");
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36
Challenge: Binary Agents
Link to the challenge:
If I remove the console.log inside the function and just look at the return value:
console.log('return value is', binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111"));
I get back:
return value is 01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111
So, you may be getting the right answer inside the function, but you aren’t returning the right answer, which is what matters.
ilenia
October 22, 2021, 5:52pm
4
What you see and what you return are different
Thanks, Ilenia, I solved it early this morning.
Regards.
josebenitez:
function convierte(numeraco) {
//console.log(numeraco);
let resulta = 0;
let arr = [];
for (var x = numeraco.length; x > 0; x--)
{ arr.push(numeraco.substr(x - 1,1)); }
// console.log(arr);
for (var i = 0; i < arr.length; i++)
{
if (arr[i] === '1') {resulta += Math.pow(2,i); }
}
// console.log(arr);
// console.log(resulta);
return resulta;
}
This is a great solution for converting the binary string into a decimal number.
But you could greatly simplify your answer by using the parseInt()
function instead. Just letting you know!
parseInt("01000001", 2) // 65
system
Closed
April 23, 2022, 5:25pm
7
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.