-3
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) != '#');

Hello there, i am stuck at a Javascript assignment and i need to show the strings above in a do-while loop and i have to use charAt to do so. and i need the loop to stop at the # sign. But the charAt method only showed one number,

so my qeustion is: how can i show all the numbers with charAt? and how can i stop my do-while loop when the # sign is equal to the # sign

ICTMitchell
  • 91
  • 1
  • 12
  • 4
    Can you show us what have you tried so far? A jsfiddle would be nice too. We arent going to work for you. You will learn nothing if you dont try. – aloisdg Dec 13 '16 at 08:40
  • 1
    My mind reading powers say you forgot to increment the `index` inside your loop. Or you are not actually using `index` as a parameter for `charAt` – UnholySheep Dec 13 '16 at 08:42

2 Answers2

0

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) !== '#');
GG.
  • 21,083
  • 14
  • 84
  • 130
0

Here is what you want.

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) !== '#')

Note: We do help you to solve the query! But please don't depend blindly on StackOverflow. That will not help you in learning. Try something and attach it with question, so that we could sort out easily.

bharadhwaj
  • 2,059
  • 22
  • 35