0

I have a string that starts with "TT" and ends with six digits(ex. "TT012345", "TT012000, TT329001). The string is always formatted like this and I need to check if the last digit in this string is of a certain value.

Say I have the string "TT032970". In this case I'd like to get a match on this string since the last digit is zero and the digit before that is a seven(I'm looking for 7). The string "TT037000" should also be a match but "TT0329701" shouldn't(since it isn't all zeroes to the right of the seven(the "last" 7 in the string)).

I was thinking of using a set of nested if's using substr() to check all places of the string for zeroes and if it isn't a zero in position n, then I check if the digit I'm looking for exists in position n.
My code is repetitive and I'm all for being efficient.

This is what I got so far(that works but only checks the last place of the string and the second last place):

var lastDigit = [3, 7, 8], tda = document.querySelectorAll('td a'), i, j;

function checkArray(num) {
    "use strict";

    for (j = 0; j < lastDigit.length; j++) {
        if (num === lastDigit[j]) {
            return true;
        }
    }
}

for (i = 0; i < tda.length; i++) {


    if ((parseInt(tda[i].textContent.substr(8, 1), 10) === 0 && checkArray(parseInt(tda[i].textContent.substr(7, 1), 10))) || checkArray(parseInt(tda[i].textContent.substr(8, 1), 10))) {
        tda[i].style.background = "rgb(255, 144, 255)";
        amountOfTickets.push(tda[i]);
    }
}

I'm positive there's a great way of checking the string for trailing zeroes and check the first non-zero digit before the zeroes. However, I'm really bad with loops and I just can't figure out how.

I'm very keen on figuring it out myself but I need a head start. I'd rather take a detailed explanation on how to do it than just the "answer".
If anything else seem off I'd gladly listen to improvements.

Thanks in advance!

0

3 Answers 3

3

To get the first digit before the zeros at the end of a string, you may use a regular expression :

+string.match(/(\d)0*$/)[1]

Example 1 :

var string = "TT032970";
var digit = +string.match(/(\d)0*$/)[1];
console.log(digit); // logs 7

Example 2 :

console.log(["TT012345","TT012000","TT329001","TT032970"].map(function(string){
   return +string.match(/(\d)0*$/)[1]
})); // logs [5, 2, 1, 7] 

Demonstration

Sign up to request clarification or add additional context in comments.

Comments

1

Obviously, from the other answers, a regular expression will be much simpler than your loops. Moreover, any nested loop solution will be difficult to work, as you don't know how many levels deep you have to look. (Is there one zero? Two? Five?)

This regex is quite simple:

/(\d)0+$/

If you do a match on that with your string, you should get either null if it doesn't match (e.g. "TT0329701") or a two-element array if it does (e.g. "TT037000" will return ["7000", "7"].)

That should be enough for your to build your own solution upon.

Best of luck.

Comments

0

The first thing I though about is something like this (depends on whether I understood your problem correctly):

function lookFor(str, digit) {
    //validate input...
    if (str.length != 8) return false;
    if (str[0] != "T" && str[1] != "T") return false;

    //start at the end and move to the left as long as there are zeros
    //the first non-zero element must be our digit, else return false
    for (var i = str.length-1; i>0; --i) {
        if (str[i] !== "0") {
            return str[i] === digit;
        }
    }
}

lookFor("TT012000", "2") --> true
lookFor("TT012000", "3") --> false

But I guess the regex solution is probably more compact than this one.

Comments

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.