[英]how to loop through two indexes in javascript
why it's giving me the first index and i need the ouput below which is "StOp mAkInG SpOnGeBoB MeMeS!"为什么它给了我第一个索引,我需要下面的输出,即“停止制造 SpOnGeBoB MeMeS!”
function spongeMeme(sentence) {
let x = sentence.split("");
for(let i = 0; i < sentence.length;i+=2){
return x[i].toUpperCase()
}
}
console.log(spongeMeme("stop Making spongebob Memes!")) // S
// output : StOp mAkInG SpOnGeBoB MeMeS!"
Try this:尝试这个:
function spongeMeme(sentence) {
let x = sentence.split("");
let newSentence = '';
for(let i = 0; i < sentence.length;i++){
// If the letter is even
if(i % 2 ==0) {
newSentence += x[i].toUpperCase();
}
// If the letter is odd
else {
newSentence += x[i].toLowerCase();
}
}
return newSentence
}
console.log(spongeMeme("stop Making spongebob Memes!"))
First of all you can only return once per function, so you have to create a variable and store all the new letter inside, and, at the end, return this variable.首先,每个函数只能返回一次,因此您必须创建一个变量并将所有新字母存储在其中,最后返回此变量。
You're returning just the first letter of what is stored inside of x.您只返回 x 中存储的内容的第一个字母。 Try to debug it and see what is going on.
尝试调试它,看看发生了什么。
The way I'd do it is like so:我这样做的方式是这样的:
function spongeMeme(sentence) {
return sentence.split("")
.map((s, i) => (i % 2 != 0 ? s.toUpperCase() : s))
.join("");
}
console.log(spongeMeme("stop Making spongebob Memes!"));
You're immediately returning on the first iteration of your for loop, hence the reason you're only getting back a single letter.您将立即返回 for 循环的第一次迭代,因此您只能返回一个字母。
Let's start by fixing your existing code:让我们从修复现有代码开始:
function spongeMeme(sentence) {
let x = sentence.split("");
for(let i = 0; i < sentence.length; i+=2) {
// You can't return here. Update the value instead.
return x[i].toUpperCase()
}
// Join your sentence back together before returning.
return x.join("");
}
console.log(spongeMeme("stop Making spongebob Memes!"))
That'll work, but we can clean it up.这行得通,但我们可以清理它。 How?
如何? By making it more declarative with map.
通过使用 map 使其更具声明性。
function spongeMeme(sentence) {
return sentence
.split('') // convert sentence to an array
.map((char, i) => { // update every other value
return (i % 2 === 0) ? char.toUpperCase() : char.toLowerCase();
})
.join(''); // convert back to a string
}
You can also do that:你也可以这样做:
const spongeMeme = (()=> { const UpperLower = [ (l)=>l.toUpperCase(), (l)=>l.toLowerCase() ] return (sentence) => { let i = -1, s = '' for (l of sentence) s += UpperLower[i=++i&1](l) return s } })() console.log(spongeMeme("stop Making spongebob Memes!"))
.as-console-wrapper {max-height: 100% !important;top: 0;} .as-console-row::after {display: none !important;}
OR或者
function spongeMemez(sentence)
{
let t = false, s = ''
for (l of sentence) s += (t=!t) ? l.toUpperCase() : l.toLowerCase()
return s
}
but I think this version is slower, because using an index should be faster than a ternary expression但我觉得这个版本比较慢,因为使用索引应该比三元表达式快
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.