+ 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?😥😥😥
8 Respuestas
+ 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 ☺
+ 7
use ? and : symbols
Example:
var age=21;
var str=age<19 ? "Give ice cream" : "Shake hand";
console.log(str)
//Output: Shake hand
+ 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") ;
}
+ 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);
+ 1
I need more answers please
+ 1
String result=(name==Paul && age<19)?new String("Give him Ice cream"):new String("Give him warm hug");
0
Thanks....but it doesn't explain how to use the comparison on the string character Paul
If I know what I'm saying?😫😫
0
@Mudh has code in the form of a ternary operator