Skip to content
Advertisement

date copy stops the loop statement to function as intended

What i am trying to do is to display available rooms which are in data i map the rooms using data.map , check if one is available between a given ( checkin and checkout date ) using availdata[p.roomId][date].i==0 , if not it will display the available room . the code works fine but as soon as it finds an available room it stops (which means it stops at the else statement ) and won’t look for the rest of the rooms . Any suggestions ? using break ; doesn’t have anything to do with the problem i am facing .

JavaScript

Advertisement

Answer

The problem in your code is this assignment in the for loop:

JavaScript

Dates are objects and objects are not copied. The references are copied. day and strToDatein contain references to the same object. day.setDate(day.getDate() + 1) modifies strToDatein. After the first room was found, strToDatein < strToDateout returns false and the for loop is skipped for all other rooms.

You can fix your problem with a real copy of the Date object in the for loop:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement