CoDEVIANT #9 (3/31/19)

Adrian Rosales
4 min readApr 1, 2019

Problem #1: String ends with?

(https://www.codewars.com/kata/string-ends-with/train/javascript)

Complete the solution so that it returns true if the first argument(string) passed in ends with the 2nd argument (also a string).

Examples:

solution(‘abc’, ‘bc’) // returns truesolution(‘abc’, ‘d’) // returns false

How Adrian Solved It:

  • I took a look at the problem and began to meditate.
  • Then I lost focus and realized that I was more likely to succeed if I got some spiritual guidance.
  • I took some Peyote and wandered into the forest to meet my spirit animal to see if she could help me connect with some way to talk to computers.
  • Eventually, she sat me down, and told me to read the docs.

Truth be told I didn’t do any of that shit. Peyote is fun for normos, but your guy’s drug of choice is rat poison. Rat’s aren’t dumb, they eat pizza, hot dogs, know to dumpster dive…that’s like your typical undergrad student the way Sallie Mae is about blowing up peoples’ phones these days.

How Adrian Actually Solved It:

function solution(str, ending){   return str.slice(-ending.length) == ending ? true: false;}

That’s it. It’s actually a stupidly simple problem. But here goes. So str is always going to be longer than ending. That said, we want to know if ending is going to be the same as the end of str. To do this, we use a ternary operator phrase to return true or false.

True

If it turns out to be true that getting the length of the ending and slicing the negative number of that length from str with .slice( ) and seeing if it is the same as ending

In this case, we’ll land with true.

False

If the final part of str does not equal ending, then we just return false.

“Best Practices” Solution:

function solution(str, ending){ 
return str.endsWith(ending);
}

Okay…well apparently there is a string method called endsWith( ) and you can pass in what you want to evaluate the ending of str against…and that’s it. It’s a good thing I didn’t whip up some weird thing that used splitting into an array 😅😅😅 *Yeah, I’m TOTES competent*

Problem 2: Multiplication Tables

(https://www.codewars.com/kata/multiplication-tables/train/javascript)

I managed to solve this one. Let me preface this by saying that I am wwaaaaaaaayyyyy too inebriated to be able to figure this out. That I did and in such a short time is kind of hacks to me. I don’t know if there’s something in the water or what.

Instructions:

Create a function that accepts dimensions, of Rows x Columns, as parameters in order to create a multiplication table sized according to the given dimensions. **The return value of the function must be an array, and the numbers must be Fixnums, NOT strings.

Example:

multiplication_table(3,3)1 2 32 4 63 6 9→[[1,2,3],[2,4,6],[3,6,9]]

Each value on the table should be equal to the value of multiplying the number in its first row times the number in its first column.

How Adrian Solved it while intoxicated:

function multiplicationTable(row,col){let answer = [];let rowArray;for(let i = 1; i <= row ; i++ ){rowArray = [];for(let c = 1; c <= col ; c++){rowArray.push(c*i);}answer.push(rowArray);}return answer;}
  • So…we decide we’re going to have a variable called answer that will be an empty array.
  • Then we allow rowArray to be a bit of a blank slate variable that exists in the scope of our function.
  • Then we create a for loop to go from the start to the end of the number that row represents starting at 1 (and not zero like usual) — We say that we’ll keep going until i is less than or equal to the value that row represents.
  • We make rowArray equal an empty array;
  • We create another for loop to iterate over the path from 1 to whatever number the variable col represents
  • We say that we’ll keep going until c is less than or equal to the value that col represents.
  • If c is equal to 1 and if i is also equal to 1 then you want to push that value of c to rowArray, otherwise you will want to push into rowArray the result of c times i.
  • Within the scope of the first for-loop, the one with i you’ll want to push rowArray into answer this will happen a number of times 😉
  • When all is said and done we just return answer.

“Best Practices Answer”:

function multiplicationTable(row,col){out = []for (var i = 1; i <= row; i++) {temp = []// console.log(temp)for (var j = 1; j <= col; j++) {temp.push(i*j)} out.push(temp)} return out}

They’re about the same. they declared temp inside of a for=loop. I had my version, rowArray be instantiated outside of the for loops.

Cool ;)

--

--