只是爲了鍛鍊,我想打印出這個模式:的JavaScript絕對的初學者=停留在函數返回「未定義」
這裏是到目前爲止的代碼:
var even = " #";
var odd = "# ";
var size = 4;
var rowCount = 1;
var lineLength = 1;
function writeOdd(size){
\t while(lineLength<=size){
\t \t document.write(odd)
\t \t if(lineLength === size){
\t \t document.write("<br>")
\t \t }
\t \t lineLength++;
\t }
}
function writeEven(size){
\t while(lineLength<=size){
\t \t document.write(even)
\t \t if(lineLength === size){
\t \t document.write("<br>")
\t \t }
\t \t lineLength++;
\t }
}
\t \t if(rowCount <= size && rowCount % 2 !== 0) {document.write(writeEven(size));}
\t \t else if(rowCount <= size && rowCount % 2 === 0){document.write(writeOdd(size));}
\t \t rowCount++;
我被困 - 第一行打印出來好,第二個給出「未定義」,就是這樣。
取決於您何時調用第一個功能。如果在頁面解析後,'document.write'會清除所有以前的內容。在所有情況下,使用DOM操作方法而不是'document.write'。 – Teemu
忘掉2k17中的document.write;) – Angels
你正在嘗試document.write writeEven和writeOdd的返回值,這是未定義的。 – James