所以在我的程序中,如果条件满足,我试图将某些值推送到数组。即使某些值不满足条件,事情仍然会被推送到数组中。任何人都可以解释为什么会发生这种情况以及如何解决它?
下面是代码:
//This program's purpose is to translate to and from morse code.
var input, output; //Declare all global variables.
var inputArray = []; //Declare the empty array to store the converted characters.
input = (prompt('Enter the word(s) or sentence(s) that you wish to convert.', 'Hello friend')).toLowerCase(); //Prompt the user for input and convert it to lowercase.
function Encode(input) { //Declare a function to encode characters into leet.
var i, il; //Declare all local variables.
for (i = 0, il = input.length; i < il; i++) { //Declare a for loop to cycle through each character in the input.
if (input.charAt(i) === 'a') { //If the character found at the current position in the input is A,
inputArray.push('.-'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End if.
else if(input.charAt(i) === 'b') { //If the character found at the current position in the input is B,
inputArray.push('-...'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'c') { //If the character found at the current position in the input is C,
inputArray.push('-.-.'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'd') { //If the character found at the current position in the input is D,
inputArray.push('-..'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'e') { //If the character found at the current position in the input is E,
inputArray.push('.'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'f') { //If the character found at the current position in the input is F,
inputArray.push('..-.'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'g') { //If the character found at the current position in the input is G,
inputArray.push('--.'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'h') { //If the character found at the current position in the input is H,
inputArray.push('....'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'i') { //If the character found at the current position in the input is I,
inputArray.push('..'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'j') { //If the character found at the current position in the input is J,
inputArray.push('.---'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'k') { //If the character found at the current position in the input is K,
inputArray.push('-.-'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'l') { //If the character found at the current position in the input is L,
inputArray.push('.-..'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'm') { //If the character found at the current position in the input is M,
inputArray.push('--'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'n') { //If the character found at the current position in the input is N,
inputArray.push('-.'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'o') { //If the character found at the current position in the input is O,
inputArray.push('---'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'p') { //If the character found at the current position in the input is P,
inputArray.push('.--.'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'q') { //If the character found at the current position in the input is Q,
inputArray.push('--.-'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'r') { //If the character found at the current position in the input is R,
inputArray.push('.-.'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 's') { //If the character found at the current position in the input is S,
inputArray.push('...'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 't') { //If the character found at the current position in the input is T,
inputArray.push('-'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'u') { //If the character found at the current position in the input is U,
inputArray.push('..-'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'v') { //If the character found at the current position in the input is V,
inputArray.push('...-'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'w') { //If the character found at the current position in the input is W,
inputArray.push('.--'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'x') { //If the character found at the current position in the input is X,
inputArray.push('-..-'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'y') { //If the character found at the current position in the input is Y,
inputArray.push('-.--'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'z') { //If the character found at the current position in the input is Z,
inputArray.push('--..'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === '0') { //If the character found at the current position in the input is 0,
inputArray.push('-----'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === '1') { //If the character found at the current position in the input is 1,
inputArray.push('.----'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === '2') { //If the character found at the current position in the input is 2,
inputArray.push('..---'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === '3') { //If the character found at the current position in the input is 3,
inputArray.push('...--'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === '4') { //If the character found at the current position in the input is 4,
inputArray.push('....-'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === '5') { //If the character found at the current position in the input is 5,
inputArray.push('.....'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === '6') { //If the character found at the current position in the input is 6,
inputArray.push('-....'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === '7') { //If the character found at the current position in the input is 7,
inputArray.push('--...'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === '8') { //If the character found at the current position in the input is 8,
inputArray.push('---..'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === '9') { //If the character found at the current position in the input is 9,
inputArray.push('----.'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === '.') { //If the character found at the current position in the input is .,
inputArray.push('.-.-.-'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === '?') { //If the character found at the current position in the input is ?,
inputArray.push('..--..'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === '!') { //If the character found at the current position in the input is !,
inputArray.push('-.-.--'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === ('(' || ')')) { //If the character found at the current position in the input is ( or ),
inputArray.push('-.--.-'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === ':') { //If the character found at the current position in the input is :,
inputArray.push('---...'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else { //If the character found at the current position in the input is not specified above,
inputArray.push(input.charAt(i)); //The push the same character to the inputArray to signify it has not been converted.
} //End else.
} //End for.
} //end function.
Encode(input); //Call the endcoder function.
output = inputArray.join(''); //Join all converted characters stored in the array together.
console.log('Input was: ' + input); //Log the input to the console.
console.log('Output is: ' + output); //Log the output to the console.
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>
示例:当我在提示符中输入“Hello friend”时,“.... /。/ .- .. / .- .. / --- / ..-。/ .-。/ .. / ./-./-../“被退回。在输出中,我在括号中标记的斜杠不应该在那里:“.... /。/ .- .. / .- .. / --- [/] ..-。/ .-。 /.././-./-..[/]"
提前感谢您的帮助!
答案 0 :(得分:0)
您只检查(i+1)
值,而不是该位置的输入字符。
替换
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
}
与
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
}
到处
//This program's purpose is to translate to and from morse code.
var input, output; //Declare all global variables.
var inputArray = []; //Declare the empty array to store the converted characters.
input = (prompt('Enter the word(s) or sentence(s) that you wish to convert.', 'Hello friend')).toLowerCase(); //Prompt the user for input and convert it to lowercase.
function Encode(input) { //Declare a function to encode characters into leet.
var i, il; //Declare all local variables.
for (i = 0, il = input.length; i < il; i++) { //Declare a for loop to cycle through each character in the input.
if (input.charAt(i) === 'a') { //If the character found at the current position in the input is A,
inputArray.push('.-'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End if.
else if(input.charAt(i) === 'b') { //If the character found at the current position in the input is B,
inputArray.push('-...'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'c') { //If the character found at the current position in the input is C,
inputArray.push('-.-.'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'd') { //If the character found at the current position in the input is D,
inputArray.push('-..'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'e') { //If the character found at the current position in the input is E,
inputArray.push('.'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'f') { //If the character found at the current position in the input is F,
inputArray.push('..-.'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'g') { //If the character found at the current position in the input is G,
inputArray.push('--.'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'h') { //If the character found at the current position in the input is H,
inputArray.push('....'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'i') { //If the character found at the current position in the input is I,
inputArray.push('..'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'j') { //If the character found at the current position in the input is J,
inputArray.push('.---'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'k') { //If the character found at the current position in the input is K,
inputArray.push('-.-'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'l') { //If the character found at the current position in the input is L,
inputArray.push('.-..'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'm') { //If the character found at the current position in the input is M,
inputArray.push('--'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'n') { //If the character found at the current position in the input is N,
inputArray.push('-.'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'o') { //If the character found at the current position in the input is O,
inputArray.push('---'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'p') { //If the character found at the current position in the input is P,
inputArray.push('.--.'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'q') { //If the character found at the current position in the input is Q,
inputArray.push('--.-'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'r') { //If the character found at the current position in the input is R,
inputArray.push('.-.'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 's') { //If the character found at the current position in the input is S,
inputArray.push('...'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 't') { //If the character found at the current position in the input is T,
inputArray.push('-'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'u') { //If the character found at the current position in the input is U,
inputArray.push('..-'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'v') { //If the character found at the current position in the input is V,
inputArray.push('...-'); //Then push the new character to the inputArray to signify it has been converted.
if ((i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'w') { //If the character found at the current position in the input is W,
inputArray.push('.--'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'x') { //If the character found at the current position in the input is X,
inputArray.push('-..-'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'y') { //If the character found at the current position in the input is Y,
inputArray.push('-.--'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === 'z') { //If the character found at the current position in the input is Z,
inputArray.push('--..'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === '0') { //If the character found at the current position in the input is 0,
inputArray.push('-----'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === '1') { //If the character found at the current position in the input is 1,
inputArray.push('.----'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === '2') { //If the character found at the current position in the input is 2,
inputArray.push('..---'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === '3') { //If the character found at the current position in the input is 3,
inputArray.push('...--'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === '4') { //If the character found at the current position in the input is 4,
inputArray.push('....-'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === '5') { //If the character found at the current position in the input is 5,
inputArray.push('.....'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === '6') { //If the character found at the current position in the input is 6,
inputArray.push('-....'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
}
} //End else if.
else if(input.charAt(i) === '7') { //If the character found at the current position in the input is 7,
inputArray.push('--...'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === '8') { //If the character found at the current position in the input is 8,
inputArray.push('---..'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === '9') { //If the character found at the current position in the input is 9,
inputArray.push('----.'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === '.') { //If the character found at the current position in the input is .,
inputArray.push('.-.-.-'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === '?') { //If the character found at the current position in the input is ?,
inputArray.push('..--..'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === '!') { //If the character found at the current position in the input is !,
inputArray.push('-.-.--'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === ('(' || ')')) { //If the character found at the current position in the input is ( or ),
inputArray.push('-.--.-'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else if(input.charAt(i) === ':') { //If the character found at the current position in the input is :,
inputArray.push('---...'); //Then push the new character to the inputArray to signify it has been converted.
if (input.charAt(i + 1) != ' ') { //If the next character is not equal to a space,
inputArray.push('/'); //Then push a / to the inputArray.
} //End if.
} //End else if.
else { //If the character found at the current position in the input is not specified above,
inputArray.push(input.charAt(i)); //The push the same character to the inputArray to signify it has not been converted.
} //End else.
} //End for.
} //end function.
Encode(input); //Call the endcoder function.
output = inputArray.join(''); //Join all converted characters stored in the array together.
console.log('Input was: ' + input); //Log the input to the console.
console.log('Output is: ' + output); //Log the output to the console.
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>