Summary of basic javascript algorithms_javascript skills
This article shares five javascript algorithms for your reference. The specific content is as follows
1. Linear search
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>线性查找</title> </head> <body> <p>数组为:[2,4,6,23,53,545,65,3,24,5,3,6]</p> <p>输入要查的值:<input type="text" id="serch" onchange="search_index(this.value)"><p> <p>这个值在数组的位置是:<span id="val"></span><p> <script> //1.声明查找函数 //Arr为数组,x为要搜索的值 function search(Arr,x){ for(var i=0; i<Arr.length; i++){ if(Arr[i]==x){ return i; //返回x在数组中的位置; } } return "不存在"; //循环结束还未发现的话 则返回"不存在"; } //2.实例练习 var arr=[2,4,6,23,53,545,65,3,24,5,3,6]; //声明一个数组 function $$(id){ return document.getElementById(id); } function search_index(value){ var val=getX(arr,value) $$("val").innerHTML=val; } function getX(Arr,x){ var count=0; console.log("循环执行了:"); for(var i=0; i<Arr.length;i++){ count++ console.log(count);//输出循环执行的次数 if(Arr[i]==x){ return i; } } return "该值不存在"; } </script> </body> </html>
2. Binary search
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>二分查找温故</title> </head> <body> <script> //二分查找值适用于已经排好序的数组中 //二分就是逢中查找 步骤较少 var arr=[-13,2,4,6,8,12,34,35,45,56,57,88,110,234,239,342];//有序数组 function binarySearch(arr,x){ var low=0,high=arr.length-1; var count=0; while(low<=high){ count++; console.log("这是第"+count+"次循环"); var mid=Math.floor((low+high)/2); if(arr[mid]==x){ console.log("x所在数组内的引索是:"+mid); return mid; } if(arr[mid]<x){//如果要查找的值大于二分值则low=mid+1; low=mid+1; console.log("此时low的值是:"+low); }else{ high=mid-1;//如果要查找的值小于二分值则high=mid-1; console.log("此时high的值是:"+high); } } } binarySearch(arr,45); </script> </body> </html>
3. Bubble sort
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>javascript冒泡排序</title> </head> <body> <script> var arr=new Array(34,-3,43,67,12,44,21,34,5,645,64,3,43,23,25); function bubbleSort(){ var temp;//声明一个缓存变量 var count_outer=0;//外层循环计数 var count_inner=0;//内层循环计数 for(var i=0; i<arr.length;i++){//第一层循环 count_outer++; console.log("这是外层循环的第"+count_outer+"次"); for(var j=arr.length;j>0;j--){//第二层循环 count_inner++; console.log("...................这是内层循环的第"+count_inner+"次"); if(arr[j-1]<arr[j-2]){//判断后面一值如果小于前面一值 temp=arr[j-2];//那么将前面的值存放在temp里面 arr[j-2]=arr[j-1];//然后将后面一直放在前面值的位置 arr[j-1]=temp;//在把temp里的值放在后面那个位置 } console.log(".......................................外层第"+count_outer+"次循环"+"内层第"+count_inner+"次循环"+"后的数组排序结果是"+arr) } } return "最终排序后的数组是:["+arr+"]....一共循环了"+count_inner+"次"; } console.log(bubbleSort()); //控制台输出 </script> </body> </html>
4. Factorial
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>阶乘</title> </head> <body> <script> //created in 2014-04-30 //factorial function function factorial(num){ if(num<=1){ return 1; }else{ return num*arguments.callee(num-1);//arguments 是一个类似数组的对象 包含函数中传入的参数 他有一个属性callee,它是一个指针 指向拥有这个arguments对象的函数也就是factorial } } var fac=factorial;//不带括号的函数名是一个指向该函数的指针 所有fac现在也指向这个阶乘函数 alert(fac(3));//6 </script> </body> </html>
5. Output odd and even number control
<html> <head> <title>只输出奇数或者偶数项</title> </head> <body> <script> var ck = true;//全局变量 function oddOreven(num) { //num为0或1 控制输出结果 是奇数还是偶数 for (var i = 0; i < 30; i++) { if (ck) { ck = false; //如果ck为true 让其等于false alert(i + num); } else { ck = true; } } } //调用 oddOreven(0); //偶数 oddOreven(1) //奇数 </script> </body> </html>
The above is the entire content of this article. I hope it can help everyone learn javascript programming better.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.
