在特定情况下编写JavaScript的for循环。

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

writing a for loop in js for specific situation

问题

I have translated the provided content into Chinese as requested:

我想编写一个for循环条件,其中:

  1. ”的计数始终是递增的,不应跳过任何数字,且“”的数量可以是n。
  2. ”的计数可以重复,例如“”,“”,“”,“***”,只要它们是递增的。
  3. ”的计数也可以是递减的,不应跳过任何数字,例如“”,“”,“”,“”,“”,“**”,“”,“**”。

我无法编写第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:

  1. The count of &quot;*&quot; is always ascending and shouldn't skip a number and &quot;*&quot; can be of n count.
  2. &quot;*&quot; count can repeat like &quot;*&quot;,&quot;**&quot;,&quot;**&quot;,&quot;***&quot; as long as they are in ascending.
  3. &quot;*&quot; count can be in descending too and shouldn't skip a number like &quot;*&quot;,&quot;**&quot;,&quot;**&quot;,&quot;***&quot;,&quot;****&quot;,&quot;**&quot;,&quot;*&quot;,&quot;**&quot;

I am not able to write the 3rd statement could anyone help me with this problem statement.

Check the following code:

let tocStars = [&#39;*&#39;, &#39;**&#39;, &#39;***&#39;, &#39;*****&#39;]; 
let valid = true; 
let count = 1; 
for (let i = 0; i &lt; tocStars.length; i++) {
 if (tocStars[i].length &gt;= count) {
 if (tocStars[i].length !== count) {
 valid = false; break; } count++; } }
 if (valid) { console.log(&quot;Valid pattern&quot;); } else { console.log(&quot;Invalid pattern&quot;); }

答案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 &lt; 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 + &quot; is a valid pattern&quot;);
  } else {
    console.log(array + &quot; is NOT a valid pattern&quot;);
  }
}

isValid([&quot;*&quot;, &quot;**&quot;, &quot;***&quot;, &quot;*****&quot;]); // is NOT a valid pattern (skips one ascending)
isValid([&quot;*&quot;, &quot;**&quot;, &quot;**&quot;, &quot;***&quot;, &quot;****&quot;, &quot;**&quot;, &quot;*&quot;, &quot;**&quot;]); // is NOT a valid pattern (skips one descending)
isValid([&quot;*&quot;, &quot;&quot;, &quot;*&quot;]); // is NOT a valid pattern (blank character)
isValid([&quot;*&quot;, &quot;**&quot;, &quot;***&quot;, &quot;****&quot;]); // is a valid pattern
isValid([&quot;*&quot;, &quot;**&quot;, &quot;**&quot;, &quot;***&quot;, &quot;****&quot;, &quot;***&quot;, &quot;**&quot;, &quot;*&quot;, &quot;**&quot;, &quot;***&quot;]); // is a valid pattern

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月19日 14:45:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76051460.html
匿名

发表评论

匿名网友

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

确定