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.

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

Atom editor mac version download
The most popular open source editor
