user4859721
user4859721

Reputation:

How would I test for a certain digit in a variable?

Lets say I had a variable called test and test = 123456789;. Then I have another variable called anotherTest and anotherTest = 1234;. How would I make a program that can test whether a variable has the digit 5 or not? Then, how could it sort the variables into two groups of which one group of variables has the digit "5" within it and the other without? Is there a easy way to do this?

Upvotes: 0

Views: 433

Answers (4)

Downgoat
Downgoat

Reputation: 14371

You can do this with RegExp, or .indexOf. Either works:


RegEx

Everyone hates RegExp for some reason, but I like it. You can use:

var test = 123456789,
    anotherTest = 1234;

/5/.test(test);
/5/.test(anotherTest);

var test = 123456789,
    anotherTest = 1234;

document.write( 'test (123456789): ' + /5/.test(test) + '<br/>' );
document.write( 'anotherTest (1234): ' + /5/.test(anotherTest) );


indexOf

This can be faster in some situations, but not always, it is also a bit more "complicated", at least in my opinion:

var test = 123456789,
    anotherTest = 1234;

(test+'').indexOf(5) > -1;
(anotherTest+'').indexOf(5) > -1;

var test = 123456789,
    anotherTest = 1234;

document.write( 'test (123456789): ' + ((test+'').indexOf(5) > -1) + '<br/>' );
document.write( 'anotherTest (1234): ' + ((anotherTest+'').indexOf(5) > -1) + '<br/>' );

Upvotes: 0

jfriend00
jfriend00

Reputation: 707606

To see if a number contains the digit "5", you can just convert the numbers to strings and then just use .indexOf("5") on each string.

var test = 123456789;
var anotherTest = 1234;

// reports whether the passed in number or string contains the
// character "5"
function containsDigit5(val) {
    // convert number to string
    // if already string, then it is left as is
    val = "" + val;
    return val.indexOf("5") >= 0;
}

containsDigit5(test);           // true
containsDigit5(anotherTest);    // false

The grouping part of your question is not entirely clear, but you can just call this function on each variable and add the numbers to one of two arrays.

var testNumbers = [123456789, 1234];
var has5 = [];
var doesNotHave5 = [];

// reports whether the passed in number or string contains the
// character "5"
function containsDigit5(val) {
    // convert number to string
    // if already string, then it is left as is
    val = "" + val;
    return val.indexOf("5") >= 0;
}

testNumbers.forEach(function(item) {
    if (containsDigit5(item)) {
        has5.push(testNumbers[i]);
    } else {
        doesNotHave5.push(testNumbers[i]);
    } 
});

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074666

How would I make a program that can test whether a variable has the digit 5 or not?

You can readily do that with strings and indexOf:

if (String(test).indexOf("5") !== -1) {
    // It has a 5 in it
}

Then, how could it sort the variables into two groups of which one group of variables has the digit "5" within it and the other without?

You can't sort the variables into groups, but you can certainly sort values into groups. For example, this loops through an array and adds values to either the with5 or without5 array depending on whether the value contains the digit 5:

var a = [
  1234,
  12345,
  123123,
  555555
];
var with5 = [];
var without5 = [];
a.forEach(function(value) {
  if (String(value).indexOf("5") === -1) {
    without5.push(value);
  } else {
    with5.push(value);
  }
});
snippet.log("with5: " + with5.join(", "));
snippet.log("without5: " + without5.join(", "));
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>


The above assumes base 10 (decimal) strings, but you can easily do the same with hexadecimal or octal or any other base you like by using Number#toString(base). E.g.:

var s = num.toString(16);

...will assign s the value of num as a hexadecimal (base 16) string.

Upvotes: 3

Stradosphere
Stradosphere

Reputation: 1285

Loop through each character of variable test, then compare using indexOf() to see if it exists in anotherTest. If so add to one array, otherwise add to array 2.

Upvotes: 0

Related Questions