英文:
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("");
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("");
}
Error message I am getting:
AssertionError: expected 'tHiS iS a TeSt' to equal 'tHiS Is a tEsT'
+ 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("");
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'));
<!-- 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("");
return arr.map(
(character, i) => (i % 2 === 0)
? character.toLowerCase()
: character.toUpperCase()
).join('');
}
console.assert(alternatingLetters('this is a test') === 'tHiS Is a tEsT');
<!-- 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("");
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("");
}
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 -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论