我正在编码消息编码器/解码器,我遇到了encode_message,decode_message和get_cipher函数的问题,我不知道哪里出错了。我还附上说明作为评论。此外,如果有更简单的方法来编码字母数组的字符,请将其添加到答案中。
'use strict()';
function bind_events() {
'use strict';
$('#encode_msg').hide();
$('#decode_msg').hide();
// bind the event handler to the event.
$('#encode_msg').click(function() {
encode_handler();
});
$('#decode_msg').click(function() {
decode_handler();
});
var $keyword = $('#keyword');
$($keyword).blur(function() {
$('#encode_msg').hide();
$('#decode_msg').hide();
if ($(this).val().length > 0) {
$('#encode_msg').show();
$('#decode_msg').show();
} else {
$('#encode_msg').hide();
$('#decode_msg').hide();
}
});
}
function update_display(header, message) {
'use strict';
return $('#display-message h2, p').text();
}
function get_keyword() {
'use strict';
return $('#keyword').val().split('');;
}
function get_message() {
'use strict';
return $('#message').val().split('');
}
/**
* Input: None
* Output: The cipher alphabet using the keyword.
* The cipher contains all characters from a to z, A to Z
* and punctuation /.,;:?!
* The order of the letters will be mixed up.
* Process:
* 0. the alphabet as an array.
* * The alphabet must include all upper and lower case letters
* * AND the following punctuation (/.,?;:!)
* 1. Create a variable to hold the keyword from the form.
* 2. Declare a variable called remaining_letters and initialize it to the keyword.
* * The keyword should be split into an array.
* * The variable will hold the remaining letters from the original alphabet
* (the alphabet ordered a to z) that are not in the keyword.
* 3. Iterate through the length of the alphabet.
* 3.1 Get the index of the alphabet letter from the keyword.
* 3.2 If the index value is less than 0, push the letter from the alphabet
* onto the remaining_letters array
* 4. Return remaining_letters
*/
function get_cipher() {
'use strict';
var alphabet = [A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, '/', '.', ',', ';', ':', '?', '!'];
var $keyword = $('#keyword');
var $remaining_letters = $keyword;
$('$keyword').val().split('');
for (i = 0; i < alphabet.length(); i++) {
if (alphabet[i] == $keyword[j]) {
if (j < 0) {
remaining_letters.push(alphabet[i]);
}
}
}
return remaining_letters;
}
function encode_handler() {
'use strict';
var encoded_message = encode_message();
update_display('Encoded Message', encoded_message);
}
function decode_handler() {
'use strict';
var decoded_message = decode_message();
update_display('Decoded Message', decoded_message);
}
/**
* Encode the message.
* Input: None
* Output: The encrypted message.
* Process:
* 1. Required Variables:
* 1. the cipher as an array
* 2. the alphabet as an array.
* * The alphabet must include all upper and lower case letters
* * AND the following punctuation ( .,?;:!) - including the space
* 3. the message from the form as an array
* 4. an empty array variable that will hold the encoded message
* 2. iterate through the length of the message.
* 1. Find the index of the letter (message) in the alphabet.
* 2. when the index value is positive (including 0)
* then get the corresponding letter from the cipher using the index
* and push it onto the encoded message array.
* otherwise
* push the letter (message) onto the encoded message array.
* 3. return the encoded message array joined together.
*/
function encode_message() {
'use strict';
var cipher = [];
var alphabet = [A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, '/', '.', ',', ';', ':', '?', '!'];
var message = get_message();
var encoded_message = [];
for (i = 0; i < message.length(); i++) {
if (message[i] == alphabet[j]) {
if (j < 0) {
encoded_message.push(message[i]);
}
}
}
return encoded_message.toString();
}
/**
* Input: None
* Output: The decrypted message.
* Process:
* 1. Required Variables:
* 1. the cipher as an array
* 2. the alphabet as an array.
* 3. the message from the form as an array
* 4. an empty array variable that will hold the decoded message
* 2. iterate through the length of the message.
* 1. Find the index of the letter (message) in the cipher.
* 2. when the index value is positive (including 0)
* then get the corresponding letter from the alphabet using the index
* and push it onto the decoded message array.
* otherwise
* push the letter (message) onto the decoded message array.
* 3. return the decoded message array joined together.
*/
function decode_message() {
'use strict';
var cipher = [];
var alphabet = [A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, '/', '.', ',', ';', ':', '?', '!'];
var message = get_message();
var decoded_message = [];
for (i = 0; i < message.length(); i++) {
if (message[i] == alphabet[j]) {
if (j < 0) {
decoded_message.push(message[i]);
}
}
}
return decoded_message.toString();
}
$(bind_events).ready();
如何编辑这些功能以使它们正常运行?