newbie here. i have made an object like this
var myMovie1=
{
title:'Plan 9 from Outer Space',
genre:'Classic',
rating:3,
showTimes:[6,8,11]
};
var myMovie2=
{
title:'Rush',
genre:'Action',
rating:5,
showTimes:[3,6,14]
};
Now created functions to check current time and movie alerts like this
// function to get date and time
var now =new Date().getHours();
function letsSeeMovie(myMovieSelected){
for (var i = 0; i < myMovieSelected.showTimes.length; i++) {
var showTime=myMovieSelected.showTimes[i];
if (now<showTime) {
return "The next moive starts at " + showTime + "am";
}
else
{
return "sorry you just missed it"
}
}
}
var okcanWeSeeit=letsSeeMovie(myMovie2);
document.write(okcanWeSeeit);
But somehow in the moive object it only displays "sorry missed it". it dosent **reach 11* and show the next playing time. Where i am doing it wrong?? Thanks.
You should return after iterating all the myMovieSelected
function letsSeeMovie(myMovieSelected){
for (var i = 0; i < myMovieSelected.showTimes.length; i++) {
var showTime=myMovieSelected.showTimes[i];
console.log(showTime)
if (now<showTime) {
return "The next moive starts at " + showTime + "am";
}
}
return "u miss it"
}
What you did just now is, you iterate the first element, if it doesnt match you already break the loop
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다