Use of the JavaScript three mesh operator

Source: Internet
Author: User

Trinocular operator

What is : Perform different actions/return different values according to different conditions

syntax : Condition 1, value 1 or Action 1://If Condition 1 is met, return value 1 or perform action 1

Condition 2 = value 2 or action 2://Otherwise, if condition 2 is met, return value 2 or perform action 2

...? ....: default value or operation; Otherwise, (none of the preceding conditions are met)//perform the default action, or return a summary of the default values: Multiple conditions, multiple events, multi-select one execution.

JS: If the operation of the statement is relatively simple, it is recommended to use three mesh operation instead of IF. Else

Summary: an expression? " Leap year ":" Common Year "

A variety of conditions 1? Value 1: Condition 2? Value: Value 3

The test questions are as follows:

2. If total>=500, call 80 percent, (less than 500 do nothing)


var money=prompt ("Please enter the amount of the item you have purchased ()");
var total=money*0.8;
money=money>=500?total:money;
document.write (Money);

/*3. Despise the question * *
If the user enters a reply, the user input is displayed
Otherwise, "The owner is lazy and nothing is left."


var reply=prompt ("Please make your comments");
Reply=reply!= ""? Reply: "The host is very lazy, nothing left";
document.write (reply);


Judging True/false
var name1= "Scott";
var name2= "John";

Name=name1>name2? " True ":" false ";
alert (name);


Test questions
Ask the user to enter a year to determine if the year is run:
Condition: The year can be divisible by 4 and cannot be divisible by 100 or the year can be divisible by 400

var year=prompt ("Please enter a year");
Year=year%4==0&&year%100!=0? " Leap year ": Year%400==0?" Leap year ":" Common Year ";
document.write (year);

/* Calculate the Leto index:
klt= Weight/(height * height)
Klt>25 Fat
<20 Skinny
Normal
*/

var stature=prompt ("Your height (m)");
var weight=prompt ("Your Weight (kg)");
var klt=weight/(stature*stature);
Klt=klt>25? " Fat ": klt<20?" Thin ":" normal ";
document.write (KLT);

Use of the JavaScript three mesh operator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.