How to get output in Javascript

Asked by Ashish Yadav about 6 months ago

0

Write a code to get the input in the given format and print the output in the given format.

Input Description: A single line contains a string.

Output Description: Print the characters in a string separated by line.

Sample Input : rahul Sample Output : r a h u l

1 Answer

    0

    Hey Ashish, if you want to each character to print in a separate line, you can use split method to convert the string into an array and interate over the array to print the characters.

    function printName(name) {
        let nameArray = name.split('');
        for(let i = 0; i < nameArray.length; i++) {
            console.log(nameArray[i]);
        }
    }
    
    printName('rahul')
    

    This code should give you an output

    r
    a
    h
    u
    l
    

    You can try this out in this playground

    @pranavtechie

    Pranav Mandava

    @pranavtechie

    Your answer