我想在语句中设置一些索引,并检查是否找到了此方法charAt,但它仅适用于字符串,因此如何使用数字处理它们
if (mobileNumber) {
let j = mobileNumber.charAt(2);
let w = mobileNumber.charAt(0);
alert("@", w, j)
if (j === 9) { //Not work quz it's a number
alert("FoundJ", j)
return;
} else if (w === 6) { //Not work quz it's a number
alert("FoundW", w)
return
} else{
alert("sorry, App does not work in your city right now")
return
}
现在我处理索引了,但是我对它们有一些疑问,这是我的声明 如果我写这个数字并且它包含9 || (2)索引中的6我在调用不知道为什么的函数时看到警报?
let j = mobileNumber.toString().charAt(2);
if (mobileNumber.length <= 0) {
this.setState(
Object.assign(validations, {
mobileNumberValid: "• Please, write your Mobile Number"
})
);
return;
} else if (mobileNumber.length < 10) {
this.setState(
Object.assign(validations, {
mobileNumberValid:
"• Your Mobile Number must be exactly 10 characters!"
})
);
return;
} else if (regNumber.test(mobileNumber) === false) {
this.setState(
Object.assign(validations, {
mobileNumberValid: "• Please, Your mobile must be a number"
})
);
return;
}
else if (j != 9 || j != 6) { // i think the issue with **OR**
alert("sorry not work in your city yet!")
return;
}
else {
this.setState(
Object.assign(validations, {
mobileNumberValid: ""
})
);
}
答案 0 :(得分:1)
首先使用toString()
将数字转换为字符串,然后使用charAt(index)
var mobileNumber = 98812345;
console.log(mobileNumber.toString().charAt(1));
答案 1 :(得分:0)
只需将它们转换为字符串即可:
if (mobileNumber) {
let j = mobileNumber.toString().charAt(2);
let w = mobileNumber.toString().charAt(0);
}
如果您需要将它们用作数字,只需将它们解析回去:
let jNumber = parseInt(j, 10);
答案 2 :(得分:0)
===执行精确比较,而没有转换类型(据我所知)
如果您有数字表达式,可以将其转换为字符串,然后使用charAt()进行比较
function findJW(mobileNumber) {
console.log("Checking "+ mobileNumber)
let str = mobileNumber.toString();
let j = str.charAt(2);
let w = str.charAt(0);
if (j == '9') {
console.log("FoundJ", j)
} else if (w == '6') {
console.log("FoundW", w)
} else{
console.log("sorry, App does not work in your city right now")
}
}
findJW(12954678)
findJW(62354678)
findJW(11111111)