How many days are calculated?
1, whether the year is a leap year, confirm the number of days in February
2, get the number of days in each month, which can be put in the array
3, get the number of days in the current month according to the month
4. The number of days obtained by adding 3 to the date is ok.
function isLeapYr(yr) { //判断闰年 return (yr % 4 === 0 && yr % 100 !== 0) || (yr % 100 === 0 && yr % 400 === 0); }function count(y, m, d) { var mdays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; var mSum = 0; var sum = 0; //如果是闰年的话,那么2月份就应该有29天 isLeapYr(y) ? mdays[1] = 29 : mdays[1]; //计算该月份之前的总天数,比如m=3,那么就计算1和2月的总天数 for (var i = 0; i < m - 1; i++) { mSum += mdays[i]; } //加上当月天数 sum = mSum + d; return sum; }
//弹出年、月、日输入框,声明年鱼儿,并赋值 var y =parseInt(prompt("请输入你的出生年份")); var m = parseInt(prompt("请输入你的出生月份")); var d =parseInt(prompt("请输入你的出生日期")); //月 //求各月份数字之和 var getMonth=new Array(31,28,31,30,31,30,31,31,30,31,30); var sum1=0,i; for(i=0;i<m-1;i++){ sum1+=getMonth[i] } //年 //判断年是否为闰年,是且大于2月份加一 if(( y%400 ==0||(y % 4 == 0&& y%100 !=0))&& m > 2){ sum=sum1 + d +1; document.write("该天为一年中的第"+sum+"天"); }else{ sum=sum1+d; document.write("该天为一年中的第"+sum+"天"); }
Use time function for calculation
var now = new Date();//输入日期以今日为例var NewYearsDay = new Date(now.getFullYear(), 0, 0, 0, 0, 0);//该年第一天console.log((now.getTime()-NewYearsDay.getTime())/86400000>>>0)//算出两者的时间戳之差就是时间差的微秒数 再用时间差除以天的微秒数86400000 取整 就是第几天
var endDate = new Date(y, m-1, d), startDate = new Date(y, 0, 0), days = (endDate - startDate) / 1000 / 60 / 60 / 24; document.write("该天为一年中的第"+ days +"天");
JS implementation of factorial
//while循环实现function calNum(n) { var product = 1; while(n > 1){//1*5*4*3*2,1*n*(n-1)*(n-2)*...*2 product *= n; n--; } return product; } console.log(calNum(5))
//for循环实现 function calNum(n){ var a = 1, str = '1*'; for (var i = 2; i <= n; i++) { str += i + '*'; a *= i; } str = str.substr(0,str.length-1); return str + '=' +a; } console.log(calNum(5));
Judge prime numbers
var prime = function(len){ var i,j; var arr = []; for(i = 1; i < len; i++){ for(j=2; j < i; j++){ if(i%j === 0) { break; } } if(i <= j && i !=1){ arr.push(i); } } return arr; };console.log(prime(100));
js Fibonacci sequence summation
Recursive algorithm
The time complexity is O(2^n), the space complexity is O(n)
function recurFib(n) { if (n < 2) { return n; } else { return recurFib(n-1) + recurFib(n-2); } } alert(recurFib(10));//将显示55
Dynamic programming
The time complexity is O( n), the space complexity is O(n)
function dynFib(n) { var res = [1,1]; if (n == 1 || n == 2) { return 1; } for (var i = 2; i < n; i++) { val[i] = val[i-1] + val[i-2]; } return val[n-1]; } alert(dynFib(10));//将显示55
Iteration method
The time complexity is O(n), the space complexity is O(1)
function iterFib(n){ var last=1; var nextlast=1; var result=1; for(var i=2;i<n;i++){ result=last+nextlast; nextlast=last; last=result; } return result; } alert(iterFib(10));//将显示55
Prime numbers
function foo(n){ var a=[],state=0; for(var i=2;i<n;i++){ var sqrt_i = Math.sqrt(i); if(i%sqrt_i===0){ continue; } for(var j=2;j<sqrt_i;j++){ if(i%j===0){ state=1; break; }else{ state=0; } } if(state===0){ a.push(i); } } console.log(a); } foo(100)
This article explains a small case for getting started with JS. For more related content, please pay attention to the php Chinese website.
Related recommendations:
Achieving dynamic display of processes through js
##particlesJS usage introduction related content
Detailed analysis of operators i and i in JS
The above is the detailed content of Introducing a small case for getting started with JS. For more information, please follow other related articles on the PHP Chinese website!

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

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.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.


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

Atom editor mac version download
The most popular open source editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment
