Home > Web Front-end > JS Tutorial > Introducing a small case for getting started with JS

Introducing a small case for getting started with JS

jacklove
Release: 2018-06-15 15:23:15
Original
1614 people have browsed it

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;
}
Copy after login
 //弹出年、月、日输入框,声明年鱼儿,并赋值
    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+"天");
        }
Copy after login

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 取整 就是第几天
Copy after login
var endDate = new Date(y, m-1, d),
    startDate = new Date(y, 0, 0),
    days = (endDate - startDate) / 1000 / 60 / 60 / 24;
document.write("该天为一年中的第"+ days +"天");
Copy after login

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))
Copy after login
//for循环实现
   function calNum(n){
        var a = 1, str = &#39;1*&#39;;        for (var i = 2; i <= n; i++) {            str += i + &#39;*&#39;;
            a *= i;
        }        str = str.substr(0,str.length-1);        return str + &#39;=&#39; +a;
    }
    console.log(calNum(5));
Copy after login

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));
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template