js shorthand for if. Conditional statements

var a = 10; varb = (a>1) ? 100:200; alert(b);

If condition a>1 true, then the variable b assign a value 100 , otherwise the variable b is assigned the value 200 .

Task Js 3_4. Complement the code: 3 local variables are declared using the var keyword. It is necessary to assign the value of the following ternary operator to the max variable: if a is greater than b , then return a , otherwise return b .
Code snippet:

if (a*b< 6) { result = "Мало"; } else { result = "Много"; }


Questions for self-control:

  • What is the syntax of the ternary operator?
  • How many arguments does a ternary operator have?
  • Switch statement in javaScript - switch

    The javascript switch statement serves to test a variable for multiple values:

    Syntax:

    switch (variable or expression) ( case option1: //..statement block.. break case option2: //..statement block.. break default: //..statement block.. )

    The value of a variable or expression is checked: in each case one of the values ​​is checked, in the case of a suitable value, one or another block of statements corresponding to the given one is executed case.

    The block starting with the service word default can be omitted. Block statements will be executed if none of the listed values ​​in all case does not fit.

    Important: The break statement is required after each considered value of the variable (after each case); if it is not used, then all the statements below will be displayed

    Compare with operator IF:

    var a = 2; switch(a) ( case 0: // if (a === 0) case 1: // if (a === 0) alert("Zero or one"); // then output... break; case 2: // if (a === 2) alert("Two"); // then output... break; default: // else alert("Many"); // otherwise output... )

    How to group multiple options?

    To execute the same statements, it is possible to group several case. As in the example above:

    Case 0: case 1: alert("Zero or one"); break; ...

    When a = 0 and a = 1, the same statement is executed: alert("Zero or one");

    Example 4: Ask the user to enter a color. Output the English translation of the entered color. For color "blue" And "blue" produce the same value.


    ✍ Solution:
    • Create a web page with html skeleton and tag script.
    • Initialize Variable color
    • var color = prompt("What color?" ) ;

      var color = prompt("What color?");

    • Check the value of a variable with a construct switch, outputting for each value the corresponding translation:
    • switch (color) ( case "red" : alert("red"); break; case "green": alert("green"); break; // ...

      If the variable color has the value "red", then display the translation - "red" in the modal window and exit the construction (break;). If the variable color has the value "green", then display the translation - "green" in the modal window and exit the construction (break;).

    • For flowers "blue" And "blue" do the grouping:
    • // ... case "blue": case "blue": alert("blue"); break; // ...

      If the variable color has the value "blue" or variable color has the value "blue", then display the translation - "blue" in the modal window and exit the construction (break;).

    • Organize the output for those colors that are not provided by the program:
    • // ... default : alert("y we have no information on this color" ) ) // end of switch

      // ... default: alert("y we have no information on this color") ) // end of switch

    • Test the script in a browser.

    Task Js 3_6. Find and fix errors in the following code snippet:

    14 15 16 17 var number = prompt("Enter number 1 or 2:" ) ; switch (number) ( case "1" ( document.write ("One" ) ; ) ; break ; case "2" ( document.write ("Two" ) ; ) ; break ; default ( document.write ("You entered value other than 1 and 2" ) ; ) ; )

    var number = prompt("Enter number 1 or 2:"); switch (number) ( case "1" ( document.write("One"); ); break; case "2" ( document.write("Two"); ); break; default ( document.write("You entered value other than 1 and 2"); ); )


    Task Js 3_7. What will be displayed on the screen when the following code is executed?:

    1 2 3 4 5 6 7 8 9 10 11 12 13 var value = "2" ; switch (value) ( ​​case "1" : case "2" : case "3" : document.write ("Hello" ) ; break ; case "4" : case "5" : document.write ("World" ) ; default : document.write("Error" ) ; )

    var value = "2"; switch (value) ( ​​case "1": case "2": case "3": document.write("Hello"); break; case "4": case "5": document.write("World"); default: document.write("Error"); )


    Task Js 3_8. Ask the user for a number - the number of crows on the branch. Depending on the entered number (no more than 10), display a message: - 1 crow sits on a branch - 4 crows sit on a branch - 10 crows sit on a branch

  • Depending on the entered number, the ending of the word changes. "crow".
  • Use the javascript switch statement to check.
  • Save this page in the results folder (it will be useful for further work).

  • Questions for self-control:

  • When is it appropriate to use the construction as a conditional operator? switch?
  • What is the purpose of the default block in the statement switch?
  • Is it necessary to use the break statement in the construction switch?
  • How grouping is done for multiple value options in an operator switch?
  • Loop statements in javaScript - For

    Syntax:

    for(initial counter value; condition; counter increment) ( //..statement block.. )

    Important: The javascript for loop is used when it is known in advance how many times the cyclic actions should be repeated (how many iterations the loop has)

    • An assignment expression is used as the initial value of the iteration counter: for example, i=0 - the loop counter starts from zero:
    • for(var i = 0; condition; counter increment) ( //.. statement block.. )

    • As a counter increment, the step with which the counter should increase is indicated: for example, indicates that each iteration of the loop will be accompanied by its increase by 1 :
    • for(var i = 0; condition; i++) ( //..statement block.. )

    • The loop condition is the end value of the counter: i10, for example, stops the loop:
    • for(var i = 0; i