Eloquent JavaScript #02 # program_structure

Source: Internet
Author: User

In the first chapter, the authors describe various values, but these independent values are meaningless and only reflect their value when placed on a larger frame. So the second chapter begins to introduce the program structure.

1, var VS. Let and const

The author recommends let, because Var has some bizarre behavior? Not explained in detail for the time being.

Const is used to bind constant values

2. About JavaScript Operating environment

In the context of running a JavaScript program, there is not only a binding that you define, but there are other "environment bindings" for all sorts of initializations.

For example, prompt, which is a binding that holds a function type, is almost no use in the online generation browser because it cannot be decorated. But it's a lot of use in a toy-type program.

typeof prompt"function"
typeof NaN"number"

3, about the self-increment number.

In JavaScript + +--+ = = *= is available, but not in Python, it has + = but no + +--

4. JavaScript Naming conventions

Follow the Hump nomenclature (you can see that the JavaScript standard function is named that way, but why is a function like number the first letter capitalized?). Because it is the constructor)

Number ("xxxx") Nannumber ("222") 222

5. Print the following graphic:

############################
            let result = "#";              for (Let i = 0; I! = 7; + +i) {                Console.log (result                ); += "#";            }

6, about the control flow of the small exercise

Write A program this uses console.log to print all the numbers from 1 to +, with the exceptions. For numbers divisible by 3, print instead of the number "Fizz" , and for numbers divisible by 5 (and not 3), print "Buzz" ins Tead.

When you have this working, modify your program to print for numbers that is "FizzBuzz" divisible by both 3 and 5 (and still P Rint or for numbers divisible by only one of "Fizz" "Buzz" those).

<script type= "Text/javascript" > for(Let i = 1; I <= 100; + +)i) {if(i% 3 = = = 0 && I% 5 = = 0) {Console.log ("Fizzbuzz"); } Else if(i% 5 = = = 0) {Console.log ("Buzz"); } Else if(i% 3 = = = 0) {Console.log ("Fizz"); } Else{console.log (i); }            }        </script>

PS. Priority +-more than = = = far greater than && | | (where && greater than | | Assignment number priority is the smallest, in addition, the above = = = is superfluous, when you can determine the value of both sides of the same type of time with = = can be.

7, exercise three:

Passing this string to console.log should show something like this:

# # # ## # # #  # # # ## # # #  # # # ## # # #  # # # ## # # #

If you had a program that generates this pattern, define a binding and a size = 8 Y size , outputting a grid of the given width and height.

<script type= "Text/javascript" >Let size= Number (Prompt ("Enter the size of chessboard")); if(!Number.isnan (size)) {                //NaN Although the meaning is "not a number", but the TypeOf output type is still number                //In addition, unexpectedly discovered IE environment actually does not support IsNaN attributeLet result = "";  for(Let i = 0; I! = size; + +)i) { for(Let j = 0; J! = size; + +)j) {if((i + j)% 2 = = 0) {result+= " "; } Else{result+= "#"; }} result+ = "\ n";            } console.log (Result); }        </script>

Eloquent JavaScript #02 # program_structure

Related Article

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.