0

I have problems with task which looks like pretty simply. I need to match two variables and return true if match and false if not.

I have made method which check it

    isAnyApplicationFromToday(): boolean {
      this.userApplications.forEach(application => {
        if (application.registerDate.toString() == this.today.toString()) {
          return true;
        }
      });
    return false;
  }

but it always return false.

Funny is that, if I console.log(), variable values match.

I tried already combination:

if (application.registerDate.toString() === this.today.toString())
if (application.registerDate === this.today)
if (application.registerDate == this.today)

image

any ideas ?

EDIT:

  isAnyApplicationFromToday(): boolean {
    let isAny = false;
      this.userApplications.forEach(application => {
        if (application.registerDate.toString() === this.today.toString()) {
          isAny = true;
        }
      });
    return isAny;
  }

work just fine.

plucins
  • 153
  • 2
  • 11
  • I suggest you to use timestamp. Convert date into timestamp and then compare. – Sachin Shah Dec 08 '18 at 12:29
  • But how I will comare if timestamp is from same day ? – plucins Dec 08 '18 at 12:30
  • 1
    The problem is you are returning inside the `forEach`, instead of the main function Take a look here: https://stackoverflow.com/questions/34653612/what-does-return-keyword-mean-inside-foreach-function – rpadovani Dec 08 '18 at 12:34
  • 1
    The `forEach` takes in a function. You're example is taking a function that returns true. This has no effect on the outer function. What you intend is for the outer function `isAnyApplicationFromToday` to return the result of the innerfunction. I suggest you do something like this: isAnyApplicationFromToday(): boolean { var result = false; this.userApplications.forEach(application => { result = application.registerDate.toString() == this.today.toString(); }); return result; } – Rafiek Dec 08 '18 at 12:37
  • Guys You're right !! – plucins Dec 08 '18 at 12:42

0 Answers0