Day 2: Conditional Statements: Switch

Day 2: Conditional Statements: Switch

Objective

In this challenge, we learn about switch statements. Check out the attached tutorial for more details.

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).

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.

Output Format

Return either A, B, C, or D according to the criteria given above.

Sample Input 0

1
adfgt

##Sample Output 0

1
AA

Explanation

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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;
}

Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×