英文:
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 <= 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);
<!-- 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 <= 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);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论