10 Days Of JavaScript

Day 2: Conditional Statements: Switch Solution

Hello Friends in this article i am gone to share Hackerrank 10 days of javascript solutions with you. | Day 2: Conditional Statements: Switch Solution.


Also visit this link:ย  Day 1: Let and Const Solution


 

Day 2: Conditional Statements: Switch Solution

Objective

In this challenge, we learn aboutย switch statements.

Task

Complete theย getLetter(s)ย function in the editor. It has one parameter: a string,ย s, consisting of lowercase English alphabetic letters (i.e.,ย aย throughย z). It must returnย A,ย B,ย C, orย Dย depending on the following criteria:

  • If the first character in stringย sย is in the setย {a,ย e,ย i,ย o,ย u}, then returnย A.
  • If the first character in stringย sย is in the setย {b,ย c,ย d,ย f,ย g}, then returnย B.
  • If the first character in stringย sย is in the setย {h,ย j,ย k,ย l,ย m}, then returnย C.
  • If the first character in stringย sย is in the setย {n,ย p,ย q,ย r,ย s,ย t,ย v,ย w,ย x,ย y,ย z}, then returnย D.

Hint:ย You can get the letter at some indexย iย inย sย using the syntaxย s[i]ย orย s.charAt(i).

Function Description

Complete theย getLetterย function in the editor below.
getLetterย has the following parameters:

  • string s:ย a string

Returns

  • string:ย a single letter determined as described above

Input Format

Stub code in the editor reads a single string denotingย sย from stdin.

Constraints

  • 1 <= |s| <= 100, whereย |s|ย is the length ofย s.
  • Stringย sย contains lowercase English alphabetic letters (i.e.,ย aย throughย z) only.

Sample Input 0

adfgt

Sample Output 0

A

Explanation 0

The first character of stringย sย = adfgtย isย a. Because the given criteria stipulate that we printย Aย any time the first character is inย {a,ย e,ย i,ย o,ย u}, we returnย Aย as our answer.

 

Solution โ€“ Day 2: Conditional Statements: Switch


'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++];
}

functionย getLetter(s)ย {
ย ย ย ย letย letter;
ย ย ย ย //ย Writeย yourย codeย here
ย ย ย ย ย switchย (s[0])ย {
ย ย ย ย ย ย ย ย caseย ('a'ย ||ย 'e'ย ||ย 'o'ย ||ย 'i'ย ||ย 'u'):
ย ย ย ย ย ย ย ย ย ย ย ย letterย =ย 'A';
ย ย ย ย ย ย ย ย ย ย ย ย break;

ย ย ย ย ย ย ย ย caseย ('b'ย ||ย 'c'ย ||ย 'd'ย ||ย 'f'ย ||ย 'g'):
ย ย ย ย ย ย ย ย ย ย ย ย letterย =ย 'B';
ย ย ย ย ย ย ย ย ย ย ย ย break;

ย ย ย ย ย ย ย ย caseย ('h'ย ||ย 'j'ย ||ย 'k'ย ||ย 'l'ย ||ย 'm'):
ย ย ย ย ย ย ย ย ย ย ย ย letterย =ย 'C';
ย ย ย ย ย ย ย ย ย ย ย ย break;

ย ย ย ย ย ย ย ย caseย ('z'ย ||ย 'n'ย ||ย 'p'ย ||ย 'q'ย ||ย 'r'ย ||ย 's'ย ||ย 't'ย ||ย 'v'ย ||ย 'w'ย ||ย 'x'ย ||ย 'y'):
ย ย ย ย ย ย ย ย ย ย ย ย letterย =ย 'D';

ย ย ย ย }

ย ย ย ย 
ย ย ย ย returnย letter;
}


functionย main()ย {
ย ย ย ย constย sย =ย readLine();
ย ย ย ย 
ย ย ย ย console.log(getLetter(s));
}