简体   繁体   English

如何在JavaScript中引用或从单个值参数提取?

[英]How Do I Reference or Pull From A Single Value Parameter in JavaScript?

I understand that the parameter in this exercise is holding a value that is a string. 我知道本练习中的参数包含一个字符串值。 I have tried these solutions: 我已经尝试过以下解决方案:

function addingGrace(s) {
console.log ("'only the beginning!'");

}

/* Do not modify code below this line */

console.log(addingGrace('only the beginning'), '<-- should be "only the beginning!"');

What I do not understand is how to pull the value out of the parameter. 我不明白的是如何从参数中提取值。 All of the tutorials that I have found have multiple parameters. 我发现的所有教程都有多个参数。

Here is the original exercise though: 这是原始的练习:

Modify the function to return the given string with an exclamation mark added to the end of it. 修改函数以返回给定的字符串,并在其末尾添加一个感叹号。

function addingGrace(s) {

}

/* Do not modify code below this line */

console.log(addingGrace('only the beginning'), '<-- should be "only the beginning!"');

Does anyone know where I can find a resource that references this type of work with single value parameters. 有谁知道我在哪里可以找到引用具有单值参数的此类工作的资源。 I do not want the answer to this particular exercise as it is for an entrance exam for a coding school. 我不希望这个特殊练习有答案,因为它是为编码学校的入学考试而准备的。 I really wanted to figure this out myself but I am stuck. 我真的很想自己弄清楚这一点,但我被困住了。

You just need to return the parameter s which holds the value only the beginning . 您只需要返回only the beginning值的参数s

Please see code snippet. 请参阅代码段。

 function addingGrace(s) { return s +"!"; } /* Do not modify code below this line */ console.log(addingGrace('only the beginning'), '<-- should be "only the beginning!"'); 

You can just use backticks (template literals/strings). 您可以只使用反引号(模板文字/字符串)。 You also need to return from the function: 您还需要从函数return

 function addingGrace(s) { return `"${s}"`; } console.log(addingGrace('only the beginning')); 

 function addingGrace(s) { return (s + '!') } console.log(addingGrace('only the beginning')) 

The code: 编码:

function addingGrace(s) {
  console.log ("'only the beginning!'");
}

does not work because the code is already logging your request instead of returning the text you add. 不起作用,因为代码已经在记录您的请求,而不是返回您添加的文本。

You probaly would want to do is this: 您可能要这样做的是:

function addingGrace(s) {
  return(s);
}

But because the exercise wanted the text to have an exclamation point do this: 但是由于练习希望文本具有感叹号,因此可以这样做:

function addingGrace(s) {
  return(s + "!");
}

I hope I helped! 希望我能帮上忙!

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

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