JavaScript "typeof" and "in"
2014-08-09 00:43
381 查看
Q:There is a house with 4 levels. In that house there is an elevator. You can program this elevator to go up or down, depending on what button the user touches inside the elevator.
levels can be only numbers: 0,1,2,3 buttons can be strings: '0','1','2','3' possible return values are numbers: -3,-2,-1,0,1,2,3
If the elevator is on the ground floor(0th level) and the user touches button '2' the elevator must go 2 levels up, so our function must return 2.
If the elevator is on the 3rd level and the user touches button '0' the elevator must go 3 levels down, so our function must return -3.
If the elevator is on the 2nd level, and the user touches button '2' the elevator must remain on the same level, so we return 0.
We cannot endanger the lives of our passengers, so if we get erronous inputs, our elevator must remain on the same level. So for example goto(2,'4') must return 0, because there is no button '4' in the elevator. goto(4,'0') must return 0, because there
is no level 4. goto(3,undefined) must return 0. goto(undefined,'2') must return 0. goto([],'2') must return 0 because the type of the input level is array instead of a number. goto(3,{}) must return 0 because the type of the input button is object instead
of a number.
MyAnswer:
levels can be only numbers: 0,1,2,3 buttons can be strings: '0','1','2','3' possible return values are numbers: -3,-2,-1,0,1,2,3
If the elevator is on the ground floor(0th level) and the user touches button '2' the elevator must go 2 levels up, so our function must return 2.
If the elevator is on the 3rd level and the user touches button '0' the elevator must go 3 levels down, so our function must return -3.
If the elevator is on the 2nd level, and the user touches button '2' the elevator must remain on the same level, so we return 0.
We cannot endanger the lives of our passengers, so if we get erronous inputs, our elevator must remain on the same level. So for example goto(2,'4') must return 0, because there is no button '4' in the elevator. goto(4,'0') must return 0, because there
is no level 4. goto(3,undefined) must return 0. goto(undefined,'2') must return 0. goto([],'2') must return 0 because the type of the input level is array instead of a number. goto(3,{}) must return 0 because the type of the input button is object instead
of a number.
MyAnswer:
function goto(level,button){ if(typeof(level)==="number"&&typeof(button)==="string"){ if(level>3||level<-3||parseInt(button)>3||parseInt(button)<-3){ return 0; }else{ return parseInt(button)-level; } }else{ return 0; }Master answer:
function goto(level,button){ return level in [0,1,2,3] && button in [0,1,2,3] ? button - level : 0 }
相关文章推荐
- How do I get the name of an object's type in JavaScript
- vue项目中Error in render function: "TypeError: Cannot read property 'matched' of undefined"
- White space is required before the attribute type in the declaration of attribute "t" for element "b
- ORA-31603: object "orders" of type TABLE not found in schema "SYSTEM"
- Android RecyclerView的案例讲述和"The hierarchy of the type ViewHolder is inconsistent"错误解决
- Exception in thread "main" org.hibernate.TypeMismatchException: Provided id of the wrong type
- The solution of html <input type = "file "> in webview not work
- vuejs路由使用的问题Error in render function: "TypeError: Cannot read property 'matched' of undefined"
- [J2ME Q&A]MMAPI"Cannot parse this type of AMR"异常之讨论
- 通过 composition模拟 "has-a"或 "is-implemented-in-terms-of"
- 打开一个VC工程提示"Not all of the windows in the workspace could be opened."
- Use javascript to detect the type of the file
- [J2ME Q&A]MMAPI"Cannot parse this type of AMR"异常之讨论
- Equivalent typeof in MC++
- [20081201]--limit the length of textarea in JavaScript
- conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
- nested exception is java.sql.SQLException: Unknown type '246 in column 1 of 3 in binary-encoded result set. 问题
- [转]Prize winner in Competition "Best C++/MFC article of May 2007"
- [转]Prize winner in Competition "Best C++/MFC article of May 2007"
- 打开一个VC工程提示"Not all of the windows in the workspace could be opened."