Day 2: Loops Solution
Hello Friends in this article i am gone to share Hackerrank 10 days of javascript solutions with you. | Day 2: Loops Solution
Also visit this link:ย Day 2: Conditional Statements: If-Else Solution
Day 2: Loops Solution
Objective
In this challenge, we practice looping over the characters of string.
Task
- First, print eachย vowelย inย sย on a new line. The English vowels areย a,ย e,ย i,ย o, andย u, and each vowel must be printed in the same order as it appeared inย s.
- Second, print eachย consonantย (i.e., non-vowel) inย sย on a new line in the same order as it appeared inย s.
Function Description
Complete theย vowelsAndConsonantsย function in the editor below.
vowelsAndConsonantsย has the following parameters:
- string s:ย the string to process
Prints
- Print each vowel ofย sย in order on a new line, then print each consonant in order on a new line. Return nothing.
Input Format
There is one line of input with the stringย s.
Output Format
First, print eachย vowelย inย sย on a new line (in the same order as they appeared inย s). Second, print eachย consonantย (i.e., non-vowel) inย sย on a new line (in the same order as they appeared inย s).
Sample Input 0
javascriptloops
Sample Output 0
a
a
i
o
o
j
v
s
c
r
p
t
l
p
s
Explanation 0
Observe the following:
- Each letter is printed on a new line.
- Then the vowels are printed in the same order as they appeared inย s.
- Then the consonants are printed in the same order as they appeared inย s.
Solution โ Day 2: Loops
'useย strict'; process.stdin.resume(); process.stdin.setEncoding('utf-8'); letย inputStringย =ย ''; letย currentLineย =ย 0; process.stdin.on('data',ย inputStdinย =>ย { ย ย ย ย inputStringย +=ย inputStdin; }); process.stdin.on('end',ย _ย =>ย { ย ย ย ย inputStringย =ย inputString.trim().split('\n').map(stringย =>ย { ย ย ย ย ย ย ย ย returnย string.trim(); ย ย ย ย }); ย ย ย ย ย ย ย ย main();ย ย ย ย }); functionย readLine()ย { ย ย ย ย returnย inputString[currentLine++]; } /* ย *ย Completeย theย vowelsAndConsonantsย function. ย *ย Printย yourย outputย usingย 'console.log()'. ย */ functionย vowelsAndConsonants(s)ย { ย ย ย ย varย vowelsย =ย ["a",ย "e",ย "i",ย "o",ย "u"]; ย ย ย ย forย (varย iย =ย 0;ย iย <ย s.length;ย i++){ ย ย ย ย ย ย ย ย ifย (vowels.indexOf(s[i])ย >ย -1){ ย ย ย ย ย ย ย ย ย ย ย ย console.log(s[i]); ย ย ย ย ย ย ย ย } ย ย ย ย }ย ย ย ย ย ย ย ย forย (varย jย =ย 0;ย jย <ย s.length;ย j++){ ย ย ย ย ย ย ย ย ifย (vowels.indexOf(s[j])ย <ย 0){ ย ย ย ย ย ย ย ย ย ย ย ย console.log(s[j]); ย ย ย ย ย ย ย ย } ย ย ย ย }ย ย ย ย } functionย main()ย { ย ย ย ย constย sย =ย readLine(); ย ย ย ย ย ย ย ย vowelsAndConsonants(s); }