I'm working on a piece of code to turn text to binary. First, I turn the text to decimal, then the decimal to binary. The problem I'm getting, is in the 'decimalToBinary' function, it is telling me "Uncaught TypeError: Cannot read property 'toString' of undefined", could someone explain what's wrong?
function start() {
var text = readLine("Input the string you would like to encode: ");
var binary = textToBinary(text);
println(binary);
}
function textToBinary(text) {
var toASCII = [];
var toBINARY = [];
text.toUpperCase();
for (var i = 0; i < text.length; i++) {
var ASCII_CODE = text.charCodeAt(i);
toASCII.push(ASCII_CODE);
}
for (var j = 0; j < toASCII.length; i++) {
var arrnum = toASCII[i]
var final = decimalToBinary(arrnum);
toBINARY.push(final);
}
return toBINARY;
}
function decimalToBinary(decimalValue) {
var binaryBase = 2;
var numBitsDesired = 8;
var binaryValue = decimalValue.toString(binaryBase);
while (binaryValue.length < numBitsDesired) {
binaryValue = "0" + binaryValue;
}
return binaryValue;
}