0

I am saving a record on button click but it always throwing an error.

I am including an if else condition. I have surpassed all the conditions but still my code is going to the if condition but It should not go to if condition.

code is -

my this.state.question.options value is -

[
    {
        id:3250,
        label:'good answer',
        assert:1
        position:1
    },
    {
        id:3249,
        label:'bad answer',
        assert:0
        position:2
    }
]

and I am checking if else condition as -

if (this.state.question.options.filter(o => o.assert === true).length <= 0) {
    hasError = true
    errorKey = 'add-question-modal-missing-assert-options'
}
else {
    alert("test");
}

my code should goto else part and print test as alert but it is going to if part and showing error. Why ?

I wanna show else part i.e test as alert

12
  • what do you mean ? Commented Sep 17, 2019 at 11:06
  • Your array has one value which is an object with 2 keys (0 and 1). There is no assert key, and definitely not one with a strict value of true. Then again, the array is not valid as your string values are not quoted. Please provide an MCVE. Commented Sep 17, 2019 at 11:06
  • So how can i do this ? or how can I make array for this filter or how to make filter according to this array ? Commented Sep 17, 2019 at 11:09
  • String values are quoted .. I am just giving you the example Commented Sep 17, 2019 at 11:09
  • Yeah, so give a decent example. One that doesn't leave us guessing. Having an array with only one object that has numeric property keys seems weird. A strict comparison to true when the occurring assert values are 0 and 1 also seems weird. Commented Sep 17, 2019 at 11:10

5 Answers 5

2

You are using the strict comparison operator (===) while comparing 2 different values. In your example, 1 is being parsed as an integer, while true is being parsed as a boolean. A strict comparison operator is used to check 2 values on equal values AND equal types.

To fix the error in your code, use a loose comparison (==) or convert the integer to a boolean by using !!1

Sign up to request clarification or add additional context in comments.

Comments

0
if (this.state.question.options.filter((el) => {return !!el.assert}).length <= 0) {
    hasError = true
    errorKey = 'add-question-modal-missing-assert-options'
}
else {
    alert("test");
}

Comments

0

In JavaScript, 1 is not strictly equal to true. However, it is a good practice to use the strict equality operator ===.

You should compare o.assert with the real possible value o.assert === 1.

In terms of readability I would also consider comparing the length to 1 instead of 0:

this.state.question.options.filter(option => option.assert === 1).length < 1

Comments

0

this.state.question.options value is -

[
    {
        id:3250,
        label:'good answer',
        assert:1,
        position:1
    },
    {
        id:3249,
        label:'bad answer',
        assert:0,
        position:2
    }
]

and then

if (this.state.question.options.filter(o => o.assert == true)).length <= 0) {
    hasError = true
    errorKey = 'add-question-modal-missing-assert-options'
} else {
    alert("test");
}

replace === strict type with ==

Comments

0

Do u want this kind of code

if (this.state.question.options.length <= 0) {
    assert = true;
    hasError = true;
    errorKey = 'add-question-modal-missing-assert-options'
}
else {
    alert("test");
}

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.