How to use javascript to do simple algorithms

How to use javascript to do simple algorithms

1 Question

As we all know, whether it is Pycharm , IDLE or java , it can calculate simple algorithms, such as addition, subtraction, multiplication and division. However, in Hbuilder, javascript can also be used to calculate addition, subtraction, multiplication and division of numbers.

For example, we calculate: Assume y=5, calculate x=y+2, and display the result.

2 Methods

First, use the <p></p> tags to write the algorithm question title. Then use the <button></button> tag to create an event, and the name after onclick in the tag must be added with (). Then write a <p id=></p> to add an id to the p tag to display the answer, and the subsequent commands are identified by the id. Finally, create variables in <script></script> and use var to create variables.

3 Experimental results and discussion

Through experiments and practice, it is proved that the proposed method is effective and can solve the problem raised at the beginning.

<!DOCTYPE html>

< html >

< head >

< meta charset = "UTF-8" >

< title ></ title >

</ head >

< body >

< p >Assume y=5, calculate x=y+2, and display the result. </ p >

< button onclick = " showAnswer () " > Click here </ button >

< p id = "demo2" ></ p >

< script type = "text/javascript" >

var y = 5 ;

var x = y + 2 ;

function showAnswer (){

document .getElementById( "demo2" ).innerHTML = "x=" + x

}

</ script >

</ body >

</html
>

Conclusion:

We also need to pay attention that in the command getElementById(), when adding the name of the previous id in the brackets, you must add quotation marks, otherwise it will not be recognized as an id.

This is the end of this article on how to use javascript to do simple algorithms. For more relevant content about using javascript to do simple algorithms, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Using JS to implement binary tree traversal algorithm example code
  • How to use JavaScript to implement sorting algorithms
  • JavaScript programming through Matlab centroid algorithm positioning learning
  • Binary Search Tree Algorithm Tutorial for JavaScript Beginners
  • Summary of seven sorting algorithms implemented in JavaScript (recommended!)
  • A brief discussion on an efficient algorithm for constructing tree structures in JavaScript
  • How to Learn Algorithmic Complexity with JavaScript
  • js implements the algorithm for specifying the order and amount of red envelopes

<<:  12 Laws of Web Design for Clean Code [Graphic]

>>:  Pure CSS to achieve cool charging animation

Recommend

In-depth understanding of mathematical expressions in CSS calc()

The mathematical expression calc() is a function ...

Analysis of MySQL crash recovery based on Redo Log and Undo Log

Table of contents MySQL crash recovery process 1....

Solve the error "Can't locate ExtUtils/MakeMaker.pm in @INC"

When installing mha4mysql, the steps are roughly:...

docker logs - view the implementation of docker container logs

You can view the container logs through the docke...

MySQL paging query optimization techniques

In applications with paging queries, queries that...

Implementing simple chat room dialogue based on websocket

This article shares the specific code for impleme...

Summary of common commands for Ubuntu servers

Most of the commands below need to be entered in ...

Installation tutorial of mysql 8.0.11 compressed version under win10

This article shares the installation tutorial of ...

How to implement property hijacking with JavaScript defineProperty

Table of contents Preface Descriptors Detailed ex...

Vue.js implements image switching function

This article shares the specific code of Vue.js t...

jQuery implements shopping cart function

This article example shares the specific code of ...