Menu

[solved]-Need Help Code Make Clear Button Work Clear Data Fields 7 Year Old Neighbor Excitedly Tol Q39037331

I NEED HELP WITH THE CODE TO MAKE THE CLEAR BUTTON WORK(SO IT WILL CLEAR THE DATA IN THE FIELDS).

Your 7 year-old neighbor has just excitedly told you that shehas discovered this ‘really neat code’ which she and her friendsare using to send messages to each other. The code, she explains,involves writing a number in place of each letter of the alphabet.The coding equivalences are: A=1, B=2, C=3, D=4, E=5, … Z=26 (yourneighbor doesn’t worry about lower case letters: she doeseverything in upper case). In addition, they are using zero foreach space between words. Thus, a message such as ‘HELLO WORLD’would be encoded as: “8 5 12 12 15 0 23 15 18 12 4”. For decoding,the numbers are to be entered with commas between them, so anencoded message such as “1,2,3,0,24,25,26” would be decoded as:“ABC XYZ” (space between C and X). Your neighbor, however, saysthat it takes a ‘really long time’ to translate her messages intocode – or to decode the messages she receives from friends, so sheis wondering if you could write a computer program to do those jobsfor her (and you have agreed!). The screen should have an inputfield for the message and an output field for the result (this canbe a text box or a div area). The functions are launched by buttonclicks using an ‘Encode’ command button, a ‘Decode’ command button,and a ‘Clear’ command button. Remember that only upper casemessages are processed, so all input in the message text box shouldbe translated into upper case before processing (re-display theuppercase version of the message in the original box). ForDecoding, you will need to separate the values based on the commaas the delimiter; also note that input such as: “1, 2, 3” (withspaces after the commas) should not crash the system but should betreated as the equivalent to: “1,2,3” Valdiation issues on Decode:As discussed in the recording for the Decode function, Decodeshould fail validation check for any portion of the decode stringthat has non-numerics. This means, for example, the following: “23” -> returns ?, not B “4A” -> returns ?, not D BUT: Continueto allow leading and trailing spaces as allowable (i.e., ignored byparseInt) You will handle any characters or values outside theexpected as follows: for Encoding, an unexpected character shouldreturn as the number 99; for Decoding, an unexpected number shouldreturn as a ? (question mark) in the final result. The Clear buttonis the standard operation: clear the display areas and put thecursor in the input field. You should review various stringhandling methods for javascript. Pay special attention to itemssuch as length, charAt(), substr(), toUpperCase(), split(), etc.Part A is the Decode function (and clear); Part B is the Encodefunction

THIS IS WHAT I HAVE SO FAR, BUT I CAN’T FIGURE OUT HOWTO GET THE CLEAR BUTTON TO WORK. THANK YOU FOR YOURHELP.

var $ = function(id) {
return document.getElementById(id);
};
  
  
window.onload = function(){
$(“btnDecode”).onclick = fnDecode;
$(“btnEncode”).onclick = fnEncode;
$(“btnClear”).onclick = fnClear;
};
function fnDecode () {
var msg = $ (“textin”).value;
if (msg === “”) {
$(“textin_span”).innerHTML = “* Please enter a message to decode*”;
$(“textin”).focus();
return;
} else {
$(“textin_span”).innerHTML = “”;
}

var nums = msg.split(“,”); //split method separates bydelimiter
var outstr = “”;

for (var i=0; i < nums.length; i++) {
var n2 = parseInt(nums[i]);
if (isNaN(n2)) {
outstr += “?”;
} else if (isNallN(nums[i].trim())) {
outstr += “?”;
} else if(n2 === 0) {
outstr += ” “;
} else if (n2 < 1 || n2 > 26) {
outstr += “?”;
} else {
outstr += String.fromCharCode(n2+64);
}
}
$(“textout”).value = outstr;
  
}

function isNallN(s) {
// parse string to check that all characters are digits….
  
  
for (i = 0; i < s.length; i++) {
var c = s.charAt(i);
if (isNaN(c)|| c === ‘ ‘){
return true;
}
}   
return false;
  
}

function fnEncode () {
//encode logic here
var msg = $(“textin”).value.toUpperCase();
$(“textin”).value = msg;
  
var outstr = “”;
var c;

for (var i=0; i<msg.length; i++) {
c = msg.charCodeAt(i);//ASCII: space=32, A=65, B=66, etc…
//var x = msg.charAt(i);
if (c === 32){
outstr += “0 “;
} else {
c = c – 64; //adj to A=1, B=2, etc.
}if (c < 1 || c > 26) {
outstr += “99 “;
} else {
outstr += c + ” “;
}
}

$(“textout”).value = outstr;   
}
  

function fnClear() {
//clear logic here…
}

Expert Answer


Answer to I NEED HELP WITH THE CODE TO MAKE THE CLEAR BUTTON WORK (SO IT WILL CLEAR THE DATA IN THE FIELDS). Your 7 year-old neigh… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *