Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I rewrite this as a function?

as part of a javascript course I’ve written a simple Caesar cypher script. I want to phrase it as a function but don’t quite understand the syntax of functions.#
enter image description here


var userinput = prompt("What's your message?");                 //get user input
let alphabet = "abcdefghijklmnopqrstuvwxyz";                    //define alphabet
let alphabetupper = alphabet.toUpperCase();                     //define alphabet uppercase (else it gets messy to do the lookup!)
let shift=15;                                                   //define letter shift
//___________________________________________
let result = "";
for (let i = 0; i < userinput.length; i++) {

    let letter = userinput[i];                                  //declare letter as userinput char at index
    if (letter.toLowerCase()==letter.toUpperCase()){            //if its not a letter...
        result +=letter;                                        //print it to result
    }

    else if ((letter===letter.toUpperCase()))  {                //else if it is an uppercase letter...
        let j=alphabetupper.indexOf(letter);                        //get index of letter in alphabet "j"
       if ((j+shift)<25){                                      //check shift pos is less than end of alphabet
      result+= ((alphabetupper[j+shift]));                     //print uppercase letter 15 places forward of result
       }
       else if ((j+shift)>25){                                 //if the new index is past z...
        result+=((alphabetupper[j+(shift-26)]));               //loop past z
       }
   
    }
    else if (/*(letter.toLowerCase()!==letter.toUpperCase())&&*/(letter==letter.toLowerCase()))  {   //if it is a lowercase letter...
        let j=alphabet.indexOf(letter);                        //get index of letter in alphabet "j"
       if ((j+shift)<25){                                      //check shift pos is less than end of alphabet
      result+= (alphabet[j+shift]);                            //print letter 15 places forward to result
       }
       else if ((j+shift)>25){                                 //if the new index is past z...
        result+=(alphabet[j+(shift-26)]);                      //loop past z
       }
   
    }
     
};
alert(("Your encoded message is ") + (result));                 //Output result

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

All you have to do now is run the code i called the function in the last line

function ceasar (userinput){
  
var userinput = prompt("What's your message?");                 //get user input
let alphabet = "abcdefghijklmnopqrstuvwxyz";                    //define alphabet
let alphabetupper = alphabet.toUpperCase();                     //define alphabet uppercase (else it gets messy to do the lookup!)
let shift=15;                                                   //define letter shift
//___________________________________________
let result = "";
for (let i = 0; i < userinput.length; i++) {

    let letter = userinput[i];                                  //declare letter as userinput char at index
    if (letter.toLowerCase()==letter.toUpperCase()){            //if its not a letter...
        result +=letter;                                        //print it to result
    }

    else if ((letter===letter.toUpperCase()))  {                //else if it is an uppercase letter...
        let j=alphabetupper.indexOf(letter);                        //get index of letter in alphabet "j"
       if ((j+shift)<25){                                      //check shift pos is less than end of alphabet
      result+= ((alphabetupper[j+shift]));                     //print uppercase letter 15 places forward of result
       }
       else if ((j+shift)>25){                                 //if the new index is past z...
        result+=((alphabetupper[j+(shift-26)]));               //loop past z
       }
   
    }
    else if (/*(letter.toLowerCase()!==letter.toUpperCase())&&*/(letter==letter.toLowerCase()))  {   //if it is a lowercase letter...
        let j=alphabet.indexOf(letter);                        //get index of letter in alphabet "j"
       if ((j+shift)<25){                                      //check shift pos is less than end of alphabet
      result+= (alphabet[j+shift]);                            //print letter 15 places forward to result
       }
       else if ((j+shift)>25){                                 //if the new index is past z...
        result+=(alphabet[j+(shift-26)]);                      //loop past z
       }
   
    }
     
};
alert(("Your encoded message is ") + (result));                 //Output result
}
ceasar();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading