检查数字的最后一位
Check the last digit of a number
我在用 javascript 做一些基本的线性搜索练习(我已经编码了 2 个月左右)并且偶然发现了以下问题:
我从用户那里得到一个数组的输入,程序告诉我他们决定检索的数字在数组的哪个位置。我想做一些干净的事情,比如“I've retrieved the number 69 for you. It was on the 22nd position of the array of inputs you created.
”
所以我想检查数字的最后一位,并相应地用 X1st、X2nd 和 X3rd 或 Xth 进行响应。
但我不知道如何检查所需号码的最后一位。我是否应该将其转换为字符串,然后使用 .pop() 函数检查?
我只占了30个输入。但我希望它的工作不依赖于一定数量的输入。
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.");
}
是的,最简单的方法就是转换为字符串:
var str_number = number.toString(); //converts number to string
var last_char = str_number.slice(-1); //gets last character
var last_digit = +(last_char); //convert last character to number
如果你尝试像
这样的模运算符会怎么样
last_digit = number % 10;
无需转换为字符串。让我们用这个数字来解决我们正在寻找的东西。大小写为 0、1、2 或其他;你使用的 +1 不是计算这个所必需的。
您可以在此处使用 %
(取模)来检查最后一位数字是多少。确保它避免了带有额外条件的 12nd 的边缘情况。
这将允许发出单个响应,而不是查看多个案例。
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.`);
我在用 javascript 做一些基本的线性搜索练习(我已经编码了 2 个月左右)并且偶然发现了以下问题:
我从用户那里得到一个数组的输入,程序告诉我他们决定检索的数字在数组的哪个位置。我想做一些干净的事情,比如“I've retrieved the number 69 for you. It was on the 22nd position of the array of inputs you created.
”
所以我想检查数字的最后一位,并相应地用 X1st、X2nd 和 X3rd 或 Xth 进行响应。 但我不知道如何检查所需号码的最后一位。我是否应该将其转换为字符串,然后使用 .pop() 函数检查?
我只占了30个输入。但我希望它的工作不依赖于一定数量的输入。
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.");
}
是的,最简单的方法就是转换为字符串:
var str_number = number.toString(); //converts number to string
var last_char = str_number.slice(-1); //gets last character
var last_digit = +(last_char); //convert last character to number
如果你尝试像
这样的模运算符会怎么样last_digit = number % 10;
无需转换为字符串。让我们用这个数字来解决我们正在寻找的东西。大小写为 0、1、2 或其他;你使用的 +1 不是计算这个所必需的。
您可以在此处使用 %
(取模)来检查最后一位数字是多少。确保它避免了带有额外条件的 12nd 的边缘情况。
这将允许发出单个响应,而不是查看多个案例。
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.`);