3

Is it possible to have a string of numbers, let's say generated by this operation

var s = "1";
onEvent("start", "click", function() {
  for (var i = 2; i < 51; i++){ 
    s = s+", "+i;
      if(i==50){
        setText("text", s);
      }
  }
});

Thus making s equal the sting of numbers "1, 2, 3, etc." now lets say there's a different function that tries to check if s ( the string ) has a certain number inside of it,

if(n == "number in s" ){
     *function*
 }

Now how are we able to find a singular number inside a string and compare it to another variable? "number in s" is the number being compared to the variable, 'n'. Now 'n' can change values but the function would should run if "number in s" contains all options for 'n'

2

4 Answers 4

2

You can use String.prototype.includes(). This would be the simplest way of achieving this.

The includes() method determines whether one string may be found within another string, returning true or false as appropriate.

In your example you can use -

if(s.includes(n)) { ... }
Sign up to request clarification or add additional context in comments.

1 Comment

This worked for my purpose of making a Recamán Sequence, and I did not know how to use if(s.split(',').indexOf(n) != -1) {...} in my case. If anyone needs my code for a Recamán Sequence here it is var j = 0; var n = 0; var s = "0, "; onEvent("gen", "click", function(){ for (var i=0; i<50; i++){ j++; if((n-j)<0 || s.includes(n-j)){ n = n + j; }else{ n = n - j; } s = s + n + ", "; } setText("numbers", s); hideElement("gen"); });
1

This is a more accurate way of doing it.

if(s.split(',').indexOf(n) != -1) {...}

If you have a string like '1,2,13', then str.includes(3) will give true which is wrong.

So instead , first we will split it by ',' to get all the numbers in array and search whether a particular number exists in it or not by using the indexOf method.

6 Comments

+1 for thinking of cases where include may fail. I may add, that they can get around this is the string is has a deliminator and just do ,3, for their search in includes.
This will not work for the first and last number in the string.
Yeah, I realize that after I said that and I was thinking of a way without using regex. So I think the split is actually the most reliable.
Yes @matthew, as I said it will be a more accurate way of doing it.
@SagarChaudhary why it will not work for first and last number ? "3,1,12".split(',').indexOf("3") ---> // 0
|
0

You can also use [String.prototype.indexOf()] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes)

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

  myString.indexOf(number) === -1 ? //number not found : //number found ;  

Comments

0
function IsNumberInsideString(s) {
    for (var i=0; i<s.length; i++) {
        if (!isNaN(parseInt(s[i]))) {
            console.log("Number is inside string");
            return ;
        }
    }
    console.log("Number is not present inside the string");
}

1 Comment

A code-only answer is a low-quality answer. Add some explanation as to why you think this code-snippet would work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.