You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

实现字符串大小写互换功能及JavaScript代码问题修正

Fixing JavaScript String Case Swap Code

Let's walk through fixing this code to properly swap lowercase and uppercase letters in the string. First, let's break down the issues in the original code:

  • isLetter function returns wrong type: It should return a boolean (true/false) instead of the letter or -1—we just need to know if the character is a letter, not return the character itself.
  • Loop condition error: i <= message.length will cause an out-of-bounds error since string indices start at 0 and go up to message.length - 1. It should be i < message.length.
  • Incorrect argument to isLetter: You're passing the index i instead of the actual character message[i].
  • Invalid character check: message[i].toLowerCase() is a string (truthy), so the condition will always pass. We need to compare the original character to its lowercase/uppercase version to check its case.
  • Strings are immutable in JavaScript: You can't directly modify message[i]—we need to build a new string with the swapped characters.
  • Typos and incomplete code: toLowerCas... is missing an 'e', and the else-if block isn't finished.

Here's the corrected, working code:

let message = "heY, WHAt are you dOING?"; 
let count_changes = 0; 
let newMessage = ""; // We'll build the swapped string here

// Updated isLetter to return boolean
let isLetter = (letter) => { 
  return ('a' <= letter && letter <= 'z') || ('A' <= letter && letter <= 'Z');
} 

for(let i = 0; i < message.length; i++) { 
  const char = message[i];
  if (isLetter(char)) {
    if (char === char.toLowerCase()) {
      // Character is lowercase, convert to uppercase
      newMessage += char.toUpperCase();
      count_changes++;
    } else if (char === char.toUpperCase()) {
      // Character is uppercase, convert to lowercase
      newMessage += char.toLowerCase();
      count_changes++;
    }
  } else {
    // Not a letter, add it as-is
    newMessage += char;
  }
}

console.log(newMessage); // Output: "HEy, whAT ARE YOU Doing?"
console.log(`Number of case changes: ${count_changes}`);

Key Fixes Explained:

  1. isLetter function: Now returns true if the input is a letter, false otherwise—this makes the condition checks cleaner.
  2. New string newMessage: Since we can't modify the original string directly, we append each processed character to this new string.
  3. Proper case checks: We compare the original character to its lowercase/uppercase equivalent to determine its current case, then convert it accordingly.
  4. Fixed loop bounds: i < message.length ensures we only iterate over valid characters in the string.
  5. Handled non-letter characters: Any character that isn't a letter is added to newMessage without changes, preserving punctuation and spaces.

内容的提问来源于stack exchange,提问作者user6311492

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:07:57