

A quiz about type-coercion, indexOf return values, and the principle of lol whoo...
source link: https://miketaylr.com/posts/2014/03/a-quiz.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

A quiz about type-coercion, indexOf return values, and the principle of lol whoops a bug.
03 Mar 2014
Given the following version
[method][code], and assuming a.User.device()
returns the string "android"
, what is the return value assuming navigator.userAgent
is:
Mozilla/5.0 (Android; Mobile; rv:30.0) Gecko/30.0 Firefox/30.0
?
version: function() {
agent = window.navigator.userAgent;
switch (a.User.device()) {
...
case "android":
return window.Number(agent.substr(agent.indexOf("Android ") + 8, 3));
...
}
}
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The answer is NaN
, obviously.
[code]: https://gist.github.com/miketaylr/9333150#file-mobile-min-js-L21214-L21226da
Recommend
-
54
Variable values have certain types. In fact, you can cast a value of one type to the other. If you do it explicitly, it is type casting (also called explicit coercion). If it happens in the background when you are trying to perform an operation...
-
33
#方法一:使用string.Contains方法 string.Contains是大小写敏感的,如果要用该方法来判断一个string是否包含某个关键字keyword,需要把这个string和这个key
-
48
作者八路(企业代号名),目前负责贝壳找房商业化方向的前端工作。 提到常见的字符串匹配算法,一般来说我们会想到一个是朴素算法(...
-
26
简介 最近做项目的时候,发现无论是前端还是后端,indexOf出现的概率都非常频繁,今天我们来看下他的实现原理吧! indexOf的含义 : 给定一个字符串去匹配另一个字符串的下标,如...
-
12
Copy link Contributor FabianWolff ...
-
2
IndexOf for chain space advertisements There is a space in the string, but when I run program, it returns -1, t...
-
2
go indexOf indexOf indexOf(s, p string) 函数会返回p在s中首次出现的位置,倘如没有则返回-1,本次使用之前提到过的kmp算法来作为indexOf的核心,并用来解决leetcode28题 ...
-
6
Golang strings.Index = JavaScript String indexOf() Theory and Practice ≡
-
6
indexOf 的 >=0 与 !==-1 问题 在一个项目中,别的同事用到了 indexOf,我一般判断都是写成 >=0,而他写作 !==-1...
-
4
April 6, 2023 /
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK