What do you mean by not working? It works fine. If you want both buttons to show up with && that’s not what && operand does. It returns the last thing, like 20 && 21 will give you 21. Try it in your console.
Hey, yes I know how this conditional operator works but I have seen some complex code for this in stackoverflow.
Anyhow my question here is why the above code is not working?
I have seen in previous challenges that Boolean can be used like button1 && button2
that’s why I have written it like that and it is working fine only when you enter age greater than 18.
But the code is not working when age is less than 18.
The && operator isn’t how you concatenate objects in React. Also, each React object needs to be enclosed in a single parent object. Try putting your two <button> objects inside a <span>:
render() {
const buttonOne = <button onClick={this.submit} >Submit</button>;
const buttonTwo = <button>You May Enter</button>;
const buttonThree = <button>You Shall Not Pass</button>;
return (
<div>
<h3>Enter Your Age to Continue</h3>
<input
style={inputStyle}
type="number"
value={this.state.input}
onChange={this.handleChange} /><br />
{this.state.input >= 18 ? buttonTwo : <span>{buttonThree}{buttonOne}</span>}
</div>
);
}
Why do you need this && anywhere there? What is it that’s not working? I didn’t make anything work, I just copypasted your code into existing react app without modifying any single line of code. And it shows “you shall not pass” button on change, when number is less than 18, and submit otherwise. Why do you need the third button and what’s not working I don’t really get.
Please describe expected behavior and actual one.
Once a number has been submitted, and the value of the input is once again changed, the button should return to reading Submit.
It seems you’re not considering this instruction in your conditional expression. To concatenate conditional expressions means something like (a<b?(c>d?a=1:a=2):a=3) (inner parenthesis for sake of clarity), so you have to consider the condition regarding < or > 18, and the condition regarding the above instruction
I think the problem with this challenge is the wording. It took me a long time to understand that we are supposed to change the button to “you shall not pass” or “you can enter” ONLY when someone clicks on the “Submit” button. The hints didn’t make it clear enough.