今天这一篇主要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
-
Object.is(..)
should take two parameters. -
It should return
true
if the passed in parameters are exactly the same value (not just===
– see below!), orfalse
otherwise. -
For
NaN
testing, you can useNumber.isNaN(..)
, but first see if you can find a way to test without usage of any utility? -
For
-0
testing, no built-in utility exists, but here’s a hint:-Infinity
. -
If the parameters are any other values, just test them for strict equality.
-
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
-
Define an
isValidName(..)
validator that takes one parameter,name
. The validator returnstrue
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
-
Define an
hoursAttended(..)
validator that takes two parameters,attended
andlength
. The validator returnstrue
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 tolength
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);