【问题标题】:Showing string with charAt in a do-while loop在 do-while 循环中显示带有 charAt 的字符串
【发布时间】:2016-12-13 08:38:38
【问题描述】:
index = 0;
string1 = '1,2,3,4,5,6,8,9,0#';
string2 = '1,2,3#';
do{
    document.write(string1.charAt(index));
    index++;
}
while(string1.charAt(index) != '#');
index = 0;
document.write('<br />');
do{
    document.write(string2.charAt(index));
    index++;
}
while(string2.charAt(index) != '#');

你好,我被困在一个 Javascript 任务中,我需要在一个 do-while 循环中显示上面的字符串,我必须使用 charAt 来这样做。我需要循环在# 符号处停止。但是charAt方法只显示一个数字,

所以我的问题是:我怎样才能用 charAt 显示所有数字?当 # 符号等于 # 符号时,我如何停止我的 do-while 循环

【问题讨论】:

  • 你能告诉我们你到目前为止做了什么吗?一个jsfiddle也会很好。我们不会为你工作。如果你不尝试,你将一无所获。
  • 我的读心能力说你忘了增加循环内的index。或者您实际上并没有使用index 作为charAt 的参数

标签: javascript loops do-while charat


【解决方案1】:

const string = '1,2,3,4,5,6,7,8,9,0#';
let index = 0;

do {
  const char = string.charAt(index);
  if (char !== ',') console.log(char);
  index++;
} while (string.charAt(index) !== '#');

【讨论】:

    【解决方案2】:

    这就是你想要的。

    index = 0
    string1 = '1,2,3,4,5,6,7,8,9,0#'
    
    do {
      if (string1.charAt(index) !== ',') 
        console.log(string1.charAt(index))
      index++
    } while (string1.charAt(index) !== '#')

    注意:我们确实会帮助您解决问题!但请不要盲目依赖 StackOverflow。这对你的学习没有帮助。尝试一些东西并附上问题,以便我们轻松解决。

    【讨论】:

      猜你喜欢
      • 2021-01-31
      • 2015-05-21
      • 1970-01-01
      • 2019-04-26
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      • 2017-03-29
      • 2020-12-30
      相关资源
      最近更新 更多