0

I have a very simple code and function. My problem is: I want to select the last digit of a given number, which I did but when I want to use it, it simply won't work.

My job in this function is to make numbers more readable, so for example: if number is 1, output must be 1st; if number is 3, output must be 3rd; if number is 402 output must be 402nd.

function humanizeChar (number) {  
var lastOne = number.toString().split('').pop();
  if (lastOne === 1) {
    return number + 'st';
  } else if (lastOne === 2) {
    return number + 'nd';
  } else if (lastOne === 3) {
    return number + 'rd';
  } else {
    return number + 'th';
  } 
}
console.log(humanizeChar(1131)); // my output is 1131th
Duca
  • 73
  • 2
  • 10

6 Answers6

1

You are trying to compare a string (lastOne) to a number with strict equality ===, that compares the value and the type. You can use simple equality == or change the numbers to strings:

function humanizeChar (number) {  
  var lastOne = number.toString().split('').pop();
  console.log(lastOne);
  if (lastOne === '1') {
    return number + 'st';
  } else if (lastOne === '2') {
    return number + 'nd';
  } else if (lastOne === '3') {
    return number + 'rd';
  } else {
    return number + 'th';
  } 
}
console.log(humanizeChar(1131)); // my output is 1131th
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
1

Try this, seem you compare the output string with the number:

function humanizeChar (number) {  
var lastOne = number.toString().split('').pop();
  if (lastOne === "1") {
    return number + 'st';
  } else if (lastOne === "2") {
    return number + 'nd';
  } else if (lastOne === "3") {
    return number + 'rd';
  } else {
    return number + 'th';
  } 
}
console.log(humanizeChar(1131)); // my output is 1131th
Rai Vu
  • 1,595
  • 1
  • 20
  • 30
  • 1
    Your answer will be more useful to others if you explain what the problem is and how your solutions solves it. – Felix Kling Jan 10 '17 at 15:22
1

Your problem is, that you are comparing strings with numbers, using the === operator. As stated in this very good answer on SO:

The identity (===) operator behaves identically to the equality (==) operator except no type conversion is done, and the types must be the same to be considered equal.

So you could either convert your lastOne manually to a number (e.g. using parseInt(lastOne)) or use the == operator instead. Or you could compare lastOne to string literals ('1','2',...) instead.

Community
  • 1
  • 1
Finn Poppinga
  • 269
  • 4
  • 11
1

JavaScript Comparison Operators Operator Description == equal to === equal value and equal type

in your case the correct operator is '=='

ottis79
  • 120
  • 1
  • 2
  • 12
1

You need to make some changes in your code like below:-

function humanizeChar (number) {
number = 7882;          //just give some number for checking
var lastOne = number.toString().split('').pop();
//console.log(lastOne);
  if (lastOne == 1) {
    return number + 'st';
  } else if (lastOne == 2) {
    return number + 'nd';
  } else if (lastOne == 3) {
    return number + 'rd';
  } else {
    return number + 'th';
  } 
}
console.log(humanizeChar());
Vishal Thakur
  • 1,564
  • 16
  • 25
1

We also need to account the 2nd from last digit. For example, 101 should be one hundred and first instead of one hundred and oneth.

//Improving from Ori Drori's answer
function humanizeChar (number) {
    var numStrs = number.toString().split('');
    var lastOne = numStrs.pop();
    var isSeondFromLastGreaterThanZero = (numStrs.length > 0 && numStrs.pop() !== '0');
    var suffix = "th";

    if (!isSeondFromLastGreaterThanZero) {
        if (lastOne === '1') {
            suffix = 'st';
        } else if (lastOne === '2') {
            suffix = 'nd';
        } else if (lastOne === '3') {
            suffix = 'rd';
        }
    }

    return number.toString() + suffix;

}
console.log(humanizeChar(1131)); // my output is 1131th
console.log(humanizeChar(1101)); // 1101st
console.log(humanizeChar(2)); // 2nd
Tianzhen Lin
  • 2,404
  • 1
  • 19
  • 19