简体   繁体   中英

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

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();

Put a function foo() { before your code and an } after it, and you've created a function with the name foo and no arguments.

When you now call the function by invoking it

foo();

you will execute the code between the curly braces {...}

Since ES6+ you can define the function as a const lambda (also code arrow function). like so:

const foo = () => { /* all your code */ }

You just have to define the function before using it, the approach above allows it to use the function before it appears in the source code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM