Home > Net >  Why is there a double quote at the beginning of the my result?
Why is there a double quote at the beginning of the my result?

Time:01-26

I have tried looking for this problem here but still can't find an answer (maybe I'm too newbie to even write the exact keywords). I am trying to overcome freecodecamp challenge on "binary agent", my question is not "How to convert binary to text string" But

why is there a double quote at the beginning of the my result ?

Thank You

    function binaryAgent(str) {
  let binaryArr;
  let decimalArr = [];
  let textArr = [];
  let joinTextArr;
  for (let i=0; i<str.length; i  ){
    // convert binary to decimal use parseInt(value, radix)
    binaryArr = str.split(" ");
    decimalArr.push(parseInt(binaryArr[i], 2));
    decimalArr = decimalArr.filter( value => !Number.isNaN(value) );
    // convert decimal to text
    textArr.push(String.fromCharCode(decimalArr[i]))
    // join to be a string
    joinTextArr = textArr.join("")
  }
  return joinTextArr;
}

console.log(binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111"));

// result > "Aren't bonfires fun!?

CodePudding user response:

Explanation

All Javascript strings will have double quotes at the beginning when printed. Hence, the problem isn't that there is a double quote printed at the start of your console log output. Rather, the issue is that there isn't a double quote printed at the end of your logged string. In fact, the end double quote is indeed printed at the end of your logged string, but is prefixed by a long string of undefined characters, which might explain why you might have missed it:

enter image description here

The underlying issue is because you are iterating through the individual characters of the string in

for (let i=0; i<str.length; i  )

This results in your index i going out of bounds of binaryArr. Thus, textArr is appended with undefined variables, leading to the actual string being printed as the final textArr to be (note the ending trail of undefined characters)

["A", "r", "e", "n", "'", "t", " ", "b", "o", "n", "f", "i", "r", "e", "s", " ", "f", "u", "n", "!", "?", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]

Fix

Rather, you want to split the input string into the individual binary words before iterating through in your for loop.

  function binaryAgent(str) {
    let binaryArr = str.split(" ");
    let decimalArr = [];
    let textArr = [];
    let joinTextArr;
    for (let i=0; i<binaryArr.length; i  ){
      // convert binary to decimal use parseInt(value, radix)
      decimalArr.push(parseInt(binaryArr[i], 2));
      decimalArr = decimalArr.filter( value => !Number.isNaN(value) );
      // convert decimal to text
      textArr.push(String.fromCharCode(decimalArr[i]))
      // join to be a string
    }
    joinTextArr = textArr.join("")

    return joinTextArr
}

Also, I included a small optimization that only calculates joinTextArr at the end of the loop, since that only has to be done once.

  •  Tags:  
  • Related