DEV Community

averagealloy
averagealloy

Posted on

challenge gauntlet #001

Quick intro

Welcome to the challenge gauntlet! This is a new series that I am trying out. I need to get better at problem-solving with pressure. I am not the best at it, to begin with, but I need to be if I want to work.

I had looked at Leet code because I heard it was the gold standard when it came to interviewing questions. After promptly reading the first question I had decided to start with code wars, they seemed a bit more digestible. So let's get into it!

The problem

Create a method called hello. Hello takes a string called name. Take the name and return it in a string. The problem also states that we need to take the name and only have the first letter of said name be capitalized. Also, make sure if someone passes an empty string it returns "Hello, World!". If someone calls the function without a name or an undefined value then return "Hello, World!" as well!

The breakdown

For me, the part the took the most thinking was the name capitalization part. I need to make it standardized, all the letters need to be lower case. After running the tests I saw some of the names were all jacked up. (meaning some of the letters were capitalized and others were not)

So I need to create a variable, in this case, that will hold the name in it's lower case form:

let lCName = name.toLowerCase()

Once I have the variable that has all letters in the name standardized to lower case, I would just need the first letter capitalized. Off to new variable land we go.

What I want this variable to hold is the final name the name that would be displayed. So to do that we need to capitalize the first letter. We could take the character at position zero and use the to upper case function. It would look something like this:

lCName.charAt(0).toUpperCase()

This is good but it's not gonna be the total name, that would just bump the first letter up, we still need the rest of the letters that make up the name. This is where we could append the slice method to that first letter it would look something like this:

+ lCName.slice(1)

So the whole variable looks like this:

let capitalName = lCName.charAt(0).toUpperCase() + lCName.slice(1);

We still need to return the phrase with the name, this is what the return statement would look like:

return `Hello, ${capitalName}!`

At this point, I would say that part one would be complete now we just have to take care of the edge cases. (if nothing is present or an empty string is passed)

I am going to start with pseudo-code. So we would have to check if something is being passed in. If it's a name then preform the logic we had just gone over and if not then render the generic string in this case it would be 'Hello, World!'.

To check if the name is an empty string would be something like this: name === ''. We would also need to check if what is being passed into the function will be undefined so we could say: name === undefined.

Here is what the function would look like in total:

function hello(name) {
  if(name === ''|| name === undefined){
    return 'Hello, World!'
} else {
   let lCName = name.toLowerCase()

   let capitalName = lCName.charAt(0).toUpperCase() + lCName.slice(1);
  return `Hello, ${capitalName}!` 
  }
 }

What to work on

In this section, I am gonna see what are my pain points and what I can do better next time. One thing that jumps out at me is readability specifically in regards to variable names. They need to be better. Along with that, I need to start putting in time and space costs. The faster I fumble through it here in the blog the better I will be in the interview.

Thanks, Mike

Top comments (1)

Collapse
 
thedaveamour profile image
David Amour

You might like hackerrank.com