“This is the 11th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

Hello, everyone, I touch fish small public, the real strong, will not complain, if you do not want to be looked down upon by others, you have to pay more than others ten times a hundred times more effort, to stand higher than others. The last article was about various ways to change the direction of this; Find the maximum length of a string that does not contain repeated characters.

Given a string

let arr="babad"
Copy the code

The length of the longest string bad is 3.

First error example

var len = 0 var arr2 = [] for(let i=0; i<arr.length; i++){ if(arr2.indexOf(arr[i]) === -1){ arr2.push(arr[i]) }else{ arr2.shift(arr[i]) continue } len = Math.max(len, Arr2. Length)//// takes the maximum length of arr2 in the loop and assigns it to len} console.log(len,"11111ress") // 2Copy the code

The print effect is as follows

The results printed here clearly don’t match

Let’s do the correct example again

var len = 0; i = 0; var arr2 = []; while (i < arr.length) { if (arr2.indexOf(arr[i]) === -1) { arr2.push(arr[i]); } else { arr2.shift(arr[i]); // Delete the first element from the array continue; } len = Math.max(len, arr2.length); // select the longest arr2 in the loop and assign len i++; } console.log(len); / / 3Copy the code

The print effect is as follows

Okay, so I have the correct print here

Why are all loops having different effects, here to summarize the following knowledge points

The break statement in both a for statement and a while statement permanently terminates its loop;
The continue statement differs in the two statements:

(1) In the for statement, the continue statement skips the rest of the loop body and goes straight back to the adjustment part;

(2) In the while, the adjustment part is part of the body of the loop, so continue will skip it.

Conclusion:

Well, this is the end of the article, welcome everyone (like + comment + attention) have a question can exchange with each other. I hope this article will be helpful to you and I hope you can support me more. Today is the 11th day of my participation in the first Wenwen Challenge 2022. Come on!