当前位置:网站首页>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
边栏推荐
- Ctfshow web entry information collection
- Cartoon: programmers don't repair computers!
- Detailed explanation of QT creator breakpoint debugger
- Misc Basic test method and knowledge points of CTF
- ionic cordova项目修改插件
- I collect multiple Oracle tables at the same time. After collecting for a while, I will report that Oracle's OGA memory is exceeded. Have you encountered it?
- Crud de MySQL
- Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)
- JS bright blind your eyes date selector
- [recruitment position] infrastructure software developer
猜你喜欢

Fr exercise topic - simple question

Bugku's Eval

Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology

Common PHP interview questions (1) (written PHP interview questions)
![[detailed explanation of Huawei machine test] character statistics and rearrangement](/img/0f/972cde8c749e7b53159c9d9975c9f5.png)
[detailed explanation of Huawei machine test] character statistics and rearrangement

Ctfshow web entry information collection

Bugku telnet
![P1451 calculate the number of cells / 1329: [example 8.2] cells](/img/c4/c62f3464608dbd6cf776c2cd7f07f3.png)
P1451 calculate the number of cells / 1329: [example 8.2] cells

"Sequelae" of the withdrawal of community group purchase from the city

CODING DevSecOps 助力金融企业跑出数字加速度
随机推荐
Bugku's Ah Da
Want to ask the big guy, is there any synchronization from Tencent cloud Mysql to other places? Binlog saved by Tencent cloud MySQL on cos
Ctfshow web entry information collection
Creation and use of thymeleaf template
ICML 2022 | explore the best architecture and training method of language model
Crud of MySQL
queryRunner. Query method
easyOCR 字符识别
[C question set] of Ⅷ
Huawei Hubble incarnation hard technology IPO harvester
Talking about how dataset and dataloader call when loading data__ getitem__ () function
我这边同时采集多个oracle表,采集一会以后,会报oracle的oga内存超出,大家有没有遇到的?
当代人的水焦虑:好水究竟在哪里?
社区团购撤城“后遗症”
I collect multiple Oracle tables at the same time. After collecting for a while, I will report that Oracle's OGA memory is exceeded. Have you encountered it?
How can the boss choose programmers to help me with development?
Go learning ----- relevant knowledge of JWT
机器学习笔记 - 灰狼优化
Stop B makes short videos, learns Tiktok to die, learns YouTube to live?
Using tensorboard to visualize the training process in pytoch