How to determine palindrome string in javascript (example analysis)
The content of this article is about how JavaScript determines palindrome strings (example analysis). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Paindromes(Paindromes), in Chinese, means that the backward and forward readings are the same, and symmetrical, such as "Shanghai tap water comes from the sea"; in English, It refers to words that are the same when viewed frontally and backwards, such as "madam"; and for numbers, also called palindromes, they refer to a symmetrical number like "16461", that is, the digits of this number are in reverse order. The number obtained after rearranging the order is the same as the original number.
Problem description
Judge the given string. If the string is a Palindromes, then return true
, otherwise return false
.
Implementation method
1. reverse()
function Palindromes(str) { let reg = /[\W_]/g; // \w 匹配所有字母和数字以及下划线; \W与之相反; [\W_] 表示匹配下划线或者所有非字母非数字中的任意一个;/g全局匹配 let newStr = str.replace(reg, '').toLowerCase(); let reverseStr = newStr.split('').reverse().join('') return reverseStr === newStr; // 与 newStr 对比 }
Actually, many steps are done here to operate the array, converting characters to arrays, flipping arrays and then converting to strings, so here Performance isn't great either. It is thought that the array is a reference type. To change the array, a new heap address space needs to be opened.
2. for loop
function Palindromes(str) { let reg = /[\W_]/g; let newStr = str.replace(reg, '').toLowerCase(); for(let i = 0, len = Math.floor(newStr.length / 2); i
Writing method 2
function Palindromes(str) { let reg = /[\W_]/g; let newStr = str.replace(reg, '').toLowerCase(); let len = newStr.length; for(let i = 0, j = len - 1; i <h4 id="Recursion">3. Recursion</h4><pre class="brush:php;toolbar:false">function palin(str) { let reg = /[\W_]/g; let newStr = str.replace(reg, '').toLowerCase(); let len = newStr.length; while(len >= 1) { console.log('--') if(newStr[0] != newStr[len - 1]) { // len = 0; // 为了终止 while 循环 否则会陷入死循环 return false; } else { return palin(newStr.slice(1, len - 1)); // 截掉收尾字符 再次比较收尾字符是否相等 // 直到字符串剩下一个字符(奇数项)或者 0 个字符(偶数项) } } return true; }
Give the question Add another requirement
For a given string, you can delete at most one character to determine whether it is still a palindrome.
Set a variable flag. When the pair of characters on both sides are found to be different for the first time, the comparison can be continued; if differences are found in subsequent comparisons, the result will be returned immediately.
function palin(str) { let flag = false; // 第一次不同可允许 let len = str.length; for(let [i, j] = [0, len - 1]; i <p>Related recommendations: <br></p><p><a href="http://www.php.cn/php-notebook-178476.html" target="_self">PHP determines whether a string is a palindrome string</a><br></p><p><a href="http://www.php.cn/js-tutorial-8061.html" target="_self">JavaScript search String and return search results to a string method_javascript skills</a></p>
The above is the detailed content of How to determine palindrome string in javascript (example analysis). For more information, please follow other related articles on the PHP Chinese website!

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools
