I was doing some basic linear search exercises (I’ve been coding for 2 months or so) with javascript and stumbled upon the following problem:
I get input from the user that goes to an array and the program tells in which position of the array the number they decided to retrieve is. I wanted to do something clean like “`I've retrieved the number 69 for you. It was on the 22nd position of the array of inputs you created.`

“

So I wanted to check the last digit of the number and respond accordingly with X1st, X2nd and X3rd or Xth. But I don’t know how to check the last digit of the desired number. Should I convert it into a string and then check with the .pop() function?

I only accounted for 30 inputs. But I would like for it to work not depending on a set number of inputs.

let inputsArray = []; let totalInputs; do { totalInputs = Number(prompt("How many number do you want to input? (max. 30)")); } while(totalInputs >= 31 || totalInputs <= 0) for(i = 0; i < totalInputs; i++) //Get the inputs from the user and add them into the inputsArray { let input = Number(prompt("Type a number")); inputsArray.push(input); } let desiredNum = Number(prompt(`What number do you want to retrieve? (Select from ${inputsArray})`)); let validator = 0; for(i = 0; i < inputsArray.length; i++) //Check on each index of the inputsArray for the number prompted { if(inputsArray[i] == desiredNum) { if (i + 1 == 1 || i + 1 == 21) //Tell the user which position in the array the prompted number was { alert(`I've retrieved the number ${desiredNum} for you. It was on the ${i+1}st position on the array of numbers you created.`); validator++; } else if (i + 1 == 2 || i + 1 == 22) { alert(`I've retrieved the number ${desiredNum} for you. It was on the ${i+1}nd position on the array of numbers you created.`); validator++; } else if (i + 1 == 3 || i + 1 == 23) { alert(`I've retrieved the number ${desiredNum} for you. It was on the ${i+1}rd position on the array of numbers you created.`); validator++; } else { alert(`I've retrieved the number ${desiredNum} for you. It was on the ${i+1}th position on the array of numbers you created.`); validator++; } } } if(validator != 1) //If the desiredNum is invalid { alert("The number you specified does not exist in the array."); }

## Answer

No need to convert to string. Let’s just address what we are looking for with the number. The case is either 0, 1, 2, or other; the +1 you use is not necessary for computing this.

You can use `%`

(modulo) here in order to check what the last digit is. Ensure that it avoids the edge case of 12nd with an extra condition.

This will allow for a single response to be issued instead of looking at multiple cases.

var desiredNum = 20; var suffixes = ["st","nd","rd"]; var suffix = num => (num < 4 || num > 13) && suffixes[num%10] ? suffixes[num%10] : "th"; for(let i = 0; i < 56; i+=11) console.log(`I've retrieved the number ${desiredNum} for you. It was on the ${(i+1)+suffix(i)} position on the array of numbers you created.`);