causeThe reason for writing this blog is that I was doing the daily question of Leetcode today. It is a question about converting a string to an integer (atoi). If you are interested, you can click on the title to see the detailed description. After I debugged it many times and finally submitted it successfully, I went to the comment section to see the experts' solutions. After reading it, I couldn't help but sigh that parseInt() in JavaScript is so excellent. This tells me a truth: the APIs that we think we are very familiar with may not be truly mastered by us. ” My answer/** * @param {string} str * @return {number} */ var myAtoi = function(str) { str = str.trim(); //Intercept the first character of the string var firstChar = str.charAt(),res = '',regSymbol = /^[\-\+]{1}/,regNumber = /^[0-9]{1}/; if(!regSymbol.test(firstChar) && !regNumber.test(firstChar)){ return 0; } //The first character is + or - if(regSymbol.test(firstChar)){ var sliceStr = str.slice(1,str.length); for(var i=0;i<sliceStr.length;i++){ if (regNumber.test(sliceStr.charAt(i))) { res += sliceStr.charAt(i); }else{ break; } } (res.length) ? res = parseInt(firstChar + res) : res = 0; if(firstChar == '-' && res < Math.pow(-2,31)){ res = Math.pow(-2,31); } if(firstChar == '+' && res > Math.pow(2,31) - 1){ res = Math.pow(2,31) - 1; } return res; } //The first character is a number if (regNumber.test(firstChar)) { var sliceStr = str; for(var i=0;i<sliceStr.length;i++){ if (regNumber.test(sliceStr.charAt(i))) { res += sliceStr.charAt(i); }else{ break; } } if (parseInt(res) > Math.pow(2,31) - 1) { res = Math.pow(2,31) - 1; } return res; } }; The answer from the boss/** * @param {string} str * @return {number} */ var myAtoi = function(str) { const number = parseInt(str, 10); if(isNaN(number)) { return 0; } else if (number < Math.pow(-2, 31) || number > Math.pow(2, 31) - 1) { return number < Math.pow(-2, 31) ? Math.pow(-2, 31) : Math.pow(2, 31) - 1; } else { return number; } }; By comparison, we know that parseInt() in JavaScript has performed a series of operations such as intercepting empty strings, judging the first letter, and returning number type results. It just happens to be a perfect match for today's daily question. It's so cool to use parseInt() to solve this problem. This is the end of this article about the wonderful uses of parseInt() in JavaScript. For more relevant JavaScript parseInt() content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to implement Linux deepin to delete redundant kernels
>>: How to find out uncommitted transaction information in MySQL
This article shares the installation steps of MyS...
This article shares the specific code of Vue to i...
1. --cpu=<value> 1) Specify how much availa...
What is volume? Volume means capacity in English,...
Table of contents Preface What situations can cau...
Table of contents Preface The relationship betwee...
Problem description (environment: windows7, MySql...
Table of contents First, configure package.json T...
Summary: In order to make your web page look more...
Preface CSS grids are usually bundled in various ...
Learning to use CSS3's 3D effects to create a...
The previous article introduced the installation ...
This article shares MYSQL logs and backup and res...
Add the following code to the CSS style of the el...
Due to the needs of the project, I plan to study ...