JS篇 -- Types Exercise

今天这一篇主要po一下几道关于types的练习题及答案

1. Object

Question Description:

Polyfill for Object.is(..)

In this exercise, you will define a polyfill for Object.is(..). No cheating and looking it up online!

Instructions

  1. Object.is(..) should take two parameters.

  2. It should return true if the passed in parameters are exactly the same value (not just === – see below!), or false otherwise.

  3. For NaN testing, you can use Number.isNaN(..), but first see if you can find a way to test without usage of any utility?

  4. For -0 testing, no built-in utility exists, but here’s a hint: -Infinity.

  5. If the parameters are any other values, just test them for strict equality.

  6. You cannot use the built-in Object.is(..) – that’s cheating!

Polyfill Pattern

NOTE: Since your JS environment probably already has Object.is(..), to test your polyfill you’ll have to first unconditionally define it (no if guard), and then add the if guard when you’re done.

To define a polyfill, it looks like this:

if (!Object.is) {
	Object.is = function ObjectIs(..) { .. };
}

Answer

if(!Object.is){
    Object.is = function ObjectIs(x,y){
        var xNegZero = isItNegZero(x);
        var yNegZero = isItNegZero(y);
        if(xNegZero || yNegZero){
            return xNegZero && yNegZero;
        }

        if(isItItNaN(x) && isItItNaN(y)){
            return true;
        } 
        else{
            return x===y;
        }

        function isItNegZero(v){
            if(v===0 && (1/v) == - Infinity){
                return true;
            } else{
                return false;
            }
        }

        function isItItNaN(k){
            if(k!==k){
                return true;
            }else {
                return false;
            }
        }

    }
}

// tests:
console.log(Object.is(42,42) === true);
console.log(Object.is("foo","foo") === true);
console.log(Object.is(false,false) === true);
console.log(Object.is(null,null) === true);
console.log(Object.is(undefined,undefined) === true);
console.log(Object.is(NaN,NaN) === true);
console.log(Object.is(-0,-0) === true);
console.log(Object.is(0,0) === true);

console.log(Object.is(-0,0) === false);
console.log(Object.is(0,-0) === false);
console.log(Object.is(0,NaN) === false);
console.log(Object.is(NaN,0) === false);
console.log(Object.is(42,"42") === false);
console.log(Object.is("42",42) === false);
console.log(Object.is("foo","bar") === false);
console.log(Object.is(false,true) === false);
console.log(Object.is(null,undefined) === false);
console.log(Object.is(undefined,null) === false);

2. Coecion

Question Description:

Working With Coercion

In this exercise, you will define some validation functions that check user inputs (such as from DOM elements). You’ll need to properly handle the coercions of the various value types.

Instructions

  1. Define an isValidName(..) validator that takes one parameter, name. The validator returns true if all the following match the parameter (false otherwise):

    • must be a string
    • must be non-empty
    • must contain non-whitespace of at least 3 characters
  2. Define an hoursAttended(..) validator that takes two parameters, attended and length. The validator returns true if all the following match the two parameters (false otherwise):

    • either parameter may only be a string or number
    • both parameters should be treated as numbers
    • both numbers must be 0 or higher
    • both numbers must be whole numbers
    • attended must be less than or equal to length

Answer

function isValidName(name){
    if(typeof name=='string' && name.length>=3 && name){
        return true
    }else {
        return false
    }
}

function hoursAttended(attended,length){
    if(typeof attended == 'string' && 
       attended.trim() !=''){
            attended = Number(attended);
        }
    if(typeof length == 'string'&& length.trim()!=''){
        length = Number(length);
        }
    if(typeof attended =='number' 
    && length =='number'
    && attended.length>=0
    && length.length>=0
    && attended.length <= length.length){
        return true
    }
    else {
        return false
    }

}

// tests:
console.log(isValidName("Frank") === true);
console.log(hoursAttended(6,10) === true);
console.log(hoursAttended(6,"10") === true);
console.log(hoursAttended("6",10) === true);
console.log(hoursAttended("6","10") === true);

console.log(isValidName(false) === false);
console.log(isValidName(null) === false);
console.log(isValidName(undefined) === false);
console.log(isValidName("") === false);
console.log(isValidName("  \t\n") === false);
console.log(isValidName("X") === false);
console.log(hoursAttended("",6) === false);
console.log(hoursAttended(6,"") === false);
console.log(hoursAttended("","") === false);
console.log(hoursAttended("foo",6) === false);
console.log(hoursAttended(6,"foo") === false);
console.log(hoursAttended("foo","bar") === false);
console.log(hoursAttended(null,null) === false);
console.log(hoursAttended(null,undefined) === false);
console.log(hoursAttended(undefined,null) === false);
console.log(hoursAttended(undefined,undefined) === false);
console.log(hoursAttended(false,false) === false);
console.log(hoursAttended(false,true) === false);
console.log(hoursAttended(true,false) === false);
console.log(hoursAttended(true,true) === false);
console.log(hoursAttended(10,6) === false);
console.log(hoursAttended(10,"6") === false);
console.log(hoursAttended("10",6) === false);
console.log(hoursAttended("10","6") === false);
console.log(hoursAttended(6,10.1) === false);
console.log(hoursAttended(6.1,10) === false);
console.log(hoursAttended(6,"10.1") === false);
console.log(hoursAttended("6.1",10) === false);
console.log(hoursAttended("6.1","10.1") === false);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值