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!

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
