函数返回的最佳实践问题

huangapple go评论61阅读模式
英文:

Problem of best practice with function return

问题

Sure, here's the translated text with the code parts excluded:

我是关于编程的新手。我有一些困难理解函数。
所以我做了练习。但我不知道为什么要使用return

在这个例子中:
函数名是reverse。
我在我的函数中返回了一个数组[1, 2, 5, 8]。
控制台打印一个名为reversed的新数组。

但为什么在验证练习后,它告诉我是false,并发送给我这个解决方案:

感谢:)

我正在搜索和使用Chat GPT来教我为什么在我的情况下要使用return。但是,我什么都不明白... 我需要你的帮助。

谢谢大家。

英文:

I am new about programming. I have some difficulties to undurstand function.
So I done practice. But, I don't know why use return.

My code work.

In this exemple :
the function name is reverse.
I return in my function an array [1, 2, 5, 8].
Console.log a new array named reversed.

function reverse(array) {
  const reversed = [];
  for (i = array.length - 1; i > -1; i--) {
    reversed.push(array[i]);
  }
  console.log(reversed);
}
return reverse([1, 2, 5, 8]);

**
But why after validate exercice, it's say me false and send me this solution :**

function reverse(array) {
  const reversed = [];
  for (let i = array.length - 1; i > -1; i--) {
    reversed.push(array[i]);
  }
  return reversed;
}

Thanks : )

I googling and use chat GPT to teach me why use return in my case. But, nothing about I can understand.. I need your help.

Thank all.

答案1

得分: 1

变量 reversed 在函数体内定义,因此只能从函数内部访问。在您的代码中,您尝试从函数外部访问该变量。return 关键字在函数内部用于返回一个值,在函数外部使用 return 是没有效果的。

英文:

The variable reversed is defined inside the function body so it can only be accessed from inside of the function. In your code you tried to access the variable from outside of the function. return keyword is used inside the function to return a value, using return outside the function has no effect.

答案2

得分: 0

以下是您要翻译的内容:

Some remarks:

  • The names reverse and reversed are very similar which can lead to confusion. I see it happening in another answer and in your comments. reverse is defined as a function, and reversed is a local variable that references an array. They are completely different objects.

  • In your code you have placed a return statement where it is not valid. A return statement can only occur inside a function body.

  • console.log(reversed) will output the result in the console window, but that is not what the purpose is of the exercise. Although console.log is very useful while you are debugging your code, it is not equivalent to a return value. The caller of your function will get as return value what the function has returned with a return statement, not what console.log has output.

  • Your code does not define i with let (or var) and so it is implicitly declared as a global variable. This is not good practice. Always define variables explicitly.

  • Your code calls the reverse function. This is what you typically would do to test your function, but in code challenges, this call is typically made by the person or framework that tests your implementation. And those tests will call it more than once, with different arrays as argument, often including boundary cases, like an empty array, or a very large array.

So when you debug code, you would call the function and print the value that it returns, like so:

function reverse(array) {
  const reversed = [];
  for (let i = array.length - 1; i > -1; i--) {
    reversed.push(array[i]);
  }
  return reversed;
}

let result = reverse([1,2,3,4]);
console.log(result); // Should be [4,3,2,1]
result = reverse([1]);
console.log(result); // Should be [1]
result = reverse([]);
console.log(result); // Should be []
result = reverse(reverse([1,2,3,4])); // Reversing the result!
console.log(result); // Should be [1,2,3,4]

Maybe I should also mention that reverse is also a native method available for arrays. It will reverse the array itself instead of creating a new array (like above). If the purpose is to always create a new array, you can still make use of it: first create a copy of the given array, reverse the copy and return that:

function reverse(array) {
  // The spread syntax creates a copy, and reverse is applied on the copy
  return [...array].reverse();
}

let result = reverse([1,2,3,4]);
console.log(result); // Should be [4,3,2,1]
result = reverse([1]);
console.log(result); // Should be [1]
result = reverse([]);
console.log(result); // Should be []
result = reverse(reverse([1,2,3,4])); // Reversing the result!
console.log(result); // Should be [1,2,3,4]
英文:

Some remarks:

  • The names reverse and reversed are very similar which can lead to confusion. I see it happening in another answer and in your comments. reverse is defined as a function, and reversed is a local variable that references an array. They are completely different objects.

  • In your code you have placed a return statement where it is not valid. A return statement can only occur inside a function body.

  • console.log(reversed) will output the result in the console window, but that is not what the purpose is of the exercise. Although console.log is very useful while you are debugging your code, it is not equivalent to a return value. The caller of your function will get as return value what the function has returned with a return statement, not what console.log has output.

  • Your code does not define i with let (or var) and so it is implicitly declared as a global variable. This is not good practice. Always define variables explicitly.

  • Your code calls the reverse function. This is what you typically would do to test your function, but in code challenges, this call is typically made by the person or framework that tests your implementation. And those tests will call it more than once, with different arrays as argument, often including boundary cases, like an empty array, or a very large array.

So when you debug code, you would call the function and print the value that it returns, like so:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function reverse(array) {
  const reversed = [];
  for (let i = array.length - 1; i &gt; -1; i--) {
    reversed.push(array[i]);
  }
  return reversed;
}

let result = reverse([1,2,3,4]);
console.log(result); // Should be [4,3,2,1]
result = reverse([1]);
console.log(result); // Should be [1]
result = reverse([]);
console.log(result); // Should be []
result = reverse(reverse([1,2,3,4])); // Reversing the result!
console.log(result); // Should be [1,2,3,4]

<!-- end snippet -->

Maybe I should also mention that reverse is also a native method available for arrays. It will reverse the array itself instead of creating a new array (like above). If the purpose is to always create a new array, you can still make use of it: first create a copy of the given array, reverse the copy and return that:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function reverse(array) {
  // The spread syntax creates a copy, and reverse is applied on the copy
  return [...array].reverse(); 
}

let result = reverse([1,2,3,4]);
console.log(result); // Should be [4,3,2,1]
result = reverse([1]);
console.log(result); // Should be [1]
result = reverse([]);
console.log(result); // Should be []
result = reverse(reverse([1,2,3,4])); // Reversing the result!
console.log(result); // Should be [1,2,3,4]

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月16日 18:18:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定