+ 5

I read an interesting topic today but can't create a code using it😥😥...i need urgent help please

How do I use ternary operators in creating a code with both string and integer characters ...example: I want to create a code that says if his name is Paul and he is less than 19 years old then output= Give him ice cream...but if his name is not Paul and he is older than 19 then output=Give him a warm hug I don't know if I'm understood?😥😥😥

29th Nov 2018, 1:03 PM
Kachi Max
Kachi Max - avatar
9 ответов
+ 10
just 1 note : U can remove ( ) around 2 conditions joined by && operator as precedence of && operator is more than conditional operator //it will reduce the expression ☺
29th Nov 2018, 4:52 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 7
use ? and : symbols Example: var age=21; var str=age<19 ? "Give ice cream" : "Shake hand"; console.log(str) //Output: Shake hand
29th Nov 2018, 1:07 PM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 6
if ( name === "Paul" && age < 19){ console.log("I like ice creams i hope Paul would nt mind ice creams as well :-) ") ; } else { console.log(" I know for sure Paul is above 19") ; }
29th Nov 2018, 4:41 PM
Morpheus
Morpheus - avatar
+ 5
condition 1 : name is Paul and age < 19 condition 2 : name is not Paul and age > 19 if condition 1 and condition 2 are not satisfied then "both the condition is not satisfied" is getting printed var age=20; var name="Paul"; var result=(age<19 && name=="Paul")?"Give him ice-cream":(age>19 && name!="Paul")?"Give him a warm hug":"both the condition is not satisfied"; console.log(result);
29th Nov 2018, 1:29 PM
Rishi Anand
Rishi Anand - avatar
+ 1
I need more answers please
29th Nov 2018, 1:22 PM
Kachi Max
Kachi Max - avatar
+ 1
String result=(name==Paul && age<19)?new String("Give him Ice cream"):new String("Give him warm hug");
30th Nov 2018, 3:42 AM
Niveditha Javaniki
Niveditha Javaniki - avatar
0
Thanks....but it doesn't explain how to use the comparison on the string character Paul If I know what I'm saying?😫😫
29th Nov 2018, 1:19 PM
Kachi Max
Kachi Max - avatar
0
@Mudh has code in the form of a ternary operator
30th Nov 2018, 8:13 PM
chuong