Run IndexOf more than one time JS

Fiera28

How to search multiple same values and look if it meets the conditions without regex? Suppose that I have this string ***6**5****8*9***2 What I need to do is to check if the string has at least three times *** altogether and then if the number before and after the *** sums 11. In the example given, these conditions are met because: First, the string has three *** altogether, and then 9 + 2 is 11.

I have solved this problem using regex, match, and replace, but how I can do it without applying to those solutions.

I have tried to do this without regex but it did not work because indexOf is only giving me one result:

string = "***6**5****8*9***2";

  for (var i = 0; i < string.length; i++) {
     let where = (string.indexOf("***"));
     let a = parseInt(string.charAt(where - 1)); 
     let b = parseInt(string.charAt(where + 3));
    if (a + b == 11){
        isTrue = true;             
    }
   }
Nina Scholz

You could split the string by stars amd keep the starts in the array then check if the value starts with a star, check the length and the left and right element for sum.

var string = "***6**5****8*9***2",
    parts = string.split(/(\*+)/),
    result = parts.some((s, i, { [i - 1]: l, [i + 1]: r }) =>
        s[0] === '*' && s.length >= 3 && +l + +r === 11
    );

console.log(result);
console.log(parts);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Run my script more than one time

Run more than one application in the same time

Is the following while loop can be run more than one time?

Is it possible for a JVM to run more than one program at the same time?

Run more than one lein task at the same time

Running where() more more than one time

Can I run more than one ansible instance at the same time on the same machine?

I can't get this jQuery animation that changes width to run more than one time

Function doesn't work anymore if run by more than one time in Python 3.7

Is it possible for more than one task in Task Scheduler to run a console application (.exe file) at the same time?

Error in Evaluate when selecting more than one cell: Run-time error '13': Type Mismatch

How can I get a JavaScript CSS animation to run more than one time?

Run more than one server using python

Run more than one task with default shortcut

How to run a winform project for more than one?

createUserJob Run More Than One Command

Counter not increasing more than one time

execute function not more than one time

UIimageview showing more than one time in uitableviewcell

update of more than one consenter at a time is not supported

Using paramiko more than one time in a script

mouse wheel firing more than one time

BottomSheet Dialog opening more than one time

Printing more than one graph at a time

Div not appending more than one time in jquery?

Innosetup: sspostinstall executed more than one time?

Changing id of an element more than one time

Using more than one time input in the chart

how to run more than one programe by the RUN (windows)