Rick Rieger
riegercodes

Follow

riegercodes

Follow

100 Days of Coding Day 1

Rick Rieger's photo
Rick Rieger
ยทFeb 2, 2022ยท

4 min read

100 Days of Coding Day 1

Hello, my name is Rick and I am in search for gig as a developer :)

This past December, I finished up a year long study with a full-stack school in NYC called Codeimmersives. We explored HTML, CSS, JavaScript, and how to build applications using the MERN stack. I learned about class-based components, passing props around (prop-drilling) and utilizing the component lifecycle(componentWillMount, componentDidMount, etc). After class based components, we moved on to functional based components using hooks, custom hooks, and different ways to manage state, such as the useContext and Redux APIs. (This is just a brief overview of the technologies I studied over the past year). Now that I am finished with Codeimmersives, I have decided to review the basics of programming/computer science, and continue to build projects. I am documenting my journey by sharing these blog posts. Please help me out by dropping a comment below on what you think!

This morning I started off with a few codewars challenges. I know to some, these may seem easy or very basic, but that's ok. :)

CODEWARS

  • Reverse words

Given a string "A dog chases the car", return "A god sesahc eht rac".

My solution:

function reverseWords(str) {
  return str.split(" ").map((word) => word.split("").reverse().join("")).join(" ");
}

.split() -takes a given string and splits it into an array. The parameters given define where to separate the string into a collection inside an array.

.map() -takes a function as an argument and returns the result. Here, I split each word in the array into their own arrays passing in an empty string (in order to make use of the reverse() method). Reverse is pretty much self explanatory, reverses the order of the array. Lastly, return the array into a string using the join() method for the individual words, then joining the complete sentence into a string as it was passed in the beginning.


  • Convert a Boolean to a String true === "true"

My solution:

function booleanToString(b){
  return b.toString()
}

This one was pretty cut and dry. Using the toString() method, return the boolean as a string.


  • Basic Mathematical Operations

Given an operation(as a string) and two values, return the result of the two values being added, subtracted, multiplied, or divided.

My solution:

function basicOp(operation, value1, value2)
{
  switch(operation){
      case '+':
      return value1 + value2 
      break;
      case '-':
      return value1 - value2 
      break;
      case '*':
      return value1 * value2 
      break;
      case '/':
      return value1 / value2 
      break;
      default:
      return 'Please provide a valid operation!'
  }
}

Another solution I found in the collection of results unlocked:

function basicOp(operation, value1, value2)
{
  return eval(value1 + operation + value2);
}

The solution I found...


  • Ones and Zeros

Given an array of ones and zeroes, convert the equivalent binary value to an integer.

Eg: [0, 0, 0, 1] is treated as 0001 which is the binary representation of 1.

First, I had to review what the heck binary numbers are, so I found a few examples on a google search(developer's best friend). I found this chart. In the examples given with the codewars challenge, The arrays were mostly 4 digits long, however they do say the length can be longer.

image.png de.com/res/hashnode/image/upload/v164372671..)

Here is my solution:

const binaryArrayToNumber = arr => {
  let x = 1
  let count = 0
  arr = arr.reverse()
  for(i=0;i<arr.length;i++){
    if(i > 0){
     x = x * 2
    }
    if(arr[i] === 1){
     count = count + x 
    }

  }
  return count
};

I had an interestingly difficult time with this one. I believed my solution was going to work at first go, but it didn't. I tried to change things and inserted many console.logs. You know that feeling you get when after realizing a small syntax error was throwing things off? Well, I sort of had that moment here. ๐Ÿง Arrays start from the left to right, not the other way around. LOL. I forgot that for a moment after reading binary numbers from right to left and it had me pretty confused. Oh well, lesson learned.

Another solution I found in the collection of results unlocked:

const binaryArrayToNumber = arr => parseInt(arr.join(''), 2);

Ok, so here is a one liner using the ES6 arrow function syntax for cleaner, more elegant code. and I'm still wrapping my head around this one. For the sake of time, I will provide some references here and move on to other things for the day. The parseInt() function parses a string argument and returns an integer of the specified radix (the base in mathematical numeral systems). Here is a video explaining more about what the radix base number number system is. Just remember you can join the numbers in the array, use the parseInt() functions and pass 2 into the second argument signifying that it is a binary number. See, I just learned something!

I was hopeful to get more accomplished today. An Army buddy,

Kollister, showed up to say hi and it pulled me away from my tasks for the day. I do miss my buddies in the band field.

Appreciate every moment you have to live.

A goal is not always meant to be reached, it often serves simply as something to aim at.โ€ โ€“ Bruce Lee

Much love,

Rick

ย 
Share this