2

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'

Soumen Mukherjee
  • 2,953
  • 3
  • 22
  • 34
xRegency
  • 45
  • 7
  • I think you are looking for [String.prototype.includes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) – Maheer Ali Sep 22 '19 at 04:50
  • possible duplicate of: https://stackoverflow.com/questions/5778020/check-whether-an-input-string-contains-a-number-in-javascript – Boney Sep 22 '19 at 05:50

4 Answers4

1

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)) { ... }
Nithin Kumar Biliya
  • 2,763
  • 3
  • 34
  • 54
  • 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"); });``` – xRegency Sep 27 '19 at 01:13
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.

Sagar Chaudhary
  • 1,343
  • 7
  • 21
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");
}
N3R4ZZuRR0
  • 2,400
  • 4
  • 18
  • 32
  • 1
    A code-only answer is a low-quality answer. Add some explanation as to why you think this code-snippet would work. – Taslim Oseni Sep 22 '19 at 11:11
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 ;  
sheygs
  • 11
  • 3