[英]Javascript check if first character is number, upperCase, lowerCase or special character
The goal is a program that checks if the first character they type is a number, uppercase letter, lowercase letter or a special character/other.目标是一个程序,检查他们键入的第一个字符是数字、大写字母、小写字母还是特殊字符/其他。
Number works absolutely fine, but my task has to compare the uppercase and lowercase and check if they are letters.数字工作得很好,但我的任务必须比较大写和小写并检查它们是否是字母。 If not, it should then return special character.
如果不是,则它应该返回特殊字符。 I also have to make it without using functions.
我还必须在不使用函数的情况下实现它。
I am struggling to get the special character to display as it is treated as either uppercase or lowercase.我正在努力让特殊字符显示,因为它被视为大写或小写。 I have tried fiddling around with the equals signs and using regex but I am confusing myself.
我试过摆弄等号和使用正则表达式,但我自己很困惑。 onlyLetters as you can see it not being used as I have tried making it == to input.
onlyLetters 正如你所看到的,它没有被使用,因为我已经尝试让它 == 输入。 Doing this led to everything (except numbers) being shown as a special character and thus reversing the problem.
这样做会导致所有内容(数字除外)都显示为特殊字符,从而扭转了问题。 I have also tried seeing what works for others but they do not work on uppercase and lowercase letters also like I am.
我也试过看看什么对其他人有效,但他们也不像我一样对大写和小写字母有效。
Is there way to return an uppercase letter, lowercase letter or special character without the use of a function?有没有办法在不使用 function 的情况下返回大写字母、小写字母或特殊字符? Help to restore sanity is greatly appreciated
非常感谢帮助恢复理智
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') }
you have an error here你这里有错误
if (input === upperCase || lowerCase) {
should be应该
if (input === upperCase || input === lowerCase) {
But just use the regex you had but did not use但是只需使用您拥有但未使用的正则表达式
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') }
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')
}
It looks like you are trying to determine the type of the input character.看起来您正在尝试确定输入字符的类型。 Here is one way to do it without using any functions:
这是一种不使用任何功能的方法:
let input = prompt('Enter a number or uppercase or lowercase letter')
let onlyLetters = /^[a-zA-Z]/
if (Number.isInteger(Number(input))) {
console.log(input + ' is a number')
} else if (input >= 'A' && input <= 'Z') {
console.log(input + ' is an uppercase letter')
} else if (input >= 'a' && input <= 'z') {
console.log(input + ' is a lowercase letter')
} else {
console.log(input + ' is a special character')
}
This solution checks if the input is a number, uppercase letter, or lowercase letter using simple comparisons.该解决方案使用简单的比较来检查输入是数字、大写字母还是小写字母。 If none of those conditions are met, it is assumed that the input is a special character.
如果这些条件都不满足,则假定输入是特殊字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.