簡體   English   中英

從 function 返回數組值

[英]Returning Array value from a function

我在從乘法 function 返回數組值時遇到問題。 我想知道什么是最好的方法? 感謝任何幫助。 謝謝

<head>
<script>

function test() {
    let list1 = [ 17, 8, 9, 5, 20 ];
    let list2 = [ 12, 4, 8, 15, 17, 5, 20, 11 ];

    // Test the multiply function by calling it two times.
    let mult1 = multiply(list1,3);
    let mult2 = multiply(list2,2);

function multiply(list,x) {            
    var myArray = [0];
    for (i = 0; i < list.length; i++) {
      myArray[i] = list[i] * x;            
    }   
  return myArray;  
}

let output = mult1 + '<br>' + mult2;

</script>

</head>

<body>
<button type="button" click="test()">Compute</button>
<p id="output"></p>
</body>

您的代碼中有幾個問題:

  1. 在HTML中, click屬性應該是onclick
  2. 要在p標簽中顯示 output:首先,您需要通過 ID 獲取元素。 然后,將值附加到innerHTML

 function test() { let list1 = [17, 8, 9, 5, 20]; let list2 = [12, 4, 8, 15, 17, 5, 20, 11]; // Test the multiply function by calling it two times. let mult1 = multiply(list1, 3); let mult2 = multiply(list2, 2); let output = document.getElementById("output"); output.innerHTML = mult1 + '<br>' + mult2; function multiply(list, x) { var myArray = [0]; for (i = 0; i < list.length; i++) { myArray[i] = list[i] * x; } return myArray; } }
 <head> </head> <body> <button type="button" onclick="test()">Compute</button> <p id="output"></p> </body>

首先,您錯過了測試function 的一個花括號。

function test() {
    let list1 = [ 17, 8, 9, 5, 20 ];
    let list2 = [ 12, 4, 8, 15, 17, 5, 20, 11 ];

    // Test the multiply function by calling it two times.
    let mult1 = multiply(list1,3);
    let mult2 = multiply(list2,2);

// You miss this curly brace below
}

其次,您不能調用 mult1 和 mult2,因為它們是在 function scope 中聲明的。

/* This function creates a scope 
 * so every variable is declared in this function cannot be called outside
 */
function test() {
    let list1 = [ 17, 8, 9, 5, 20 ];
    let list2 = [ 12, 4, 8, 15, 17, 5, 20, 11 ];

    // Test the multiply function by calling it two times.
    let mult1 = multiply(list1,3);
    let mult2 = multiply(list2,2);
}

console.log(mult1, mult2) // undefined, undefined
// Uncaught ReferenceError: mult1 is not defined
let output = mult1 + '<br>' + mult2;

您可以按以下方式解決此問題

function test() {
    let list1 = [ 17, 8, 9, 5, 20 ];
    let list2 = [ 12, 4, 8, 15, 17, 5, 20, 11 ];

    // Test the multiply function by calling it two times.
    let mult1 = multiply(list1,3);
    let mult2 = multiply(list2,2);
    // Return value here
    return { mult1, mult2 }
}
// Read it here
let { mult1, mult2 } = test();
let output = mult1 + '<br>' + mult2;

你可以像這樣實現它:

 function test() { let list1 = [ 17, 8, 9, 5, 20 ]; let list2 = [ 12, 4, 8, 15, 17, 5, 20, 11 ]; const multiply = (list, x) => list.map(num => num * x); return [ multiply(list1, 3), multiply(list2, 2) ]; } const [ mult1, mult2 ] = test(); const output = mult1 + '<br>' + mult2; // Reflect the result inside your div#output element document.getElementById('output').innerHTML = output;
 <div id="output"></div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM