challenge gauntlet #002
Published: 2020-09-26 19:46:33

#The problem
Given a string, create a function that turns all characters into the pound symbol('#') excluding the last four characters. Return the masked string. 

#Edge cases

So the problem is super vague but the test cases would clear some stuff up so let me put them here!
 
```
maskify("4556364607935616") == "############5616"
maskify(     "64607935616") ==      "#######5616"
maskify(               "1") ==                "1"
maskify(                "") ==                 ""
```

Alright, so what is this telling us? The numbers part we already knew. What we didn't know is what to do when the string length was less than four and now we see that we would just return the string.

This is good but what about letters? What if what you are being asked is a security question? Then you would only look for the last four letters in a string (of letters and spaces). 

#The Breakdown 

Things I want to cover before we get into any real thinking. A return statement, If we have a function it needs to return something and the last thing we want is to be caught forgetting a return statement. So that's on the list I am just going to re-read the problem to see if we need more than one return statement. 

----------------------------Reading-------------------------------


Reading the problem over again the answer would be no but looking at the examples in the edge case section would make it a yes. What would they be? Well, one for each case. Ok, so then what would the cases be then? It all hinges on how long the string is. If we can't get the last four characters or if there are less than four characters return the string. The other would be if there are more than four characters return the masked string.

```
function maskify(cc) {
   
  if(cc.length > 4){
     // do stuff 
   } else {
    return cc
  }
}
```
Ok, so we have what we got so far. A function that takes in a string in this case 'cc' and a conditional. But wait weren't we talking about return statements I only see one of them? Where is the other one? 

We need to talk about what going on in that conditional before we see a return statement. So at this point in the program, we know for sure that the string that we have been given is larger than four characters. We can break this down even further and say that we need a part that contains that last four digits. We can create a variable that contains a substring that contains those last 4 characters. That would look like this! :
`let lastFour = cc.substr(-4);`

Great we have those characters locked down. Now to move on to the rest of the string. We know that we need to convert those values into hash marks but how can we do that? First, we would need to get those values by themselves with another variable that points to another substring. We would need to start on at the beginning in the zero position and then go the length of the string minus the 4 we needed previously. It would look like this: 
```let everythingButTheLastFour = cc.substr(0, cc.length - 4);```

We are moving along nicely we got the last four and everything outside of it. Still need to make them hash marks but we could make a variable holding the number of hash marks we need. We could do this by calling the length function on everything but the last four and put it into a variable! It would look like this: 
```let everythingButTheLastFourNum = everythingButTheLastFour.length;```


We have got our number, the number of characters that in the string that excludes the last four. We just need to print out a hash mark for every one of them. Sounds like a great opportunity for a for-loop. It would look something like this:
```
for(let i = 0; i < everythingButTheLastFourNum; i++){
      '#';
    }
```
If you are thinking this is bad code and it doesn't work you would be correct. Remember the return statement that I was missing long, long ago from the pseudo-code. It's making its debut now. As we saw in the edge case section the function is looking for a return string. So we must make one. Outside of the conditional, we will create a variable that will hold an empty string. It will look something like this:
`let maskedCc = "";`

Now with our empty string, we can put our hash marks there:
```
for(let i = 0; i < everythingButTheLastFourNum; i++){
      maskedCc += '#';
    }
```
We would still have to add the last four characters so let's do that now: 
`maskedCc += lastFour;`

Last but not least the return statement with everything in it.  
`return maskedCc`

Here is what the code would look like all together!:
```
function maskify(cc) {
    let maskedCc = "";
  if(cc.length > 4){
    let lastFour = cc.substr(-4);
    let everythingButTheLastFour = cc.substr(0, cc.length - 4);
    
    let everythingButTheLastFourNum = everythingButTheLastFour.length;
    
    for(let i = 0; i < everythingButTheLastFourNum; i++){
      maskedCc += '#';
    }
    maskedCc += lastFour;
     return maskedCc
  } else {
    return cc
  }
}

```


#What to work on!

This week I feel like I did better with variable names, could use some work on the for-loop variable name though. I need to get more familiar with Data structures and algorithms. The better I get with them the easier the problems will be I believe. Another gap I need to fill for next week would be Big O, writing about it making assumptions, and testing more! I hoped this helped it is a lot of fun!

-thanks, Mike