mercredi 3 février 2016

Why are some if statement rules not working?

So in my program, I am trying to push certain values to an array if conditions are met. Even though the conditions are not met for some values, things still get pushed to the array. Can anyone explain why this is happening and how to fix it?

Heres the code:

    //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="http://ift.tt/1fyy28x"></script>

Example: When I enter "Hello friend" into the prompt, "...././.-../.-../---/ ..-./.-./.././-./-../" is returned. In the output, the slashes I marked in the brackets are not supposed to be there: "...././.-../.-../---[/] ..-./.-./.././-./-..[/]"

Thanks for any help in advance!

Aucun commentaire:

Enregistrer un commentaire