Saturday 20 April 2019

Day 2: Loops :- 10 Days Of Javascript HackerRank Solution

Problem:-


Objective
In this challenge, we practice looping over the characters of string. Check out the attached tutorial for more details.
Task
Complete the vowelsAndConsonants function in the editor below. It has one parameter, a string, , consisting of lowercase English alphabetic letters (i.e., a through z). The function must do the following:
  1. First, print each vowel in  on a new line. The English vowels are aeio, and u, and each vowel must be printed in the same order as it appeared in .
  2. Second, print each consonant (i.e., non-vowel) in  on a new line in the same order as it appeared in .
Input Format
Locked stub code in the editor reads string  from stdin and passes it to the function.
Output Format
First, print each vowel in  on a new line (in the same order as they appeared in ). Second, print each consonant (i.e., non-vowel) in  on a new line (in the same order as they appeared in ).
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 .
  • Then the consonants are printed in the same order as they appeared in .

Solution:-


'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) {
const vowels = 'aeiou';
var consonants = '';

for (var i = 0; i < s.length; i++) {
if (vowels.includes(s[i])) {
console.log(s[i]);
}
else {
consonants += s[i] + '\n';
}
}

console.log(consonants.trim());
}


function main() {
const s = readLine();
vowelsAndConsonants(s);
}

6 comments:

  1. /tmp/submission/20201220/05/30/hackerrank-e5a840eb33f534e69668e39e83881d2a/code/Solution.js:31
    let inputString;
    ^

    SyntaxError: Identifier 'inputString' has already been declared
    at new Script (vm.js:83:7)
    at checkScriptSyntax (internal/bootstrap/node.js:619:5)
    at startup (internal/bootstrap/node.js:280:11)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

    ReplyDelete
  2. this code also work as well

    function vowelsAndConsonants(s) {
    let vowels = 'aeiou';
    let consonents = [];
    for(var i=0; i<s.length; i++){
    if(vowels.includes(s[i])){
    console.log(s[i]);
    }else{
    consonents.push(s[i]);
    }
    }
    for(let j=0; j<consonents.length; j++){
    console.log(consonents[j]);

    }
    }

    ReplyDelete
  3. when i use return statement instead console.log it just return first letter only

    ReplyDelete
  4. great! please keep me updated

    ReplyDelete
  5. function vowelsAndConsonants(s) {

    const vowels = 'aeiou';
    var consonants = '';
    for (var i = 0; i < s.length; i++) {
    if (vowels.includes(s[i])) {
    console.log(s[i]);
    }
    else {
    consonants +=s[i];
    }
    }
    for (let j=0; j<consonants.length; j++){
    console.log(consonants[j])
    }
    }

    ReplyDelete
  6. optimal and simple solution
    function vowelsAndConsonants(s) {
    let vowels = 'aeiou';
    let consonents = [];
    let string=[...s]
    string.filter((item)=>{
    vowels.includes(item) ? console.log(item): consonents.push(item)
    })
    consonents.map((item)=>console.log(item))
    }

    ReplyDelete

Error While embed the video in Your website page

Error:- Refused to display '<URL>' in a frame because it set 'X-Frame-Options' to 'sameorigin Solution:- if ...