Day 2: Conditional Statements: Switch (10 Days of Javascript)

Task:

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

  • If the first character in the 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


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



  function getLetter(s) {
    let letter;
    // Write your code here
    switch (true) {
        case 'aeiou'.includes(s[0]):
            letter = 'A';
            break;
        case 'bcdfg'.includes(s[0]):
            letter = 'B';
            break;
        case 'hjklm'.includes(s[0]):
            letter = 'C';
            break;
        default:
            letter = 'D';
            break;
    }
    return letter;
}




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

Leave a Reply