Read a name and greet the user, then say how many characters the name has.
What to build
A program that reads one line and prints two lines about it.
Requirements
- Read the name with
prompt(). - Print
Hello, NAME!on the first line. - Print
Your name has N characters.on the second line, where N is the length of the name.
Input
One line: a name.
Output
Two lines. Always write the word "characters", even when N is 1.
Constraints
The name has at least 1 character, and no spaces at either end.
Example
Input:
Mitu
Output:
Hello, Mitu!
Your name has 4 characters.
Why it works
prompt() gives the line as text. A template string places that text inside the greeting, and every string knows its own length, so you never count the letters yourself.
Examples
Example 1 · Input
Mitu
Expected output
Hello, Mitu! Your name has 4 characters.
Example 2 · Input
Rahim
Expected output
Hello, Rahim! Your name has 5 characters.
Hints
Hint 1
prompt() gives the line as a string.
Hint 2
A template string puts values inside text: `Hello, ${name}!`.
Hint 3
A string's length is name.length.
Solution
Show one correct solution (try it yourself first)
const name = prompt();
console.log(`Hello, ${name}!`);
console.log(`Your name has ${name.length} characters.`);