js练习2 --- 日期、正则、prototype、字符串

目录 

第一题

题目:创建一脚本显示当前时间和日期,并根据当前时间和日期计算出七天后的时间和日期。

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
  <div id="cDate"></div>
  <div id="fDate"></div>
    <script type="text/javascript"> 
        setInterval(function(){   
          let today = new Date();      
          let year = today.getFullYear();     //获取当前年份   
          let mon = today.getMonth()+1;       //获取当前月份   
          let day = today.getDate();          //获取当前月份中的第几天
          let h = today.getHours();           //获取小时
          let m = today.getMinutes()<10?'0'+today.getMinutes():today.getMinutes();//获取分
          let s = today.getSeconds()<10?'0'+today.getSeconds():today.getSeconds(); //获取秒
          let cdate = document.getElementById('cDate');
          cdate.innerHTML='当前时间: '+year+'年'+mon+'月'+day+'日'+'  '+h+':'+m+':'+s;
          
          let future = new Date();
          future.setDate(today.getDate() + 7);
          let year2 = future.getFullYear();  
          let mon2 = future.getMonth()+1; 
          let day2 = future.getDate(); 
          let fdate = document.getElementById('fDate');
          fdate.innerHTML='七天后: '+year2+'年'+mon2+'月'+day2+'日'+'  '+h+':'+m+':'+s;
        },1000)
    </script>
</body>
</html>

第二题

题目:创建一个脚本显示当天到2022-10-20之间的天数。

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
  <div id="Date"></div>
    <script type="text/javascript"> 
      let cur = Date.parse(new Date());         //当前时间
      let target = Date.parse("2022-10-20");
      let days = Math.ceil((target - cur) / (24*3600*1000));
      let date = document.getElementById('Date');
      date.innerHTML='距离2022-10-20还有: '+ days +'天';
    </script>
</body>
</html>

题目:创建一个函数,利用正则化表达式,把“aaa-bbb-cc”这种形式的命名转换为“aaaBbbCc”。

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
  <script type="text/javascript"> 
    function transform(str) {
      let newArr = "";
      let arr = str.split('-');
      for(let i = 0; i < arr.length; i++) {
        if(i == 0) {
          newArr += arr[0];
        } else {
          newArr += arr[i].substring(0,1).toUpperCase() + arr[i].substring(1);
        }
      }
      return newArr;
    }
    console.log(transform("aaa-bbb-cc"));
  </script>
</body>
</html>

题目:求斐波那契数列,利用callee属性。

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
  <script type="text/javascript"> 
    function fibonacci(num) {
      if(num == 0 || num == 1) {
        return 1;
      } else {
        return arguments.callee(num-1)+arguments.callee(num-2);
      }
    }
    //打印斐波那契数列的前10个
    for(let i = 0; i < 10; i++) {
      console.log(fibonacci(i));
    }
  </script>
</body>
</html>

题目:为String对象扩张一个check方法,用来检测字符串中是否包含指定的特殊字符。

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
  <script type="text/javascript">
    //定义check函数检测的特殊字符为"#"
    String.prototype.check = function() {
      return this.indexOf('#') == -1 ? false : true;
    }
    console.log("cdvvfsd#cdfv".check());
    console.log("cdvvfsdcdfv".check());
  </script>
</body>
</html>

题目:为String对象定义了一个contains0方法,用于判断字符串是否包含子字符串。

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
  <script type="text/javascript">
    String.prototype.contains = function(subStr) {
      return this.indexOf(subStr) == -1 ? false : true;
    }
    console.log("abbbcdddd".contains("bbc"));
    console.log("abbbcdddd".contains("cnn"));
  </script>
</body>
</html>

题目:自定义加密和解密方法。利用String对象charCodeAt)方法获取字符的Unicode编码,再利用fromCharCode()方法进行解码。

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
  <script type="text/javascript">
    //加密函数
    function encrypt(str) {
      let newStr = "";
      for(let i = 0; i < str.length; i++) {
        newStr += String.fromCharCode(str.charCodeAt(i)+str.length);
      }
      return newStr;
    }
    //解密函数
    function decrypt(str) {
      let newStr = "";
      for(let i = 0; i < str.length; i++) {
        newStr += String.fromCharCode(str.charCodeAt(i)-str.length);
      }
      return newStr;
    }
    let originStr = new String("Hello World!");
    let enStr = encrypt(originStr);
    let deStr = decrypt(enStr);
    console.log("原字符串:" + originStr);
    console.log("加密后:" + enStr);
    console.log("解密后:" + deStr);
  </script>
</body>
</html>

题目:对下一页的一段文章,练习String对象的search和replace方法以及正则表达式。比如:查找文章中所有的“NBA”,并且替换为National Basketball Association。

Dirk Werner Nowitzki(born June 19,1978)is a German former professional basketball player.An alumnus of Rontgen Gymnasium and the DJK Wurzburg basketball club, Nowitzki was chosen as the ninth pick in the 1998 NBA draft by the Milwaukee Bucks and was immediately traded to the Dallas Mavericks, where he had played his entire 21year National Basketball Association (NBA) career.In the NBA, he won the league Most Valuable Player(MVP) award in 2007, was an NBA champion in 2011, and was a 14-time All-Star. 

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
  <script type="text/javascript">
    let article = "Dirk Werner Nowitzki(born June 19,1978)is a German former professional basketball player.An alumnus of Rontgen Gymnasium "
    + "and the DJK Wurzburg basketball club, Nowitzki was chosen as the ninth pick in the 1998 NBA draft by the Milwaukee  Bucks and was "
    +"immediately traded to the Dallas Mavericks, where he had played his entire 21-year National Basketball Association (NBA) career."
    +"In the NBA, he won the league Most Valuable Player(MVP) award in 2007, was an NBA champion in 2011, and was a 14-time All-Star.";
    //第一个"NBA"匹配项的索引位置
    console.log(article.search("NBA"));    
    //查找文章中所有的“NBA”,并且替换为National Basketball Association
    console.log(article.replace(/NBA/g,"National Basketball Association"));
  </script>
</body>
</html>
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

漂流の少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值