2021年3月17日星期三

confusion coming from the slice method in JavaScript involving a if statement

I was reading a JavaScript book and this example was shown...

var text = "Relics of World War II were found";  for (var i = 0; i < text.length; i++) {    if (text.slice(i, i + 12) === "World War II") {      text = text.slice(0, i) + "the Second World War" + text.slice(i + 12);    }  }  console.log(text);

I understood that the code's purpose is to find the banned segment(World War II) and replace it with "the Second World War". The thing that knocked me off guard was the use of (i) with (12) in the slice method. I wanted to know why 12 was put in there along side (i), I know "World War II" is 12 characters but what does it do?

Help is greatly appreciated!!

https://stackoverflow.com/questions/66666237/confusion-coming-from-the-slice-method-in-javascript-involving-a-if-statement March 17, 2021 at 10:38AM

没有评论:

发表评论