toggleCase:仅取回输入字符串的第一个字符

问题描述

我正在尝试编写一个给定字符串的程序,该程序将所有大写字符转换为小写,并将所有小写字符转换为大写。

对于我的assertEqual测试,我使用字符串“ Test” 预期结果是:tEST 但我回来了:t

这是来自assertEqual测试的错误消息:

Failed [应将大写字母更改为小写字母,并将小写字母更改为大写字母预期的tEST,但得到:t

我正在为循环中的当前元素使用别名,我尝试将其删除,但没有任何改变。

// inputs: a string 
// outputs: a new string with all the input string cases switched(ex: UPPER -> upper)
// create an empty string to add characters to
var cased = '';
// split the input string up into an array or characters
function splitUp(string) {
  var stringArray = string.split('');
// iterate over the array of characters
for(var i = 0; i < stringArray.length; i++) {
  // if stringArray[i] is equal to the upper-case version of itself,if(stringArray[i] === stringArray[i].toupperCase()) {
    // change it to lower case
    stringArray[i] = stringArray[i].toLowerCase();
      // add lower-cased character to cased string
      cased += stringArray[i];
  // otherwise,if character is equal to the lower-case version of itself
  } else if(stringArray[i] === stringArray[i].toLowerCase) {
    // change it to upper case
    stringArray[i] = stringArray[i].toupperCase();
      // add upper-cased character to cased string
      cased += stringArray[i];
    }
  }
return cased;
}

// ASSERTION FUNCTIONS
function assertEqual(actual,expected,testName) {
if(actual === expected){
  console.log('passed')
} else {
  console.log(`Failed [${testName} Expected ${expected} but got ${actual}`)
  }
}

// TEST CASES
var actualCaseTest = splitUp('Test');
var expectedCaseTestResult = 'tEST';
assertEqual(actualCaseTest,expectedCaseTestResult,'should change upper case to lower case and lower case to upper case');

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)