parseint

Why does parseInt('dsff66',16) return 13?

牧云@^-^@ 提交于 2019-12-03 10:31:31
today I stumbled on a strange (in my opinion) case in JavaScript. I passed a non-hexadecimal string to the parseInt function with the base of 16 and...I got the result. I would expect the function to throw some kind of exception or at least return NaN, but it succeeded parsing it and returned an int. My call was: var parsed = parseInt('dsff66', 16); // note the 's' in the first argument document.write(parsed); and the result was: 13 . I noticed that it "stops" parsing with the first character that doesn't belong to the numeral system specified in the 2nd argument, so calling parseInt('fg',16)

vue简易计算器

不打扰是莪最后的温柔 提交于 2019-12-03 10:29:07
一、代码实现 <body> <div id="app"> <input type="text" v-model="n1"> <select v-model="opt"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> <input type="text" v-model="n2"> <input type="button" value="=" @click="calc"> <input type="text" v-model="result"> </div> <script> // 创建 Vue 实例,得到 ViewModel var vm = new Vue({ el: '#app', data: { n1: 0, n2: 0, result: 0, opt: '+' }, methods: { calc() { // 计算器算数的方法 // 逻辑: /* switch (this.opt) { case '+': this.result = parseInt(this.n1) + parseInt(this.n2) break; case '-': this.result =

TypeScript: error when using parseInt() on a number

匿名 (未验证) 提交于 2019-12-03 08:36:05
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: The JavaScript function parseInt can be used to force conversion of a given parameter to an integer, whether that parameter is a string, float number, number, etc. In JavaScript, parseInt(1.2) would yield 1 with no errors, however, in TypeScript, it throws an error during compilation saying: error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'. Am I missing something here or is it an expected behaviour from TypeScript? 回答1: Don't use parseInt to do this operation -- use Math.floor . Using parseInt to floor

How to increment / decrement hex color values with javascript / jQuery

匿名 (未验证) 提交于 2019-12-03 08:28:06
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is it possible to increment or decrement hex color values on a step-by-step basis in jQuery / javascript? What I would like to do is something like this: Adapting from a for-loop like for (var i = 0; i <= 100; i++) { console.log(i); } I would like to do something like for (var color = 000000; color <= ffffff; color++) { console.log(color); } without any conversion. Is that possible? I've allready tried this: for (var color = parseInt('000000', 16); color <= parseInt('ffffff', 16); color++){ console.log(color.toString(16)); } and it works but

正则相关应用

十年热恋 提交于 2019-12-03 04:25:31
/** * 重写js native toFixed 方法 * @param d * @returns { string | number } */ export function toFixed(d) { let s = this + '' if (!d) d = 0 d = parseInt(d) if (s.indexOf('.') == -1) s += '.' s += new Array(d + 1).join('0') if (new RegExp('^(-|\\+)?(\\d+(\\.\\d{0,' + (d + 1) + '})?)\\d*$').test(s)) { s = '0' + RegExp.$2 let pm = RegExp.$1 let a = RegExp.$3.length let b = true if (a == d + 2) { a = s.match(/\d/g) if ((pm !== '-' && parseInt(a[a.length - 1]) > 4) || (pm === '-' && parseInt(a[a.length - 1]) > 5)) { for (var i = a.length - 2; i >= 0; i--) { a[i] = parseInt(a[i]) + 1 if (a[i] == 10) { a

Why doesn't backgroundColor=rgb(a,b,c) work?

匿名 (未验证) 提交于 2019-12-03 03:04:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: <html> <head> <title> Colors </title> </head> <body> <script type = "text/javascript" > var a = parseInt ( prompt ( "Enter R" )); var b = parseInt ( prompt ( "Enter G" )); var c = parseInt ( prompt ( "Enter B" )); document . body . style . backgroundColor = rgb ( a , b , c ); </script> </body> </html> Why doesn't the background color change according to the RGB values? What have I done wrong?? 回答1: You need to use quotes: document . body . style . backgroundColor = 'rgb(' + a + ',' + b + ',' + c + ')' ; JS Fiddle demo . Or:

Evaluate dice rolling notation strings

匿名 (未验证) 提交于 2019-12-03 02:44:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Rules Write a function that accepts string as a parameter, returning evaluated value of expression in dice notation , including addition and multiplication. To clear the things up, here comes EBNF definition of legal expressions: roll ::= [positive integer], "d", positive integer entity ::= roll | positive number expression ::= entity { [, whitespace], "+"|"*"[, whitespace], entity } Example inputs: "3d6 + 12" "4*d12 + 3" "d100" Using eval functions, or similar, is not forbidden, but I encourage to solving without using these. Re-entrancy is

parseInt(“010”, 10); vs. parseInt(010, 10);

匿名 (未验证) 提交于 2019-12-03 02:33:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm confused about a particular result of parseInt . I thought I understood most of its quirks, but this one is unclear. parseInt("010", 10); // 10 parseInt(010, 10); // 8, but expecting 10 In the second example I specified the correct radix. What's the reason for the unexpected result? Solution The problem is with octal literal. When a number is prepended with 0, it's seen an as octal literal. If you execute console(010); // 8 in non-script mode, you will get 8. The reason that parseInt is having what I thought was strange behavior was

jQuery UI Resizable alsoResize reverse

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How to make the jQuery UI Resizable alsoResize reverse direction. suppose in the html there is two div tag is there, if i resize in upward means the other thing has to resize downward <script> $(function() { $("#resizable").resizable({alsoResize: ".myiframe"}); }); </script> <div id = "resizable"> This is the resizable content... </div> <div class="myframe"> This must resize in reverse direction... </div> i tried it but of no use please guide to solve this 回答1: By modifying the code jQuery uses to implement the alsoResize option, we can make

JavaScript seconds to time string with format hh:mm:ss

匿名 (未验证) 提交于 2019-12-03 02:03:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I want to convert a duration of time, i.e., number of seconds to colon-separated time string (hh:mm:ss) I found some useful answers here but they all talk about converting to x hours and x minutes format. So is there a tiny snippet that does this in jQuery or just raw JavaScript? 回答1: String.prototype.toHHMMSS = function () { var sec_num = parseInt(this, 10); // don't forget the second param var hours = Math.floor(sec_num / 3600); var minutes = Math.floor((sec_num - (hours * 3600)) / 60); var seconds = sec_num - (hours * 3600) - (minutes *