0

I have what I thought would be a simple logic check. In my code

$scope.seatMap.PlaneTypeCode = "175"

However, when I set

$scope.seatMap.PlaneTypeCode === "175" //my debugger returns <b>false </b>

parseInt($scope.seatMap.PlaneTypeCode,10) ===175  // equals 17

I added a few zeros on the radix but that did nothing to help.

I am not sure how to do a comparison check. Any insight on this would be hugely appreciated.

Here is my full if statement

if (parseInt(col.name,10) ===4 && parseInt($scope.seatMap.PlaneTypeCode,10) ===175 && $scope.TripSummary) {
       col.available = false;
    }

****** Changed my response to this

if (parseInt(col.name,10) ===4 && $scope.seatMap.PlaneTypeCode ==="175" && $scope.TripSummary) {
            col.available = false;
        }  // still getting false
3
  • 1
    The first step would be to find out what console.log($scope.seatMap.PlaneTypeCode); shows in the console. You don’t have to guess. Commented Aug 3, 2016 at 5:10
  • However, when I set: you're not using assignment operator, where = is assignment, Try $scope.seatMap.PlaneTypeCode = "175" instead $scope.seatMap.PlaneTypeCode === "175" Commented Aug 3, 2016 at 5:13
  • it returned "175". I am missing something simple here. just not sure what. Commented Aug 3, 2016 at 5:13

3 Answers 3

1

=== is a best practice, you should use it. Review the reference provided by @Joyson

You don't need the ,10 in parseInt because it is the default.

var PlaneTypeCode = "175";
if (parseInt(PlaneTypeCode) === 175) {
  console.log('equal');
}

If PlaneTypeCode is a code and can contain anything other than digits, a better comparison would be:

if (PlaneTypeCode === "175")
Sign up to request clarification or add additional context in comments.

3 Comments

I know this should be the case basic javascript, but I am getting false on this and have been for a while. there has to be something I am missing.
if (PlaneTypeCode === "175") would be better since in the first portion of this answer, the variable is a string and following the parseInt, the comparison value will be converted to a number - hence the fail on the strict comparison
A radix should continue to be used since not all browsers in use are compliant with ES5, which introduced the default of 10. Also, 0x will still indicate base 16. Using strict equality can introduce as many issues as it solves, and in this case is illogical. If PlaneTypeCode is a string and strict equality is necessary, then PlaneTypeCode === "175" should be used since parseInt(PlaneTypeCode) === 175 is equivalent to PlaneTypeCode == 175 (i.e. the conversion implicit in == is explicit using parseInt, so what's the point?).
1

You can use == instead of ===

$scope.seatMap.PlaneTypeCode == "175"

Please refer to Difference between == and === to know more

1 Comment

That was false for me too. I tried that initially. Thanks.
0

use angular.equals($scope.seatMap.PlaneTypeCode,"175")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.