如何从一个函数获取输出

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

How to get the output from a function

问题

以下是您要翻译的内容:

"I'm new to JS. I'm trying to get the output from a function below with return but somehow it didn't work and only console.log() works.

var merge = function(nums1, m, nums2, n) {
  let hasil = [];
  for (let i = 0; i <= m; i++) {
    const element = nums1[i];
    hasil.push(element);
  }
  for (let j = 0; j <= n; j++) {
    const element2 = nums2[j];
    hasil.push(element2);
  }
  let jawaban = hasil.sort();
  console.log(jawaban)
  return jawaban
};
merge([1, 2, 3], 1, [5, 9, 2], 2);
英文:

I'm new to JS. I'm trying to get the output from a function below with return but somehow it didn't work and only console.log() works.

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

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

var merge = function(nums1, m, nums2, n) {
  let hasil = [];
  for (let i = 0; i &lt;= m; i++) {
    const element = nums1[i];
    hasil.push(element);
  }
  for (let j = 0; j &lt;= n; j++) {
    const element2 = nums2[j];
    hasil.push(element2);
  }
  let jawaban = hasil.sort();
  console.log(jawaban)
  return jawaban
};
merge([1, 2, 3], 1, [5, 9, 2], 2);

<!-- end snippet -->

答案1

得分: 2

只需尝试调用 merge() 函数并将返回值赋给一个新变量,然后打印它。

var merge = function(nums1, m, nums2, n) {
  let hasil = [];
  for (let i = 0; i <= m; i++) {
    const element = nums1[i];
    hasil.push(element);
  }
  for (let j = 0; j <= n; j++) {
    const element2 = nums2[j];
    hasil.push(element2);
  }
  let jawaban = hasil.sort();
  return jawaban
};
let result = merge([1, 2, 3], 1, [5, 9, 2], 2);
console.log(result);
英文:

Just try to call merge() function and assign the return value to a new variable. Then print it.

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

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

var merge = function(nums1, m, nums2, n) {
  let hasil = [];
  for (let i = 0; i &lt;= m; i++) {
    const element = nums1[i];
    hasil.push(element);
  }
  for (let j = 0; j &lt;= n; j++) {
    const element2 = nums2[j];
    hasil.push(element2);
  }
  let jawaban = hasil.sort();
  return jawaban
};
let result = merge([1, 2, 3], 1, [5, 9, 2], 2);
console.log(result);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月22日 11:42:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528478.html
匿名

发表评论

匿名网友

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

确定