


Interpret the usage of For, While and recursion in JavaScript_Basic knowledge
May 16, 2016 pm 05:34 PMfor loop:
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)
while (condition) {
//do something;
//change condition;
}
Recursion:
Use for loop to do substring
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
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
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:
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.

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

17 ways to solve the kernel_security_check_failure blue screen

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

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

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 use JavaScript and WebSocket to implement a real-time online ordering system

Simple JavaScript Tutorial: How to Get HTTP Status Code

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