# Algorithm

What is “Algorithm”?

According to Merriam-Webster, “algorithm” are defined as

“a procedure for solving a mathematical problem ( as of finding the greatest common divisor) in a finite number of steps that frequently involves repetition of an operation.”

??? For me, it’s hard to understand…Merriam-Webster also mentioned

“broadly: a step-by-step procedure for solving a problem or accomplishing some end”

Now, I am getting understand this. After researching “Algorithm” on Internet, “Algorithm “in “my” interpretation means

“A procedure or calculation method for solving problem”.

It made me more clear what I learned these past two weeks. I really had a difficult time these past two weeks in my programming class. We’ve been studying “Algorithm” , with using javascript, but actually, I didn’t know what I was doing! I was having a very hard time following the class, because our instructor’s typing is super super fast!! I was desperate to catch up typing the code as fast as possible with my instructor, of course, is was absolutely impossible thought, so there were no time to understand what the code means!!

Anyway, I am still struggling to understand how it works javascript, but I feel like I understand a little about “Algorithm”.

“Algorithm” is the method for solving problem, and there are several solution to solve one problem. I really like this concept. There could be more than one answer. There are various way to solve the problem. I feel flexible about this.

I want to try to use some algorithm with some example to understand it.

I currently live near Joyce station with my host family. When I go to the school in Downtown, there are several way to get there.

- Walk to the Joyce Station. Take a Skytrain from Joyce station to Granville station . Walk from Granville station to the school.
- Call Uber ride.(Easiest way, but very expensive)
- Walk from the home to the school. (About 10km, and will be take 2 hours!)
- Walk to the bus stop at Kingsway. (But you have to walk a long steep slope) Take a bus from Kingsway to Granville street. Walk to the school.

The above methods are “Algorithms” for commuting to the school from my place.

Which method I should choose? I think №2 is the easiest way, but I don’t have work, so I would get broke soon! №3 is the most reasonable way, but I cannot spend 4 hours (roundtrip) to go to school every day! So the method of №1 is the reasonable, and efficient method for me.

Now, it occurred to some idea. If I were a PC or a program, which method is easy for me? I definitely choose the method №1. You can also apply it to programming, Javascript. I finally found out that we were studying the several methods of solving the problem with Javascript for these two weeks!

Here are some examples what I learned of Javascript. This is the easiest one for me:)

Reverse the text

For example, you want to make “apple “ ===> “elppa”

Method №1

function reverseStr1(str){

//turn str to an array of characters

const arr = str.split(‘’)

//use reverse method

arr.reverse()

return arr.join(‘’)

}

console.log(reverseStr1(‘apple’) )

Method 2

function reverseStr2(str){

let reversed = ‘’

for(let character of str){

reversed = character + reversed

}

//‘’=a

//a = p+ a

//pa = p+ pa

//ppa = l + ppa

//lppa = e+ lppa

//elppa

return reversed

}

console.log(reverseStr2(‘apple’))

Method 3

function reversedStr3(str){

return str.split(‘’).reduce((reversed, character) => {

return character + reversed

}, ‘’)

}

console.log(reversedStr3(‘apple’))

//Method 3 is quite similar with method 2, but much shorter code.

There are other ways to do reverse text, but I just picked the above three methods. You can choose one of the method(code), but also you have to consider which code makes PC less work load.

My conclusion is that Algorithm is the methods of solving the problem, and there are always more than one answer.

By the way, does anyone know how to memorize the javascript functions quickly ?There are so many, and I’ve been overwhelmed so many javascript functions!