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

Detailed explanation of the difference between run/cmd/entrypoint in docker

In Dockerfile, run, cmd, and entrypoint can all b...

JavaScript object-oriented class inheritance case explanation

1. Object-oriented class inheritance In the above...

Detailed explanation of the command mode in Javascript practice

Table of contents definition structure Examples C...

Understand CSS3 FlexBox elastic layout in 10 minutes

Basic Introduction Features Flexbox is a CSS disp...

Understand the use of CSS3's all attribute

1. Compatibility As shown below: The compatibilit...

Simple implementation method of vue3 source code analysis

Table of contents Preface 🍹Preparation 🍲vue3 usag...

...

Detailed explanation of MYSQL database table structure optimization method

This article uses an example to illustrate the me...

How to use an image button as a reset form button

When we make a form, we often set a submit button ...

Example of implementing TikTok text shaking effect with CSS

In daily development, front-end students often ar...

What does this.parentNode.parentNode (parent node of parent node) mean?

The parent node of the parent node, for example, t...

UDP DUP timeout UPD port status detection code example

I have written an example before, a simple UDP se...

Detailed explanation of Js class construction and inheritance cases

The definition and inheritance of classes in JS a...