Reputation: 131
Web developer n00b here. I was trying to test my JavaScript function decryptStr
in the body like so:
<script type="text/javascript">
var str = "010510160";
document.write(decryptStr(str));
</script>
I ran it in Google Chrome and the JavaScript console said,
Uncaught Improper input; cannot decrypt.
I'm confused about what that means. Any idea what the problem is here? "Improper unit; cannot decrypt" is a string corresponding to a type of error the following function was supposed to catch in the event that the inputted string was not of the proper form:
function decryptStr(thisString)
{
var decryptInputError = "Improper input; cannot decrypt.";
thisString.trim();
var retString = "";
for (var i = 0, j = thisString.length; i < j; ++i)
{
var thisChar = thisString.charAt(i);
if (thisChar > '9' || thisChar < '0')
{
throw decryptInputError;
return;
}
else if (thisChar > '1' && thisChar < '10')
{
var nextChar = thisString.charAt(i+1);
if (nextChar < '0' || nextChar > '1')
{
throw decryptInputError;
return;
}
var thisSubtring = "";
for (var j = 0, k = parseInt(thisChar, 10); j < k; j++)
thisSubstring += nextChar;
retString += thisSubstring;
}
else
{
retString += thisChar;
}
}
if (!(retString.length % 8))
{
throw decryptInputError;
return;
}
for (var i = 0, j = retString.length; i < j; i += 8)
{
retString = "";
retString += parseInt(thisString.substr(i,8), 2);
}
return retString;
}
Upvotes: 0
Views: 51
Reputation: 1450
I believe this is because you throw the error and do not catch it in your code...
Look here
Basically basicaly, the "throw" statement is made so that you can create your own custom errors. You still need to "catch" them and error handle them in the rest of your code.
<script type="text/javascript">
try{
var str = "010510160";
document.write(decryptStr(str));
}
catch(err){
//error handling code here
}
</script>
also, this piece of code:
if (nextChar < '0' || nextChar > '1')
{
throw decryptInputError;
return;
}
will throw the error for every character that isn't '0' or '1', if i'm not mistaken...
Upvotes: 1