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的第一類不顯示警告告訴用戶他們共享該類?爲什麼這些值不相等?
忽略潦草的代碼我累了,現在我累了,感到困惑。
因爲你比較兩個不同的陣列。 '.slice'給你一個新的數組。嘗試循環。 – elclanrs
@elclanrs如果.slice的值是相同的,如果我測試它們是否相等,它是否應該不會恢復爲真? – TheWalkingPacifist
不,比較數組直接比較對象身份。如果你想比較數組的內容,你必須循環和比較每一個。 –