"Practice" javascript some exercises summarize, thinking model

Source: Internet
Author: User

1 Write a method () to determine whether a number can be divisible by 3 and 5 at the same time

<script type= "Text/javascript" >functionmethod () {varnum = Prompt ("Enter a number"); if(IsNaN (num)) {return""; }   //this verifies that the input is not a number, and if it does not enter isNaN, then return ' will exit the function        if(+num% 3 = = 0 && +num% 5 ==0) {document.write (num+ "can be divisible by 3 and 5"); }Else{document.write (num+ "cannot be divisible by 3 and 5")}} method ()//calling Functions</script>

2 Write a Method GetValue (), on any parameter x, returns the value of Y; x<1, when Y;10>x>1 is returned, y=2*x-1;x>10, y=3*x-11

<script type= "Text/javascript" >functionget () {varx = document.getElementById ("in1"). Value,//gets the value of the in1, which is the values entered xy = 0; if(+x < 1) {y=x; }Else if(+x >= 1 && +x < 10) {y= (+x)-1; }Else if(+x > 10) {y= 11 (+x); } document.getElementById ("In2"). Value =y;//Gets the value of in2 and assigns the y of the operation pair to in2, so that the number of calculations will appear in the input of in2    }</script>Enter a number:<input id= "in1" type= "text" onblur= "Get ()" Value= "" ><br>//OnBlur lose Focus buttonThe value of calculated y is <input type= "tel" value= "" id= "in2" ></body>

3 Implement a simple calculator, enter two numbers and an operation symbol, calculate the result. The calculation process uses a parametric and return-worthy function encapsulation implementation

    functionMethod (a,b,c) {//Judging if A and B are numbers .        if(IsNaN (a) | |IsNaN (b)) {            return"Please enter the correct word";//isNaN judgment is not a number if it is not entered if function, and Returun returns the value "、、、、"        }        if(["+", "-", "*", "/"].indexof (c) = =-1) {//Idndexof () is the array subscript, if C is in the array, is to return the corresponding subscript, the smallest is 0, if =-1, indicating there is no            return"Please enter the correct (subtraction)"; }        varNum =eval (A+C+B);//evaluates the result and returns a result        returnnum; }    functionJisuan () {//The first step gets the input numbers and symbols        varA =document.getelementbyid ("one"). Value, b=document.getelementbyid ("the"). Value, C=document.getelementbyid ("FH"). Value; //The function method is called, and the returned value is received        varnum =method (A,B,C); document.getElementById ("JG"). innerhtml=num; }    </script>Number 1:<input type= "text" id= "one" ><br>Number 2:<input type= "text" id= "both" ><br>Symbols:<input type= "text" id= "FH" ><br><button onclick= "Jisuan ()" > Calculation </button> Result: <span id= "JG" ></span>

4 Full selection/All-in-one effect. If the check box below is not selected, the Select all check box is automatically unchecked and checked automatically if all

<script type= "Text/javascript" >//Select All/ Select all    /*1. Find All box 2, get all marquee Check Property 3, get all the checkbox element 4, set the check attribute of these checkbox elements to the Check property of the Select box*/        functionCheck (obj) {//1. Find all Boxes//2. Get all marquee Check Properties//3. Get all the checkbox elements        varCkarr = document.getelementsbyname ("list"); //4. Set the check attribute of these checkbox elements to the selected property of the Select All box         for(vari = 0; i<ckarr.length;i++){            if(obj.checked) {//ckarr[i].setattribute ("Checked", "checked");ckarr[i].checked =true; }Else{        //ckarr[i].removeattribute ("checked");ckarr[i].checked =false; }                    }    }        /*1, get all the checkbox element 2, each to determine whether to select 3, if there is no selected check box unchecked and select*/    </script>varCkarr = document.getelementsbyname ("list");  for(vari = 0; i<ckarr.length;i++) {Ckarr[i].setattribute ("onclick", "Isxz ()"); }        functionIsxz () {//1. Get all checkbox elements        varFlag =true; //2, each judge whether to select         for(vari = 0; i<ckarr.length;i++){            if(!ckarr[i].checked) {Flag=false; }        }    //3, if there is no check box selected uncheck the Reverse selection        //flag = true; Select All        //flag = false; Are there any selected        if(flag) {document.getElementById ("QX"). Checked =true; }Else{document.getElementById ("QX"). Checked =false; }    }</script>

"Practice" javascript some exercises summarize, thinking model

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.