【日常】JS练习题(一)

  1. 排序扑克牌(J,Q,K,A,2…,10,大王,小王) 从小到大排序,2算大的。
// 1. 排序扑克牌
//   (J,Q,K,A,2.....,10,大王,小王)	
//    从小到大排序,2算大的。

(function (cards) {
	var orderCards;
	//console.log(cards);

	for(let index in cards){
		if (cards[index] == 'J')  {cards[index] = '11'}
		else if(cards[index] == 'Q') {cards[index] = '12'}
		else if(cards[index] == 'K') {cards[index] = '13'}
		else if(cards[index] == 'A') {cards[index] = '14'}
		else if(cards[index] == '2') {cards[index] = '15'}
		else if(cards[index] == 'Black Joker') {cards[index] = '16'}
		else if(cards[index] == 'Red Joker') {cards[index] = '17'}
		//console.log(index);
	}
	//console.log(cards);
	orderCards = cards.sort(function (i, j){return i - j});

	for(let index in orderCards){
		if (orderCards[index] == '11')  {orderCards[index] = 'J'}
		else if(orderCards[index] == '12') {orderCards[index] = 'Q'}
		else if(orderCards[index] == '13') {orderCards[index] = 'K'}
		else if(orderCards[index] == '14') {orderCards[index] = 'A'}
		else if(orderCards[index] == '15') {orderCards[index] = '2'}
		else if(orderCards[index] == '16') {orderCards[index] = 'Black Joker'}
		else if(orderCards[index] == '17') {orderCards[index] = 'Red Joker'}
	}

	console.log(orderCards);
})(['J','Q','K','A','2','3','4','5','6','7','8','9','10','Red Joker','Black Joker'])
  1. 写一个函数,函数原型如下
    function myRound( num , p )
    {
    // 写实现精确点小数点后几位的函数
    }
// 2. 写一个函数,函数原型如下
// function myRound( num , p )
// {
// 	//写实现精确点小数点后几位的函数
// }	
// var r = myRound( 34.567 , 2 ) ; == > 34.57
// var r = myRound( 34.5678 , 3 ) ; == > 34.568

function myRound(num, p) {
	// body...
	var num = new String(num);

	var index = (num.slice(num.indexOf('.'), num.length-1).length - p);

	return num.slice(0, num.length - index);	
}


var r = myRound( 34.5678 , 3 );
console.log(r);
  1. 写一个函数,函数原型如下
    function myRandom( num )
    {
    //写一个实现求随机整数的函数
    }
    var r = myRandom(100); ==> [0,100)
// 3. 写一个函数,函数原型如下
// function myRandom( num )
// {
// 	//写一个实现求随机整数的函数
// }
// var r = myRandom(100); ==> [0,100)

function myRandom( num ) {
	// body...
	return Math.floor(Math.random() * num);
} 

var r = myRandom(100);

console.log(r);
  1. 小明的爸爸要退休了,小明要继承爸爸的衣钵, 爸爸是打鱼的,爸爸说,要三天打鱼两天晒网。爸爸的退休时间为2015/2/3,请问小明今天在干什么?
// 4. 小明的爸爸要退休了,小明要继承爸爸的衣钵,
// 爸爸是打鱼的,爸爸说,要三天打鱼两天晒网。
// 爸爸的退休时间为2015/2/3,请问小明今天在干什么

(function () {
   // body..
   var milli = 1000 * 60 * 60 * 24;
   var dateBack = new Date(2015,2,3);
   var dataNow = new Date();
   var days = Math.floor((dataNow.getTime() -  dateBack.getTime()) / milli)

   if(days % 5 == 0 || days % 5 == 1 || days % 5 == 2){
   	console.log("该去打鱼啦");
   }
   else{
   	console.log("该去晒网啦");
   }
})();
  1. 小明是1999/2/5出生的,小明的妹妹于569天后出生,请问小明的妹妹几年几月几日出生?
// 5. 小明是1999/2/5出生的,小明的妹妹于569天后
// 出生,请问小明的妹妹几年几月几日出生

(function () {
	// body...
	var dateBro = new Date(1999,2,5);
	var dateSis = new Date();
	var milli = dateBro.getTime() + 569 * 1000 * 60 * 60 * 24;
	dateSis.setTime(milli);

	console.log("妹妹出生的日期为:" + 
		dateSis.getFullYear() + "/" + dateSis.getMonth() 
		+ "/" + dateSis.getDate());
	
})()
  1. 实现页面验证,需要撰写的验证函数如下:
    实现对身份证号码的验证:17个数字 + 1个数字|x|X
    实现对数字的验证:只能是小数,且是小数点2位。
    范例:23.568 ==> false
    45.6 ==> true
    45 ==> true
<!-- 
// 6. 实现页面验证,需要撰写的验证函数如下:
// 实现对身份证号码的验证:17个数字 + 1个数字|x|X
// 实现对数字的验证:只能是小数,且是小数点2位。
//  范例:23.568 ==> false
//      45.6 ==> true
//      45 ==> true 
-->
<!DOCTYPE html>
<html>
<head>
    <title>test</title>
</head>
<body>
<script >
    function check() {
        // body...
        var idcard = document.getElementById("idcard").value;
        if (idcard.length < 18) {alert("身份证号长度18位!")}

        var flag = true;
        for(let index in idcard){
            //alert(index);
            if (isNaN(idcard[index])) {
                //alert(index);
                if(index == idcard.length-1 && (idcard[index] == 'x' || idcard[index] == 'X')){
                    flag = true;
                }else{
                    flag = false;
                    break;
                }
            }
        }
        //alert("1111");
        if(flag){
            alert("身份证号验证成功!");
        }
        else{
            alert("身份证号不正确!")
        }
    }

    function checkNum() {
        // body...
        var num = document.getElementById("num").value;
        var index = num.indexOf('.');
        if(index > 0 && num.slice(index,num.length-1).length == 2){
            alert("数字验证成功!");
        }else{
            alert("数字验证不成功!");
        }
    }
    
</script>

<form onsubmit="check()">
    <table>
        <tr>
            <td>
                <input type="text" name="idcard" id="idcard">
            </td>
            <td>
                <input type="submit">
            </td>
        </tr>
        <tr>
            <td>
                <input type="text" name="num" id="num">
            </td>
            <td>
                <input type="button" onclick="checkNum()" value="验证">
            </td>
        </tr>
    </table>
</form>
</body>
</html>
  1. A说B说谎了,B说C说谎了,C说A和B都说谎了,请问谁说谎,谁没有说谎?
// 7. A说B说谎了,B说C说谎了,C说A和B都说谎了,
// 	请问谁说谎,谁没有说谎
(function () {
	// body...
	for(var a = 0; a <= 1; a++){
		for(var b = 0; b <= 1; b++){
			for(var c = 0; c <= 1; c++){
				if(((a&&!b)||(!a&&b)) && ((b&&!c)||(!b&&c)) &&
				 ((c&&!a&&!b)||(!c&&a&&b)||(!c&&!a&&b)||(!c&&a&&!b)) ){
					console.log(a,b,c);
				}
			}
		}
	}
})()
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值