0

I have this code here, when I debug it, it prints out the characters on multiple lines but I want them to be on the same line in order to recompose the word inputted. For example, when I debug it inputting the word hello it prints out:

h
i
l
l
i

how ever I would like to print this: hilli. Any idea?? Thanks.

var say = prompt("name");

for ( var count = 0; count <= say.length; count++){

var letter = say.charAt(count);

  if (letter == "a" || letter == "e" || letter == "o" || letter == "u"){

  letter = "i";
  }

  console.log(letter);

}
2
  • 4
    Concat the values into a variable and use console.log() just once. Commented Jan 9, 2015 at 16:09
  • Even better. Use RegExp or regular string replacement to work out the occurrences and print the result. You don't need any loop here. Commented Jan 9, 2015 at 16:10

1 Answer 1

3

console.log always outputs each invocation on a line of its own.

If you want a single line, you'll have to coalesce the characters into a new string, and then output that after the loop.

For your particular code, you could also just use a regexp to do the whole thing.

name = name.replace(/[aeou]/gi, 'i');   // 'g' for global, 'i' for case-insensitive

or for case-sensitive:

name = name.replace(/[aeou]/g, 'i').replace(/[AEOU]/g, 'I');

There might be better ways of writing that last version, but the above is quick and self-explanatory.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much! I finally made it. However, could you please explain this ----name.replace(/[aeou]/g, 'i')---- to me.
It just means "replace every character (/g) in the set [aeou] with the letter i.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.