-2

Trying to get last digit from a string in javascript.

Note that I am not looking to get the last character on a string, but the last digit as a number

For example:

lastDigit("MA01A"); // returns 1
lastDigit("MA01"); // returns 1
lastDigit("1"); // returns 1
lastDigit("1MABC"); // returns 1
lastDigit("MABC"); // returns undefined

How can I get it?

7
  • What research have you done into this string manipulation question? I ask this, because this is a very basic question that should have solutions already or resources available on any search engine Commented Nov 13, 2018 at 15:30
  • const source="MA01"; let char = source[source.length-1]; console.log(char); Commented Nov 13, 2018 at 15:30
  • Not duplicate..pls understnad Commented Nov 13, 2018 at 15:33
  • @Xatenev and the others, the last number inside a string, not any last character. Commented Nov 13, 2018 at 15:34
  • In your example the last number & last character are the same thing, is an input where there is a number followed by one or more letters possible? Commented Nov 13, 2018 at 15:35

4 Answers 4

2

Supposing you may have non-numerical characters at the end of the string, you can traverse the string in reverse and return the first number you encounter, or (e.g.) null otherwise

function getLatestDigit(text) {
  for (var i = text.length - 1; i >= 0; i--) {
    if (!isNaN(text.charAt(i))) return text.charAt(i);
  }
  return null;
}

console.log(getLatestDigit("MA01"));
console.log(getLatestDigit("MA12aaaaa"));
console.log(getLatestDigit("NoNumbersHere"));

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

1 Comment

This is the best method imo, the RegEx solutions are significant overkill for this trivial task.
1

Here's a version of this that doesn't use a for loop. Two versions: a short one, returning a string, and a slightly longer one returning a number, in case you need that too.

function lastDigit(str) {
  // Remove all non-digits from the string and return the last
  // character
  return str.replace(/[^\d]*/g, '').slice(-1) || undefined;
}

function lastDigitAsNumber(str) {
  // Remove all non-digits from the string
  const digit = str.replace(/[^\d]*/g, '').slice(-1)
  // Parse the last digit as number (this will be 0 for ''
  const number = Number(digit)
  // Return accordingly
  return digit.length > 0 ? number : undefined;
}

console.log(lastDigit("MA01A")); // returns '1'
console.log(lastDigit("MA01")); // returns '1'
console.log(lastDigit("1")); // returns '1'
console.log(lastDigit("1MABC")); // returns '1'
console.log(lastDigit("MABC")); // returns undefined

console.log(lastDigitAsNumber("MA01A")); // returns 1
console.log(lastDigitAsNumber("MA01")); // returns 1
console.log(lastDigitAsNumber("1")); // returns 1
console.log(lastDigitAsNumber("1MABC")); // returns 1
console.log(lastDigitAsNumber("MABC")); // returns undefined

Comments

0

Try this....

var source="MA01";

var lastNum = source.replace(/.*?(\d+)[^\d]*$/,'$1')

alert(lastNum);

1 Comment

Try it with "MA01aaaaaaaaaaa". Given the example you're not wrong (and the question is unclear about the input) but if we take the title of the question literally this doesn't really work.
0

var source = "MA01",
    lastNum = source.match(/(\d+)(?!.*\d)/)[0];

alert(lastNum);

3 Comments

Try adding a letter at the end of the string. It throws an error. See my comment to the other answer for why I'm pointing this out.
And this returns 1 but only because it parses 01, what if the input ended 12?
Now you're returning 01.

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.