Javascript检查第一个字符是否为数字、大写、小写或特殊字符



程序的目标是检查用户键入的第一个字符是否为数字、大写字母、小写字母或特殊字符/其他。

Number工作得很好,但是我的任务必须比较大写和小写,并检查它们是否为字母。如果不是,它应该返回特殊字符。我还必须使它不使用函数。

我正在努力获得特殊字符显示,因为它被视为大写或小写。我试过摆弄等号和使用正则表达式,但我自己很困惑。onlyLetters,你可以看到它没有被使用,因为我已经尝试使它==输入。这样做会导致所有内容(除了数字)都显示为特殊字符,从而逆转问题。我也试着看看什么对别人有用,但他们也不像我一样用大写和小写字母。

在不使用函数的情况下,是否有方法返回大写字母、小写字母或特殊字符?帮助恢复理智是非常感激的

let input = prompt('Enter a number or uppercase or lowercase letter')
let onlyLetters = /^[a-zA-Z]/
const upperCase = input.toUpperCase()
const lowerCase = input.toLowerCase()
const numInput = Number(input)
if (Number.isInteger(numInput) == true) {
console.log(input + ' is a number')
} else if (input === upperCase || lowerCase) {
if (input === upperCase) {
console.log(input + ' is an uppercase letter')
} else if (input === lowerCase) {
console.log(input + ' is a lowercase letter')
}
} else {
console.log(input + ' is a special character')
}

这里有错误

if (input === upperCase || lowerCase) {

应该

if (input === upperCase || input === lowerCase) {

使用你的正则表达式,但不使用

let input = prompt('Enter a number or uppercase or lowercase letter')
const numInput = Number(input)
if (Number.isInteger(numInput)) {
console.log(input + ' is a number')
} else if (input.match(/[a-zA-Z]/)) {
const upperCase = input.toUpperCase()
const lowerCase = input.toLowerCase()
if (input === upperCase) {
console.log(input + ' is an uppercase letter')
} else if (input === lowerCase) {
console.log(input + ' is a lowercase letter')
}
} else {
console.log(input + ' is a special character')
}

我对ASCII值进行了大小写检查。这样,如果它不是一个ASCII值,它可以转到else语句并返回一个特殊字符。

let input = prompt('Enter a number or uppercase or lowercase letter')
const upperCase = input.toUpperCase()
const lowerCase = input.toLowerCase()
const numInput = Number(input)
charCode = input.charCodeAt(0)
if (Number.isInteger(numInput) == true) {
console.log( input + ' is a number')
}
// 65 to 90 for uppercase
// 97 to 122 for lowercase
else if (charCode >= 65 && charCode <=90) {
console.log(input + ' is an uppercase letter')
}
else if (charCode >= 97 && charCode <=122) {
console.log(input + ' is a lowercase letter')
}
else {
console.log(input + ' is a special character')
}

相关内容

最新更新