Interview Qs Decoded - # 2
Tags: codenewbie, javascript, career, beginners
Published: 2020-06-11 23:40:29

I wanted to share with you all an actual challenge I was given during an interview!

The interviewer said, "Alright, let's do some **string reversals**!"

I responded with this exact face:
![A very excited girl](https://dev-to-uploads.s3.amazonaws.com/i/stz4ei0bsmg8eb0jyf69.gif)

---

##**The Q:** Write a function that accepts a string as a single argument, and when called, reverses the string.

**Restrictions:** 
- You *cannot* use **.reverse()** or **.reduce()**!

**Edge Cases:** 
- Punctuation does not matter
- Disregard numbers, we are only looking at strings

**Given Argument:**
```javascript
const string="Reverse me!";
```

**Setup:**
```javascript
function revFunction(str){
};
```
---

I asked the interviewer if I could use **.split()**, and he said, "sure, go ahead."

*Protip: Always ask questions!!*

```javascript
function revFunction(str){
   const splitString =  str.split('');
};
```

What **.split()** does is separate each character into individual indexes, making it into an array. I set it as a const variable **splitString** so I could call on it later.

Next, I thought of iterating through this new array, and so I utilized a **classic for-loop**

```javascript
function revFunction(str){
   const splitString =  str.split('');
   for(let i = 0; i <= str.length; i++){}
};
```

Now, what do I do when iterating through str? Yes, I need to go through them individually, but I need to iterate the other way. I asked if I could use **.pop()** since pop removes the last item in an array. He gave me the go-ahead.

```javascript
function revFunction(str){
   const splitString =  str.split('');
   for(let i = 0; i <= str.length; i++){
      // --------
      splitString.pop();
      // --------
   }
};
```

But we need that last letter! We can't just send it into the abyss! My solution to hold the popped item was to **.push()** it into a new, empty array.


```javascript
function revFunction(str){
   const splitString =  str.split('');
   // --------
   const reversedString = [];
   // --------
   for(let i = 0; i <= str.length; i++){
      // --------
       reversedString.push(splitString.pop());
      // --------
   }
};
```

At this point, if we console.log the revFunction, it will return as an array.


```javascript
// [!,e,m, ,e,s,r,e,v,e,R]
```
Success!

But, the interviewer wanted the function to return a solid string. I asked if I could use **.join()**. He approved.

```javascript
function revFunction(str){
   const splitString =  str.split('');
   const reversedString = [];
   for(let i = 0; i <= str.length; i++){
       reversedString.push(splitString.pop());
   }
   // ---- //
   return reversedString.join('');
   // ---- //
};
```

This should return:

```javascript
//!em esreveR
```
---

We did it! There are many ways to reverse a string without the **.reverse()** method. What I covered was the **iterative** way (*iterating* with a for-loop). 

##I challenge you to solve this challenge (in your language of choice) in the comments!

---

###[Try it out on my Repl!](https://repl.it/@catcarbonell/reverseString)

---

###Thanks for reading!!
Got a beginner-level interview question for me? DM me on [Twitter!](https://twitter.com/catcarbn) I'm always up for challenges!