简体   繁体   English

检查字符串中的字母是否为大写

[英]Checking if a letter in a String is Upper Case

I am trying to complete the Caesar Cipher in JavaScript.我正在尝试完成 JavaScript 中的凯撒密码。 I need a peice of code that checks to see if a letter in a string is upper case or lower case.我需要一段代码来检查字符串中的字母是大写还是小写。

 const caesar = function(str) { for (i = 0; i < str.length; i++) { if (str[i] === str[i].toUpperCase) { console.log('YES'); } else { console.log('NO'); } } } caesar("HeLLO")

Where am I going wrong?我哪里错了?

UPDATE: Additional Question更新:附加问题

Here is my full code - I swapped out my check for str[i] = str[i].toUpperCase() TO 64 < asciiNum < 91 but the check doesn't work.这是我的完整代码 - 我将我的检查换成了str[i] = str[i].toUpperCase() TO 64 < asciiNum < 91但检查不起作用。

Any idea why using ASCII numbers to check for capital is not working in this instance?知道为什么在这种情况下使用 ASCII 数字检查大小写不起作用吗?

const caesar = function(str, shift) {

  var solved = ""; 

  for (i=0; i < str.length; i++){

   var asciiNum = str[i].charCodeAt();
   console.log(asciiNum);

   if (64 < asciiNum < 91) {      // check for upper cased
   newNum = asciiNum + shift;

        if(newNum > 90){
            solved += String.fromCharCode(newNum - 26);
               } else {
            solved += String.fromCharCode(newNum);
         }

 } else if (96 < asciiNum < 123) {         //check for lower case

   newNum = asciiNum + shift;

        if(newNum > 122){
            solved += String.fromCharCode(newNum - 26);
               } else {
            solved += String.fromCharCode(newNum);   
         }    
      } 

   }

  console.log(solved);

}

caesar("Hello, World!", 5);

Your issue was you weren't calling toUpperCase() .你的问题是你没有打电话toUpperCase() The computer saw it but didn't know it was supposed to run the function.计算机看到了它,但不知道它应该运行 function。 To fix it, just put parentheses.要修复它,只需加上括号。

However, to make it cleaner, I'd restructure this like so:但是,为了使其更清洁,我会像这样重组它:

 // Gets a string from the user to test // Replace this with whatever string should be tested (probably a variable) var str = prompt("What string should be tested?"); const caesar = function(arg) { return arg.split("").every(function(e) { /* Makes an array of characters and tests to see if every character satisfies the conditions */ if (e === e.toUpperCase()) { // If the character is upper case return true; // Return true; the program should test the next character } else { console.log(`${e} was not uppercase`); return false; // Return false // The program should stop, because it detected a lowercase character } }); } console.log(`The result of running caesar() on string '${str}' was ${caesar(str)}`); /* Instead of using console.log inside the function Return the *result* of the function so that you can use it later */

This method is faster to run and a little cleaner.这种方法运行起来更快,也更干净一些。

If you want to check if every letter is uppercase, leave it as it is.如果要检查每个字母是否都是大写,请保持原样。 If you want to check if some letters are uppercase, change the every to some .如果要检查某些字母是否为大写,请将every更改为some If you want to reverse and check if all characters (or some) are lowercase, replace the e === e.toUpperCase() with e === e.toLowerCase() or e.== e.toUpperCase() .如果要反转并检查所有字符(或某些字符)是否为小写,请将e === e.toUpperCase()替换为 e === e === e.toLowerCase()e.== e.toUpperCase() If a letter is not uppercase, it puts which one it was in the console and stops checking others.如果一个字母不是大写的,它将把它放在控制台中,并停止检查其他字母。 After it's finished, it puts whether all were uppercase in the string.完成后,它会在字符串中输入是否全部大写。

Only missing part in your code is a paranthesis () in toUpperCase .您的代码中唯一缺少的部分是toUpperCase中的括号()

You're missing parentheses after toUpperCase .您在toUpperCase之后缺少括号。

if (str[i] === str[i].toUpperCase) {

For JS you're trying to access a toUpperCase property instead call a function.对于 JS,您尝试访问toUpperCase属性,而不是调用 function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM