David Sarvasidze
David Sarvasidze

Reputation: 27

JS, function returns undefined instead of true or false

I have a function which checks some parameters and returns either true/false accordingly.

My problem is that it returns true (the last return true;), but then when I call this function again and it gets to console.log(return 1) or console.log(return 2),the function only executes the console.log() and then returns undefined instead of true or false. My assumption is that its not allowed to return from .map() unless its finished running?

isFlashingUnderscore() {
  let count = 0;
  if (!_.isEmpty(this.currentElement.value)) {
    _.map(Object.keys(this.currentElement.value), key => {
      count++
      if (this.currentElement.value[key].object_type == 'date_range') {
        return false;
      } else if (this.currentElement.value[key].object_type == 'date') {
        if (count >= 2) {
          console.log('return 1');
          return false;
        } else {
          console.log('return 2');
          return true;
        }
      } else {
        return true;
      }
    })
  } else {
    console.log('returns this true')
    return true;
  }
}

Upvotes: 1

Views: 1485

Answers (3)

luckysoni
luckysoni

Reputation: 59

please Try this 

isFlashingUnderscore(){
    let count = 0;
    if(!_.isEmpty(this.currentElement.value)){
      var returnVal = _.map(Object.keys(this.currentElement.value), key => {
        count++
        if (this.currentElement.value[key].object_type == 'date_range'){
          return false;
        }else if(this.currentElement.value[key].object_type == 'date'){
          if(count >= 2){
            console.log('return 1');
            return false;
          }else{
            console.log('return 2');
            return true;
          }
        }else{
          return true;
        }
      })
     if(returnVal){
           return true;
        }else{
          return false;
        }
      }else{
       console.log('returns this true')
       return true;
     }
   }

Upvotes: 0

Martijn
Martijn

Reputation: 16123

Not an answer to your actual problem but your code can be simplified. After a return, the remainder of the function does not get executed. You can make use of that:

isFlashingUnderscore(){
    let count = 0;
    if(!_.isEmpty(this.currentElement.value)){
        _.map(Object.keys(this.currentElement.value), key => {
            count++
            if (this.currentElement.value[key].object_type == 'date_range'){
                return false;
            }
            if(this.currentElement.value[key].object_type == 'date'){
                return false;
            }
            if (count >= 2) {
                return false;
            }
        })
    }
    return true;
}

By simplifying it, it becomes a lot easier to understand what is going on and what it is you don't want to happen.

Upvotes: 2

jstuartmilne
jstuartmilne

Reputation: 4508

So inside your if block if(!_.isEmpty(this.currentElement.value)){ you are returning nothing. you are just calling a map but not returning anything. Im not sure what you want to do with that map maybe check every value is true maybe check at least one is. not sure. the return within the map function is for the map lambda meaning you are returning something for each iteration within that map.

say you want to return true if every value is true, then something like this is what you want:

return  _.map(Object.keys(this.currentElement.value), key => {
          count++
          if (this.currentElement.value[key].object_type == 'date_range') {
            return false;
          } else if (this.currentElement.value[key].object_type == 'date') {
            if (count >= 2) {
              console.log('return 1');
              return false;
            } else {
              console.log('return 2');
              return true;
            }
          } else {
            return true;
          }
        }).every(a=>a);

Upvotes: 1

Related Questions