[英]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.