MeXiCaN WaVe 
Tags: mexicanwave, codechallenge, codekata, solution
Published: 2022-01-29 09:21:18

After a week of declaring my job search, I was fortunate enough to be getting a couple of phone screenings and even a back-to-back double tech interview just last week. It was my first EVER tech interview(s) and I could have been better prepared. Plus, the nerves did not help whatsoever. It was two 1-hour long interviews with a half-hour to recollect myself. I will try to attempt to do similar code challenges to the ones I failed horribly in my tech interview in future posts (which I will eventually link here) to redeem myself from the turmoil of that day. I guess it's a right of passage!



![Homer going in fetal position depicting inner turmoil](https://media.giphy.com/media/l2Je3r4PwyjBTKTkI/giphy.gif)



One was converting USD to a denomination or something to that effect, and the second one was returning the frequency of sequential characters. The closest challenges that I could find to them are [this](https://leetcode.com/discuss/interview-question/483660/google-phone-currency-conversion) (the original one was much more complex) and [this](https://practice.geeksforgeeks.org/problems/easy-string2212/1/).



##Let's do the challenge!



Anyway, today I'm giving myself a little bit of a mental break by trying my hand at doing a fun, simpler code challenge taken from [Codewars](https://www.codewars.com/kata/58f5c63f1e26ecda7e000029/train/ruby). There's nothing like a code challenge that I can do to build up my self-esteem again.



![One guy tries to initiate a Mexican wave at a game, but nobody joins in](https://media.giphy.com/media/HS5h2pK4V7gRi/giphy.gif)



###Task
In this simple Kata your task is to create a function that turns a string into a Mexican Wave. You will be passed a string and you must return that string in an array where an uppercase letter is a person standing up.



###Rules
1. The input string will always be the lower case but may be empty.
2. If the character in the string is whitespace then pass over it
as if it was an empty seat



###Example



```
wave("hello") => ["Hello", "hEllo", "heLlo", "helLo", "hellO"]
```



I'm trying to be better at jotting down what my thought process is before trying to code.



```
# input = str
# use input size to determine how many iterations needed
# capitalize letter in first position, append that to new arr
# output is an arr of strs with each character capitalized by position
# algo: create a range from 0 to string.size
# call 'map' on that range with 'index' as a parameter
# use str element reference and concatenations to convert character at index position into uppercase
# next if str[index] == ''
# str[0...index] + str[index].upcase + str[index + 1..-1]
```



I wrote this code in Ruby because it felt like forever since I was so caught up with JavaScript, React, and TailwindCSS. First off, test out any possible methods that can be used in `irb`:



![Code snippet of irb](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3irfqc329pp6xd15lqsu.png)



Set `string = "hello"` in the console:



![Code snippet of irb](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h2e2q4w7k2z9wzjbtppw.png)



Now let's write it out. Don't forget to test it to make sure it's doing what it's supposed to:



![Code snippet of irb](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uhmcbwo7lsut5zu6wqz8.png)



![Code snippet of irb](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lr2bhto0y2u97g3j8yfu.png)



I ended up using `.each` instead of `.map`, since I'm not transforming anymore and used a `next if`, so it skips that particular number.



And ta-dah! It returned `true`. How did you do? What could have be done better?



```
def wave(str)
result = []

(0...str.size).each do |index|
next if str[index] == ' '
result << str[0...index] + str[index].upcase + str[index + 1..-1]
end
result
end

p wave("hello") => ["Hello", "hEllo", "heLlo", "helLo", "hellO"]
```