var student = new Object();
student.classes = ["Digital Photography","Chemistry I","Art of Film","Business in Microcomputer Applications","Study Hall","Algebra 2","Careers","Business Math"]
function newStudent(){
var studentName = document.getElementById("name").value;
var one = document.getElementById("1").value;
var two = document.getElementById("2").value;
var three = document.getElementById("3").value;
var four = document.getElementById("4").value;
var five = document.getElementById("5").value;
var six = document.getElementById("6").value;
var seven = document.getElementById("7").value;
var eight = document.getElementById("8").value;
var newStudent = new Object();
newStudent.name = studentName;
newStudent.classes = [one, two, three, four, five, six, seven, eight];
if(student.classes.slice(0,1) == newStudent.classes.slice(0,1)){
alert('You share first hour with Student');
} else {
alert("You have " + newStudent.classes.slice(0,1) + " instead of " + student.classes.slice(0,1));
}
}
为什么,如果我输入数码摄影作为newStudent的第一堂课没有显示警告告诉用户他们分享该课程?
忽略我厌倦的草率代码,现在我感到厌倦和困惑。
答案 0 :(得分:3)
在这种情况下student.classes.slice(0,1)
你得到一个元素的数组,所以要使它的元素使用索引器
...
if(student.classes.slice(0,1)[0] == newStudent.classes.slice(0,1)[0]){
...