2017-03-09 59 views
3

我試圖勾搭javascript和我創建了一個多維數組還挺:的Javascript環流式陣列

var students = [ [['David'], [80]], [['Vinoth'], [77]], [['Goren'],[55]] ]; 

然後,我創建了一個if語句,這將考驗,如果學生成績是F至A.

for (var i = 0; i < students.length; i++) { 
    for (var j = 1; j < students.length; j++) { 
    document.write(students[i][j] + "<br/>"); 

    if (students[i][j] < 60) { 
     document.write(students[i][j] + " is Grade : F"); 
    } else if (students[i][j] < 70) { 
     document.write(students[i][j] + " is Grade : D"); 
    } else if (students[i][j] < 80) { 
     document.write(students[i][j] + " is Grade : C"); 
    } else if (students[i][j] < 90) { 
     document.write(students[i][j] + " is Grade : B"); 
    } else if (students[i][j] < 100) { 
     document.write(students[i][j] + " is Grade : A"); 
    } 
    } 
} 

在我的追求中,輸出學生和成績的名字,我甚至最終創建了三個for循環,這些循環不起作用。

我想知道怎樣才能達到這個輸出:

David's grade is 80 and is C Grade. 
Vinoth's grade is 77 and is C Grade. 
Goren's grade is 55 and is F Grade. 

任何想法,缺少我的代碼?

+0

爲什麼你使用'students.length'作爲兩個'for'循環的限制? – Barmar

+0

你的數據結構沒有意義。循環將其視爲分數的二維數組,但數組也包含名稱。爲什麼在另一個數組級別的名稱和分數? – Barmar

+0

大衛的等級是不是應該等於B,因爲他有80分以上?如果沒有,您需要在代碼中更改<與<=。 –

回答

3

其他答案建議簡化您的陣列結構。如果你真的想使用額外的嵌套,你需要做一個額外的索引。但是你不需要嵌套循環,因爲它仍然只是一個線性結構。

for (var i = 0; i < students.length; i++) { 
    var student = students[i][0][0]; 
    var score = students[i][1][0]; 
    var grade; 
    if (score < 60) { 
     grade = "F"; 
    } else if (score < 70) { 
     grade = "D"; 
    } else if (score < 80) { 
     grade = "C"; 
    } else if (score < 90) { 
     grade = "B"; 
    } else if (score < 100) { 
     grade = "A"; 
    } 
    document.write(student + "'s score is " + score + ' and is Grade ' + grade + '<br>'); 
} 

另外,如果你期望的80得分生成一個C級,而不是B,你應該使用<=而不是<的比較。

一般情況下,我建議使用對象,而不是對異構數據陣列,所以您的陣列應該是這樣的:

var students = [ { 
    name: 'David', 
    score: 80 
}, { 
    name: 'Vinoth', 
    score: 77 
}, { 
    name: 'Goren', 
    score: 55 
}]; 

然後使用students[i].namestudents[i].score,這是很容易理解的。

這個想法是,您使用數組來收集所有相同類型的事物,並收集有關事物相關信息的對象。

+0

我同意使用對象而不是製作複雜的多維數組。 –

+1

@le_m謝謝,我在比較中添加了一個關於使用'<='而不是'<'的註釋來更改成績之間邊界的學生成績。 – Barmar

1

你的數據結構很奇怪,它深深地嵌套。

我將它轉換爲較淺:

var students = [ [ 'David', 80], ['Vinoth', 77], ['Goren', 55] ]; 

這將是更好地界定一個參數函數來確定等級的信:

function getGrade(value) { 
    if (value > 90) return 'A'; 
    else if (value > 80) return 'B'; 
    else if (value > 70) return 'C'; 
    else if (value > 60) return 'D'; 
    else return 'F' 
} 

我寧願forEach循環,但由於您似乎對for有偏好,我們可以這樣寫:

for (let i = 0; i < students.length; i++) { 
    console.log(`${students[i][0]}'s grade is ${students[i][1]} and is a ${getGrade(students[i][1])} Grade`); 
} 
+0

我認爲現在推薦模板文字還爲時過早。沒有IE的版本實現它。 – Barmar

+0

@Barmar多數民衆贊成,這只是示例代碼,雖然後沒有說它在瀏覽器中運行的任何東西。 –

1

您的學生數組的內部元素(如名稱或成績)本身被包裹在括號內,因此成爲數組。取下內支架。

此外,如果您希望根據您的示例將得分80評分爲「C」,則您的比較運算符<應該變爲<=

let students = [['David', 80], ['Vinoth', 77], ['Goren', 55]]; 
 

 
// Define rating function: 
 
let rate = (grade) => grade <= 60 ? 'F' : grade <= 70 ? 'D' : grade <= 80 ? 'C' : grade <= 90 ? 'B' : 'A'; 
 

 
// Map students to phrases: 
 
let phrases = students.map((student) => `${student[0]}'s grade is ${student[1]} and is ${rate(student[1])} grade.`); 
 

 
// Output DOM nodes: 
 
phrases.forEach((phrase) => { 
 
    let el = document.createElement('div'); 
 
    el.textContent = phrase; 
 
    document.body.appendChild(el); 
 
});

1

如果你真的,真的只想解決只用很少的改動你的解決方案不接觸你的數據,那麼你是失蹤3個,做事能獲得你想要的輸出:

  • document.write("<br>")
  • 內循環,直到students.length - 1 stundent的
  • 名稱訪問作爲students[i][j - 1]

下面是完整的固定碼,點擊 「運行代碼片斷」 來運行它在底部

var students = [ [['David'], [80]], [['Vinoth'], [77]], [['Goren'],[55]] ]; 
 

 
for (var i = 0; i < students.length; i++) { 
 
    for (var j = 1; j < students.length - 1; j++) { 
 
     document.write(students[i][j - 1] + "'s grade is "); 
 

 
     if (students[i][j] < 60) { 
 
      document.write(students[i][j] + " and is Grade : F"); 
 
     } else if (students[i][j] < 70) { 
 
      document.write(students[i][j] + " and is Grade : D"); 
 
     } else if (students[i][j] < 80) { 
 
      document.write(students[i][j] + " and is Grade : C"); 
 
     } else if (students[i][j] < 90) { 
 
      document.write(students[i][j] + " and is Grade : B"); 
 
     } else if (students[i][j] < 100) { 
 
      document.write(students[i][j] + " and is Grade : A"); 
 
     } 
 

 
    } 
 
    document.write("<br>") 
 

 
}

1

你的數組是3級嵌套。所以你可能想要運行3個循環。然而,第二級只有2個元素,第三級只有1個元素。所以你只能運行1個循環。請參閱以下工作片段。

var students = [ [['David'], [80]], [['Vinoth'], [77]], [['Goren'],[55]] ]; 
 
for (var i = 0; i < students.length; i++) { 
 
    document.write(students[i][0][0] + " "); 
 

 
    if (students[i][1][0] < 60) { 
 
     document.write(students[i][1][0] + " is Grade : F" + "<br/>"); 
 
    } else if (students[i][1][0] < 70) { 
 
     document.write(students[i][1][0] + " is Grade : D"+ "<br/>"); 
 
    } else if (students[i][1][0] < 80) { 
 
     document.write(students[i][1][0] + " is Grade : C"+ "<br/>"); 
 
    } else if (students[i][1][0] < 90) { 
 
     document.write(students[i][1][0] + " is Grade : B"+ "<br/>"); 
 
    } else if (students[i][1][0] < 100) { 
 
     document.write(students[i][1][0] + " is Grade : A"+ "<br/>"); 
 
    } 
 
}

1

Array對象的是這個特定的情況下更好的數據結構。另外,我已將分數分數轉換爲函數,使代碼更易於閱讀和推理。

function getGrade (score) { 
    var grades = [ 
    { 
     letter: 'A', 
     minScore: 90 
    }, 
    { 
     letter: 'B', 
     minScore: 80 
    }, 
    { 
     letter: 'C', 
     minScore: 70 
    }, 
    { 
     letter: 'D', 
     minScore: 60 
    }, 
    { 
     letter: 'F', 
     minScore: 0 
    } 
    ]; 
    for (var i = 0; i < grades.length; i++) { 
    if (grades[i].minScore <= score) return grades[i].letter; 
    } 
} 

var students = [ 
    { 
    name: 'David', 
    score: 80 
    }, 
    { 
    name: 'Vinoth', 
    score: 77 
    }, 
    { 
    name: 'Goren', 
    score: 55 
    } 
]; 

students.forEach(function (student) { 
    var str = student.name + 
      "'s score is " + 
      student.score + 
      " and is " + 
      getGrade(student.score) + 
      " Grade."; 
    console.log(str); 
}); 
1

我希望這將適用於你

var students = [ [['David'], [80]], [['Vinoth'], [77]], [['Goren'],[55]] ]; 
    for (var i = 0; i < students.length; i++) { 
     document.write("<br/>"+students[i][0] + "'s"); 

爲(VAR J = 1;Ĵ< students.length; J ++){ 的console.log(學生[I] [0]) ;

if (students[i][j] < 60) { 
    document.write(students[i][j] + " is Grade : F"); 
} 
else if (students[i][j] < 70) { 
    document.write(students[i][j] + " is Grade : D"); 
} else if (students[i][j] < 80) { 
    document.write(students[i][j] + " is Grade : C"); 
} else if (students[i][j] < 90) { 
    document.write(students[i][j] + " is Grade : B"); 
} else if (students[i][j] < 100) { 
    document.write(students[i][j] + " is Grade : A"); 
} 

}}