A newbies journey. Words to Numbers, a javascript interview question
Tags: javascript, interview, career
Published: 2021-07-04 22:58:07

I'm a career change newbie hoping to land my first job. I have had countless interviews and they have all varied, from being sent a faceless link to a tech test(ergh) to having to attend an in-person interview (during a pandemic!). I have had a few stand out interviews, both good and bad reasons. One interviewer said i seemed very stuck on 'continuing to learn' when I asked about what learning and development was in place, needless to say I didn't want that job. 

I am so eager and excited to get my first software dev role but I won't settle for the sake of a job - I'm interviewing them as much as they are interviewing me and I think you will ultimately be a better employee if you are in the right company. 

And so I get to the point of my post, the second ( of at least 50) interviews where I actually got to speak to one of the software devs in the company and so far my favourite interview. I have been extremely nervous (hello imposter syndrome) and this was no exception but the interviewer was understanding and made me feel very comfortable . 
We discussed my Github, projects i'd done at bootcamp and my motivations for career change and then came the kata.

Convert a number to the english word
- input will be whole numbers 
- between 1 - 999.

I have always tried to keep track of katas, handy bits of JS and interview questions and this has been so far my favourite. I think because I want this job so much ( the company is amazing !)

My variable names need tidying up but I'm so pleased with this solution although i'm sure it could be refactored further and tided up.

Here is what I came up with, I didn't finish it during the interview but I enjoyed it so much I wanted to finish. Any advice, tips or even just a discussion is very welcomed. 


```javascript 
function numberToWords(num) {
  //creating a dictionary of numbers to words
  const lessThan20 = {
    1: "one",
    2: "two",
    3: "three",
    4: "four",
    5: "five",
    6: "six",
    7: "seven",
    8: "eight",
    9: "nine",
    10: "ten",
    11: "eleven",
    12: "twelve",
    13: "thirteen",
    14: "fourteen",
    15: "fifteen",
    16: "sixteen",
    17: "seventeen",
    18: "eighteen",
    19: "nineteen",
  };

  const doubleToconvert = {
    2: "twenty",
    3: "thirty",
    4: "forty",
    5: "fifty",
    6: "sixty",
    7: "seventy",
    8: "eighty",
    9: "ninety",
  };

  function separateTheNums(num) {
    let split = [...(num + "")].map((n) => +n);
    return split;
  }

  //I made this a global function so it could be re-used
  function doubleDigit(num) {
    //first num
    const singleNum = Math.floor(num / 10);
    //second num
    const doubleNum = num % 10;
    const doubleDigits = doubleToconvert[singleNum] + lessThan20[doubleNum];
    return doubleDigits;
  }

  //i refactored this so it would work for 1000's
  function hundreds(num) {
    let seperateNum = separateTheNums(num);
    const first = lessThan20[seperateNum[0]] + " hundred and ";
    const doubleOfHun = [seperateNum[1]] + [seperateNum[2]];
    //added this ternary becuase it wasnt working with the 'teens'
    let checkForTeens =
      doubleOfHun < 20 ? lessThan20[doubleOfHun] : doubleDigit(doubleOfHun);
    hunConverted = first + checkForTeens;
    return hunConverted;
  }
  function thousands(num) {
    let seperateNum = separateTheNums(num);
    const first = lessThan20[seperateNum[0]] + " thousand, ";
    //removed the 'hundreds' but it is an array- to use the hundreds function i need to pass it a number
    let thouRemoved = seperateNum.slice(1);
    let x = thouRemoved.join("");
    let convertedhun = hundreds(x);
    return first + convertedhun;
  }

  if (num <= 19) {
    console.log(lessThan20[num]);
  } else if (num > 20 && num <= 99) {
    console.log(doubleDigit(num));
  } else if (num > 99 && num <= 999) {
    console.log(hundreds(num));
  } else {
    console.log(thousands(num));
  }
}

numberToWords(17);
numberToWords(415);
numberToWords(9);
numberToWords(4417);
numberToWords(9999);
```