Home > database >  How do I rewrite this as a function?
How do I rewrite this as a function?

Time:01-23

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

CodePudding user response:

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

CodePudding user response:

You can convert this script into a function by wrapping the entire code block in a function definition and providing a name for the function. You can pass any input parameters that the function needs, such as the message to be encoded, the shift value, etc. Here's an example of how you might convert your script into a function:

function caesarCipher(userinput, shift) {
    let alphabet = "abcdefghijklmnopqrstuvwxyz";
    let alphabetupper = alphabet.toUpperCase();
    let result = "";
    for (let i = 0; i < userinput.length; i  ) {
        // rest of the code stays the same
    }
    alert(("Your encoded message is ")   (result));
}

You can call this function by passing the input message and shift value as arguments, for example:

caesarCipher("What's your message?", 15);

This way you can reuse the function multiple times with different input messages and shift values.

CodePudding user response:

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.

  • Related