0

I know that this question was asked about in PHP but I could not find anything in javascript.

I have a random number generated between 000(yeah it is just three zero's but it is shown that way from how the number is generated) and 999999999 and I want to test true or false for if whether it includes a specific sequence of numbers like 777, 9000, 28, or the like of any length and from any beginning of numbers...

for example, finding 289 in 678342891 or 728987699 would be true, and finding 289 in 678529187 or 023829564 would be false.

is this possible, and how would I do it?

2 Answers2

3

you can use .includes method in JS after transforming both the number and the other number to strings using .toSting method

let n = 12345589;
let sub = 55;
let sub2 = 25;


function isSeq(number, sub){
  number = number.toString(10);
  sub = sub.toString(10);
  return number.includes(sub);

}

console.log(isSeq(n, sub));
console.log(isSeq(n, sub2));
Ahmed Gaafer
  • 1,603
  • 9
  • 26
0

Check whether your test number is a substring in your long random number. Since you have mentioned that 000 is a value, this tells me that it is a string already(no need to call toString() for it)

var random=678342891;
var search1=289;
var search2=777;

console.log( random.toString().indexOf(search1.toString()) != -1 );//true

console.log( random.toString().indexOf(search2.toString()) != -1 );//false

A function to test it would look like so:

function test(random,search){
    return random.toString().indexOf(search.toString()) != -1
}
Ziarek
  • 679
  • 6
  • 14