当前位置:网站首页>Optional parameters in the for loop
Optional parameters in the for loop
2022-07-05 15:19:00 【I'll come to C】
const arr = [1, 2, 3]
for (let i = 0; i < arr.length; i++) {
console.log(i);
} In daily development , Many times you will use for loop , Do you know when for When the first parameter or the third parameter in the loop is not filled , What will it do ?
Situation 1 The first parameter is not filled in

var longestCommonPrefix = function(strs) {
if (strs.length === 0) return "";
let one = strs[0];
for (let i = 1; i < strs.length; i++) {
let j = 0;
// j To be less than strs The length of the string in the array starting from the second element
// And less than one, That is to say strs The length of the first element string
for (; j < strs[i].length && j < one.length; j++) {
if (strs[i][j] !== one[j]) break;
}
// the second for The first parameter of the loop is not filled , For the second for It can also be used outside the loop j Parameters
one = strs[i].substr(0, j);
if (one === "") return ""
}
return one;
};for The first parameter in the loop is not filled , In order to in for Outside of the loop , You can also use the first parameter
Situation two The third parameter is not filled in 
var romanToInt = function(s) {
const obj = {
I: 1,
IV: 4,
V: 5,
IX: 9,
X: 10,
XL: 40,
L: 50,
XC: 90,
C: 100,
CD: 400,
D: 500,
CM: 900,
M: 1000,
};
let result = 0;
for (let i = 0; i < s.length;) {
if (i + 1 < s.length && obj[s.substring(i, i + 2)]) {
result += obj[s.substring(i, i + 2)];
i += 2;
} else {
result += obj[s.substring(i, i + 1)];
i++;
}
}
return result;
};for The third parameter in the cycle is not filled , It's for different conditions , by for Cycle through different steps .
expand
substr and substring The difference between
const str = "123456789";
console.log(str.substr(2,5)); // 34567
console.log(str.substring(2,5)); // 345
console.log(str.slice(2, 5)); // 345
console.log(str); // 123456789
substr The second parameter is length
substring The second parameter is the index value
slice Be similar to substring, The second parameter is also the index value
边栏推荐
- Brief introduction of machine learning framework
- PHP high concurrency and large traffic solution (PHP interview theory question)
- sql server学习笔记
- R 熵权法计算权重及综合得分
- Super wow fast row, you are worth learning!
- 1330: [example 8.3] minimum steps
- Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment
- Jmeter性能测试:ServerAgent资源监控
- The elimination strategy of redis
- Detailed explanation of QT creator breakpoint debugger
猜你喜欢

P1451 求细胞数量/1329:【例8.2】细胞

CODING DevSecOps 助力金融企业跑出数字加速度

Number protection AXB function! (essence)

Common PHP interview questions (1) (written PHP interview questions)

Under the crisis of enterprise development, is digital transformation the future savior of enterprises

Run faster with go: use golang to serve machine learning
![P6183 [USACO10MAR] The Rock Game S](/img/f4/d8c8763c27385d759d117b515fbf0f.png)
P6183 [USACO10MAR] The Rock Game S

Database learning - Database Security

机器学习笔记 - 灰狼优化

Creation and use of thymeleaf template
随机推荐
GPS原始坐标转百度地图坐标(纯C代码)
Redis distributed lock principle and its implementation with PHP (1)
Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)
超越PaLM!北大碩士提出DiVeRSe,全面刷新NLP推理排行榜
Visual task scheduling & drag and drop | scalph data integration based on Apache seatunnel
超越PaLM!北大硕士提出DiVeRSe,全面刷新NLP推理排行榜
[C question set] of Ⅷ
How can I quickly check whether there is an error after FreeSurfer runs Recon all—— Core command tail redirection
How to paste the contents copied by the computer into mobaxterm? How to copy and paste
Mysql---- function
Mongdb learning notes
Redis' transaction mechanism
mapper. Comments in XML files
【jvm】运算指令
Talk about your understanding of microservices (PHP interview theory question)
PHP high concurrency and large traffic solution (PHP interview theory question)
CPU design related notes
亿咖通科技通过ISO27001与ISO21434安全管理体系认证
Photoshop plug-in action related concepts actionlist actiondescriptor actionlist action execution load call delete PS plug-in development
Huawei Hubble incarnation hard technology IPO harvester