Home Web Front-end JS Tutorial Interpret the usage of For, While and recursion in JavaScript_Basic knowledge

Interpret the usage of For, While and recursion in JavaScript_Basic knowledge

May 16, 2016 pm 05:34 PM
for javascript while

for loop:

Copy code The code is as follows:

for(i=start; i<end; i ) {

}


while loop: (Note, if the condition is always true, it will enter an infinite loop and the browser will hang)
Copy code The code is as follows:

while (condition) {
//do something;
//change condition;
}

Recursion:

Use for loop to do substring
Copy code The code is as follows:

function substring(all, start, end) {
for(i=start; i<=end; i) {
console.log( all[i]);
}

substring("eclipse", 1, 4); //clip


Use recursion to implement substring
Copy code The code is as follows:

function substring(all, start, end) {
if(start >= end) {
return all[start];
}
else {
return all[start] substring(all, start 1, end);
}

substring("eclipse", 1, 4); //clip


Use for loop to access object properties:

For arrays and strings, we use index [] to access specific values; for objects, we also use [], but we will use a special variable: propertyName

Copy code The code is as follows:

var person = {
name: "Morgan Jones",
telephone: "(650) 777 - 7777",
email: "morgan.jones@example.com"
};

for (var propertyName in person) {
console.log(propertyName ":" person[propertyName]);
}


Use a for loop to find the array Data within:
Copy code The code is as follows:

var table = [
["Person", "Age", "City"],
["Sue", 22, "San Francisco"],
["Joe", 45, "Halifax"]
] ;

var i;
var rows=table.length;
for (r=0;r<rows;r ) {
var c;
var cells = table[r].length ;
var rowText = "";
for (c=0;c<cells;c ) {
rowText = table[r][c];
if (c < cells-1 ) {
                                                                                                                                                  🎜>Person Age City

Sue 22 San Francisco

Joe 45 Halifax

-------------------------------------------------- ----------------------------------

break:

Use break to exit the loop immediately, suitable for for and while loops.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

17 ways to solve the kernel_security_check_failure blue screen 17 ways to solve the kernel_security_check_failure blue screen Feb 12, 2024 pm 08:51 PM

17 ways to solve the kernel_security_check_failure blue screen

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to implement an online speech recognition system using WebSocket and JavaScript

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to implement an online reservation system using WebSocket and JavaScript

How to uninstall Skype for Business on Win10? How to completely uninstall Skype on your computer How to uninstall Skype for Business on Win10? How to completely uninstall Skype on your computer Feb 13, 2024 pm 12:30 PM

How to uninstall Skype for Business on Win10? How to completely uninstall Skype on your computer

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

How to use JavaScript and WebSocket to implement a real-time online ordering system

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

Simple JavaScript Tutorial: How to Get HTTP Status Code

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecasting system

See all articles