简体   繁体   English

如何将其重写为 function?

[英]How do I rewrite this as a function?

as part of a javascript course I've written a simple Caesar cypher script.作为 javascript 课程的一部分,我编写了一个简单的 Caesar cypher 脚本。 I want to phrase it as a function but don't quite understand the syntax of functions.# enter image description here我想将其表述为 function 但不太了解函数的语法。# 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

 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.在您的代码之前放置一个function foo() { ,在它之后放置一个} ,您就创建了一个名称为foo而没有 arguments 的 function。

When you now call the function by invoking it当您现在调用 function 时

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).从 ES6+ 开始,您可以将 function 定义为 const lambda(也是代码箭头函数)。 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.您只需在使用它之前定义 function,上面的方法允许它在 function 出现在源代码中之前使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将实例重写为 function? - How do I rewrite an instantiation to a function? 如何使用 Promise 重写函数? - How do I rewrite a function using promises? 如何使用装饰器重写反应组件作为纯函数? - How do I rewrite a react component with decorators as a pure function? 我该如何使用工厂函数重写它? - How do I rewrite this using a factory function instead? 如何将使用组合参数 (latlong) 的 function 重写为具有单独参数、纬度和经度的 function? - How do I rewrite a function that uses a combined parameter (latlong) into a function with seperate perameters, latitude and longitude? 如何重写此Firebase函数,以使返回的数组不为空? - How do I rewrite this Firebase function so my returned array isn't empty? 如何递归地重写这个 function? - How can I rewrite this function recursively? 我该如何在没有`var`的情况下重写此代码? - How do I rewrite this code without the `var`? 如何使用 if...else 子句重写它? - How do I rewrite this using if…else clauses? 如何通过添加for循环来遍历data.games并在搜索中显示比分时也进行添加来重写搜索功能? - How do I rewrite the search function by adding a for loop to go through data.games and also add when I search that it says the score?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM