试图重构代码,但不确定哪里出错了?

huangapple go评论62阅读模式
英文:

Was trying to refactor code and not sure where I am going wrong?

问题

我最初写的代码是为了将字符串转换为小写并且变为“whisper”:

function whisper(str) {
return str.toLowerCase();
}


我的最近的函数在大写和小写之间反复切换,但是单词“test”中的t保持小写。我感到困惑:

function alternatingLetters(str) {
const arr = str.split("");
let isUpper = false;

for (let i = 0; i < arr.length; i++) {
if (arr[i] !== " ") {
arr[i] = isUpper ? arr[i].toUpperCase() : arr[i].toLowerCase();
isUpper = !isUpper;
}
}

return arr.join("");
}


我收到的错误消息是:

AssertionError: 期望 'tHiS iS a TeSt' 等于 'tHiS Is a tEsT'
+ 期望 - 实际

  -tHiS iS a TeSt
  +tHiS Is a tEsT

有任何建议吗?我还尝试过创建一个版本,不使用循环,类似于最初的解决方案,但不确定是否可行!
英文:

Original code i wrote for first pset which was to lower case and "whipser":

function whisper(str) {
  return str.toLowerCase();
}

my most recent function which is bouncing between lower and uppercase but the t in test stays lowercase. Getting confused:

function alternatingLetters(str) {
  const arr = str.split(&quot;&quot;);
  let isUpper = false;

  for (let i = 0; i &lt; arr.length; i++) {
    if (arr[i] !== &quot; &quot;) {
      arr[i] = isUpper ? arr[i].toUpperCase() : arr[i].toLowerCase();
      isUpper = !isUpper;
    }
  }

  return arr.join(&quot;&quot;);
}

Error message I am getting:

AssertionError: expected &#39;tHiS iS a TeSt&#39; to equal &#39;tHiS Is a tEsT&#39;
      + expected - actual

      -tHiS iS a TeSt
      +tHiS Is a tEsT

Any tips would be great! I also tried to make a version without a loop and just return like original solution but not sure if that can be done!

答案1

得分: 1

一种解决方案是将 isUpper 标志移到 if 条件外。

function alternatingLetters(str) {
  const arr = str.split("");
  let isUpper = false;

  for (let i = 0; i < arr.length; i++) {
    if (arr[i] !== " ") {
      arr[i] = isUpper ? arr[i].toUpperCase() : arr[i].toLowerCase();
    }
    isUpper = !isUpper;
  }

  return arr.join("");
}

console.log(alternatingLetters('this is a test'));
英文:

One solution is moving the isUpper flag outside the if condition.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function alternatingLetters(str) {
  const arr = str.split(&quot;&quot;);
  let isUpper = false;

  for (let i = 0; i &lt; arr.length; i++) {
    if (arr[i] !== &quot; &quot;) {
      arr[i] = isUpper ? arr[i].toUpperCase() : arr[i].toLowerCase();
    }
    isUpper = !isUpper;
  }

  return arr.join(&quot;&quot;);
}

console.log(alternatingLetters(&#39;this is a test&#39;));

<!-- end snippet -->

答案2

得分: 1

I also tried to make a version without a loop and just return like original solution but not sure if that can be done!

Not just with a standard string function (like toLowerCase()), but can be done with map:

function alternatingLetters(str) {
  const arr = str.split("");

  return arr.map(
    (character, i) => (i % 2 === 0)
        ? character.toLowerCase()
        : character.toUpperCase()
  ).join('');
}

console.assert(alternatingLetters('this is a test') === 'tHiS Is a tEsT');
英文:

> I also tried to make a version without a loop and just return like original solution but not sure if that can be done!

Not just with a standard string function (like toLowerCase()), but can be done with map:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function alternatingLetters(str) {
  const arr = str.split(&quot;&quot;);

  return arr.map(
    (character, i) =&gt; (i % 2 === 0)
        ? character.toLowerCase()
        : character.toUpperCase()
  ).join(&#39;&#39;);
}

console.assert(alternatingLetters(&#39;this is a test&#39;) === &#39;tHiS Is a tEsT&#39;);

<!-- end snippet -->

答案3

得分: 1

function alternatingLetters(str) {
  const arr = str.split("");
  let isUpper = false;
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] !== " ") {
      arr[i] = isUpper ? arr[i].toUpperCase() : arr[i].toLowerCase();
    }
    isUpper = !isUpper;
  }
  return arr.join("");
}
英文:
function alternatingLetters(str) {
  const arr = str.split(&quot;&quot;);
  let isUpper = false;
  for (let i = 0; i &lt; arr.length; i++) {
    if (arr[i] !== &quot; &quot;) {
      arr[i] = isUpper ? arr[i].toUpperCase() : arr[i].toLowerCase();
    }
    isUpper = !isUpper;
  }
  return arr.join(&quot;&quot;);
}

try to isUpper = !isUpper; outside of if block.

答案4

得分: 0

我明显误解了这个问题。以下代码片段中的函数翻转了字符串中每个字符的大小写:

function upsideDown(s){
 s=s.split("");
 for (let i=0;i<s.length; i++){
  let t=s[i].toLowerCase();
  s[i]=t==s[i]?s[i].toUpperCase():t;
 }
 return s.join("");
}

console.log(upsideDown("This Is a cRaZy TeSt!"))
英文:

I clearly misunderstood the question. The function in the following snippet flips the case of each character in the string:

<!-- begin snippet:js console:true -->
<!-- language:lang-js -->

function upsideDown(s){
s=s.split("");
for (let i=0;i<s.length; i++){
let t=s[i].toLowerCase();
s[i]=t==s[i]?s[i].toUpperCase():t;
}
return s.join("");
}

console.log(upsideDown("This Is a cRaZy TeSt!"))
<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月18日 00:01:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75486596.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定