英文:
writing a for loop in js for specific situation
问题
I have translated the provided content into Chinese as requested:
我想编写一个for循环条件,其中:
- “”的计数始终是递增的,不应跳过任何数字,且“”的数量可以是n。
- “”的计数可以重复,例如“”,“”,“”,“***”,只要它们是递增的。
- “”的计数也可以是递减的,不应跳过任何数字,例如“”,“”,“”,“”,“”,“**”,“”,“**”。
我无法编写第3个语句,有人能帮我解决这个问题吗?
请检查以下代码:
let tocStars = ['*', '**', '***', '*****'];
let valid = true;
let count = 1;
for (let i = 0; i < tocStars.length; i++) {
if (tocStars[i].length >= count) {
if (tocStars[i].length !== count) {
valid = false;
break;
}
count++;
}
}
if (valid) {
console.log("有效模式");
} else {
console.log("无效模式");
}
英文:
I want to write a for loop condition where:
- The count of
"*"
is always ascending and shouldn't skip a number and"*"
can be ofn
count. "*"
count can repeat like"*","**","**","***"
as long as they are in ascending."*"
count can be in descending too and shouldn't skip a number like"*","**","**","***","****","**","*","**"
I am not able to write the 3rd statement could anyone help me with this problem statement.
Check the following code:
let tocStars = ['*', '**', '***', '*****'];
let valid = true;
let count = 1;
for (let i = 0; i < tocStars.length; i++) {
if (tocStars[i].length >= count) {
if (tocStars[i].length !== count) {
valid = false; break; } count++; } }
if (valid) { console.log("Valid pattern"); } else { console.log("Invalid pattern"); }
答案1
得分: 0
Is that what you're trying to achieve?
函数isValid(array) {
let valid = true;
let counter = 1;
for (let i = 0; i < array.length; i++) {
// Remove the line below if not relevant to your case
// I assumed that when there is no character, the pattern is invalid?
if (counter === 0) {
valid = false;
break;
};
if (array[i].length === counter) {
continue;
}
if (array[i].length === counter + 1) {
counter++;
continue;
}
if (array[i].length === counter - 1) {
counter--;
continue;
}
valid = false;
break;
}
if (valid) {
console.log(array + " is a valid pattern");
} else {
console.log(array + " is NOT a valid pattern");
}
}
isValid(["", "", "", ""]); // is NOT a valid pattern (skips one ascending)
isValid(["*", "", "", "", "", "**", "", "**"]); // is NOT a valid pattern (skips one descending)
isValid(["", "", ""]); // is NOT a valid pattern (blank character)
isValid(["", "", "", ""]); // is a valid pattern
isValid(["", "", "", "", "", "", "", "*", "", "***"]); // is a valid pattern
英文:
Is that what you're trying to achieve ?
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function isValid(array) {
let valid = true;
let counter = 1;
for (let i = 0; i < array.length; i++) {
// Remove the line below if not relevant to your case
// I assumed that when there is no character, the pattern is invalid?
if (counter === 0) {
valid = false;
break;
};
if (array[i].length === counter) {
continue;
}
if (array[i].length === counter + 1) {
counter++;
continue;
}
if (array[i].length === counter - 1) {
counter--;
continue;
}
valid = false;
break;
}
if (valid) {
console.log(array + " is a valid pattern");
} else {
console.log(array + " is NOT a valid pattern");
}
}
isValid(["*", "**", "***", "*****"]); // is NOT a valid pattern (skips one ascending)
isValid(["*", "**", "**", "***", "****", "**", "*", "**"]); // is NOT a valid pattern (skips one descending)
isValid(["*", "", "*"]); // is NOT a valid pattern (blank character)
isValid(["*", "**", "***", "****"]); // is a valid pattern
isValid(["*", "**", "**", "***", "****", "***", "**", "*", "**", "***"]); // is a valid pattern
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论