JavaScript with half the effort (1)

Source: Internet
Author: User
Tags date arithmetic operators array arrays expression variables variable window
Javascript

This article is Dengyong by the classic forum

Tsinghua University published the "Half a series of JavaScript", I read the book, some translated it. A few years ago looked, recently nothing, turned over, very helpful. This book should have CD-ROM, but the school book, the CD-ROM is not known. I hope it helps you learn JavaScript.

The first chapter of JavaScript introduction

1. Enter a JavaScript statement in the address bar

Javascript:Document.write ("Show text")

2. Embed JavaScript in an HTML document

<script language=javascript>
Document.bgcolor= "Blue."
</script>

Chapter II using Variables and arrays

1. Declaring variables

<script language=javascripe>
Var Answer1,answer2,answer3,answer4;
answer1=9;
answer2=2.5
answer3= "Milkey May"
Answer4=true
</script>

2. Using integer

<script language=javascript>
var decimalnum,hexadecimalnum,octalnum
Decimalnum=24
hexadecimalnum=0x24
octalnum=024
document.write ("Display decimal number:" + decimalnum+ "<br>")
document.write ("Display hexadecimal number:" + hexadecimalnum + "<br>")
document.write ("Show octal number:" + octalnum + "<br>")
</script>

3. Using floating point numbers

<script language=javascript>
var num1,num2,num3,num4
num1=1234567890000.0
Num2=5.14e23
num3=0.0000123456
Num4=6.0254e3-4
document.write ("Floating-point number 1:" +num1+ "<br>")
document.write ("Floating-point number 2:" +num2+ "<br>")
document.write ("Floating-point number 3:" +num3+ "<br>")
document.write ("Floating-point number 4:" +num4+ "<br>")
</script>

4. Using Boolean values

<script language=javascript>
var answer1,answer2
Answer1=true
Answer2=false
document.write ("Display Boolean 1:" +answer1+ "<br>")
document.write ("Display Boolean 2:" +answer2+ "<br>")
</script>

5. Using a String

<script language=javascript>
var str1,str2
str1= "FDSGDG DSFDSF"
Str2= "Wuhan Rtvu"
document.write ("Display string 1:" +str1+ "<br>")
document.write ("Display string 2:" +str2+ "<br>")
</script>

6. Determine variable type

<script>
var answer1,answer2,answer3,answer4
Answer1=9
answer2=2.5
Answer3= "Milky May"
Answer4=true
document.write ("The type of variable 1 is:" +typeof answer1 + "<br>")
document.write ("The type of variable 2 is:" +typeof answer2 + "<br>")
document.write ("The type of variable 3 is:" +typeof Answer3 + "<br>")
document.write ("The type of variable 4 is:" +typeof answer4 + "<br>")
</script>

7. Convert a string into a number

<script>
var str1= "in January"
var int1=parseint (STR1)
document.write ("str1 data type is:" +typeof str1+ "<br>")
document.write ("Int1 data type is:" +typeof int1+ "<br>")
</script>

8. Convert numbers to Strings

<script>
var int1=256
var str1= "" +int1
document.write ("str1 data type is:" +typeof str1+ "<br>")
document.write ("Int1 data type is:" +typeof int1+ "<br>")
</script>

9. Declaring an array

<script>
Array=new Array (5)
Array[0]=1
Array[1]=3
Array[2]=5
Array[3]=7
array[4]=11
document.write ("Array is:" +array[0]+ "" +array[1]+ "" +array[2]+ "" +array[3]+ "" +array[4])
</script>

10. Determine the number of array elements

<script>
Array=new Array (5)
Array[0]=1
Array[1]=3
Array[2]=5
Array[3]=7
array[4]=11
document.write ("Array is:" +array[0]+ "" +array[1]+ "" +array[2]+ "" +array[3]+ "" +array[4]+ "<br>")
document.write ("The number of elements of an array is" +array.length)
</script>

11. Convert an array to a string

<script>
Array=new Array ()
array[0]= "Dark"
array[1]= "Apple"
array[2]= "Nebula"
array[3]= "Water"
Str1=array.join ()
Str2=array.join ("")
document.write (str1+ "<br>")
document.write (STR2)
</script>

12. Sorting arrays

<script>
Array=new Array ()
array[0]= "Dark"
array[1]= "Apple"
array[2]= "Nebula"
array[3]= "Water"
Str1=array.sort ()
document.write (str1+ "<br>")
</script>

Chapter Three Creating an expression

1. Using Arithmetic operators

<script>
Var1=12
var2=10
Varadd=var1+var2
Varsub=var1-var2
Varmult=var1*var2
Vardiv=var1/var2
Varmod=var1%var2
document.write ("Data 1 is:" +var1+ "<br>")
document.write ("Data 2 is:" +var2+ "<br>")
document.write ("Add data is:" +varadd+ "<br>")
document.write ("Data subtraction is:" +varsub+ "<br>")
document.write ("Data multiplication is:" +varmult+ "<br>")
document.write ("Dividing data is:" +vardiv+ "<br>")
document.write ("Data division remainder is:" +varmod+ "<br>")
</script>

2. Increment variable and decrement variable

<script>
Days=1
document.write ("Output variable" +days+ "<br>")
days++
document.write ("Incremental variable becomes:" +days)
</script>

3. Create a comparison expression

<script>
Daysofmonth=28
if (daysofmonth==28)
Month= "February"
document.write ("Days of Month:" +daysofmonth+ "<br>")
document.write ("Month:" +month)
</script>

4. Creating Logical Expressions

<script>
Dayofmonth=28
if (dayofmonth==28 | | dayofmonth==29)
Month= "February"
document.write ("Days of Month:" +dayofmonth+ "<br>")
document.write ("Month:" +month)
</script>

5. Using the conditional operator

<script language= "JavaScript" >
Stomach= "Hungry";
Time= "5:00";
(stomach== "Hungry" &&time== "5:00")? Eat = "Dinner": eat= "a snack";
document.write ("output result" +eat);
</script>

6. Identifying numbers

<script>
var1=24;
(isNaN (var1))? document.write ("Variable var1" +var1+ "not Number"):D ocument.write ("Variable var1" +var1+ "is number")
</script>

Fourth Chapter control procedure Flow

1. Using the If–else statement

<script>
Month= "December"
Date=25
if (month== "December" && date==25)
document.write ("Today is Christmas, shop closes")
Else
document.write ("Welcome, you come to the store to shop")
</script>

2. Use for loop

<script>
for (count=1;count<=10;count++)
document.write ("Output" +count+ "" + "<br>")
</script>

3. Use while loop

<script>
Count=1
while (count<=15) {
document.write ("Output" +count+ "" + "<br>")
count++}
</script>

4. Interrupt Cycle

<script>
Count=1
while (count<=15) {
count++
if (count==8)
Break
document.write ("Output first" +count+ "sentence" + "<br>")}
</script>

5. Continue the Cycle

<script>
Count=1
while (count<=15) {
count++
if (count==8)
Continue
document.write ("Output first" +count+ "sentence" + "<br>")}
</script>

6. Use JavaScript timer

<script>
function Rabbit ()
{document.write ("output statement")
}
</script>
<body onload=window.settimeout (Rabbit (), 5000) >

7. Set the recurrence interval

<script>
Window.setinterval ("Document.form1.text2.value=document.form1.text1.value", 3000)
</script>
<form name=form1>
<input Type=text name=text1><br>
<input Type=text name=text2><br>
</form>

8. Purge Timeout and interval

<script>
Stop=window.setinterval ("Document.form1.text2.value=document.form1.text1.value", 300)
</script>
<form name=form1>
<input Type=text name=text1><br>
<input Type=text name=text2><br>
<input Type=button name=button1 value= "purge timeout and interval" onclick=clearinterval (stop) >
</form>



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.