Introducing a small case for getting started with JS
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!

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

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
