Hiatus

Well, I’m back! After being away for far too long I’m ready to jump back into the saddle. Things in my life went sideways over the summer and I’ve been trying to pick up the pieces ever since. I’m still putting the pieces back together and have decided that sometimes you just have to get back to doing something that you love, and let the pieces fall where they may.

427307

True to my nature I jumped back in, feet first of course. I know it’s late in the month but if you’re interested in winning a free t-shirt AND contributing to some really cool application check out Hacktoberfest. I’m on my third project so far and keeping an eye out for the fourth. So far I’ve added code to Code Buddies and fixed some CSS for a handy weather app. Right now I’m working on fixing code and CSS for Coder Bounty which is a cool website that post bounties for code fixes and when you do one you get a reward for it.

I’ll be jumping back into the Learn Ruby the Hard Way book and posting about JavaScript again soon too so stay tuned! Happy coding!!

Advertisements

JAVASCRIPT FUNCTIONS Part 2: THE WEB DEVELOPER BOOTCAMP

The Web Developer Bootcamp by Colt Steele

The other day I talked about functions … and today I’m going to talk more about functions! Functions are a huge part of JavaScript so it’s incredibly important to understand them. The Mozilla MDN has a really good explanation of functions I recommend reading.

If we recall from my previous post a basic function looks like this

function sayHello() {
   console.log("Hello World!");
}

You can also add an argument to your function. For example, if you wanted to have a function that takes a number and finds the square you would write:

function square(num) {
     return num * num
}

Then if we wanted to know the square of the number 8 we would write

square(8);
2a71a34118

Note: I forgot the ; after square(8). JavaScript is usually not this forgiving when you forget the semicolon.

You probably also noticed I didn’t use console.log in the second example. The return keyword returns the value to the function caller as well as stops the execution of any more code.

 

 

 

 

 

 

 

JavaScript Functions: The Web Developer BootCamp

The Web Developer Bootcamp

Welcome back to my ramblings on JavaScript as I understand it! My hopes are to help myself better understand what I’m doing and if I can help someone else get this language then that is awesome!

Today is all about functions. What is a function? Well a function wraps chunks of code into nice little packages that can be reused over and over. What does that mean? That means that instead of having to write

console.log(“Hello World”); over and over to say Hello World we can write a function and then call that function at a later time when we need it.

function sayHello() {
   console.log("Hello World!");
}

Then we just need to call the function.

sayHello();

Now, that’s a pretty simplistic use. If you wanted to write a code that sang a song you could console.log() each line but, if you want to use that song more than once you would then have to rewrite that code. A big concept in JavaScript is DRY: Don’t Repeat Yourself. So use a function that you can call over and over instead of typing everything all over again every time! Why make more work for yourself than you need to right!?!?

function singSong() {
     console.log("Mary had a little lamb, little lamb");
     console.log("It's fleece was white as snow.");
     console.log("And everywhere that Mary went.");
     console.log("It was sure to go.");
}

If I wanted to sing the song 2 times I would just put the function into the console twice and it will do all the work for me.

21e4c17344

The function singSong() in the JavaScript console.

Up next will be functions that take arguments. No, not that kind of arguing! 😉

 

 

 

 

 

 

 

 

 

 

 

JavaScript For Loops: The Web Developer Bootcamp

The last post was about while loops. This one is about for loops. Yes, there is more than one kind of loop, there are 5 I believe, but I’ll just focus on these two and the others are just versions of a for or while loop. When do you use a while loop or a for loop? Truthfully you can use them interchangeably BUT you usually want to use a while loop when you don’t know how many times you need to loop or want your code to loop while something is true and to stop when it returns false.

2994f6981e

This will run until either yes or yeah are entered.

A for loop is used when you know how many times you want to loop and for iterating through arrays or want to count something. The following two snippets do the exact same thing. The first is a while loop and the second is a for loop. In this case a for loop makes more sense because we have a set start point and a set end point.

2f9b8f660a

While Loop

ca5974d7dd

For Loop

// Print all numbers between -10 and 19
// prints to the console before the code runs.
console.log("Printing all numbers between - 10 and 19");

// for as long as i is less than 20 add one to the starting number of -10
for(var i = -10; i < 20; i++) {
// print each number (i) to the console. 
     console.log(i)
}

You can read more about for loops on the MDN

JavaScript While Loops : The Web Developer BootCamp

The Web Developer Bootcamp by Colt Steele on Udemy

Loops in JavaScript, and other programming languages, let you repeat something over and over without having to write a ton of code. Say you wanted to count from 1 to 10 in the console. You could write

console.log(1);

console.log(2);

console.log(3);

console.log(4);

console.log(5); ...you get the point

While writing that code 10 times may not be the worst thing you’ve ever done, writing that same code hundreds of times…

e2usislqilsfm

What if you want to print out the numbers between -10 and 19, all even numbers between 10 and 40, all odd numbers between 300 and 333, or all numbers between 5 and 50 that are divisible by both 3 and 5? You could console.log() each number but that’s not a very effective use of your time and would leave you with a rather large .js file.

You can use while loops to do the counting for you and use the time you saved to go jet skiing or mountain climbing or binge watch Jessica Jones on Netflix.

If I want to print out all numbers between -10 and 19 I would simply write

// This says to start the count at -10.
var counter = -10

// While the count is less than 20 
while(counter < 20){
     // Log the number to the console     
     console.log(counter);
     // Count up one
     counter++;
}

If I run that in the console log I will get this:

3a3b50b40a

Here is my code for each of the problems

7966a05ab4

If you open the console on CodePen you will see each of these run. It’s on the bottom left hand side.

d47744b7fa

See the Pen While Loops Exercises by Marcia (@BratCat) on CodePen.

 

Learn Ruby the Hard Way Exercise 39: Hashes, Oh Lovely Hashes

Now we have hashes which are a very handy way to store information. Still confuse me some but hopefully it will make more sense by the time I’m done with this exercise.

70b3c99b94

My code for this exercise.

6722e0dafa

What it looks like in terminal

Next we create a hash module.

99cf97261d

The we create a file to pull this module

152bdafefa

And THEN we fix all our errors lol!

acfbbd749b

First round of errors that I got. It’s always with the typos. One little typo at that.

However, just because Ruby only threw that one error and then ran doesn’t mean there were no errors. At the end of the run I got this:

96fd2af180

This is not what it’s supposed to say!

After reading backwards and forwards it dawned on me what the error was.

995692e0f6

Do you see my error?

I put if !states not if !state …whoops!

Once I found and fixed all my errors I finally got it to run correctly.

3039e47d93

Continue reading

Learn Ruby the Hard Way Exercise 38: Doing Things to Arrays

Arrays! Here and array, there an array, everywhere an array!

09d11769ea

The code for exercise 38. I accidentally saved this as ex37.rb at first. I fixed it though. 

c20929d6bb

What terminal has to say

Study Drills

1) Take each function that is called, and go through the steps for function calls to translate them to what Ruby does. For example, more_stuff.pop() is pop(more_stuff).

0fcddc31f8

2) Translate these two ways to view the function calls in English. For example, more_stuff.pop() reads as, “Call pop on more_stuff.” Meanwhile, pop(more_stuff)means, “Call pop with argument more_stuff.” Understand how they are really the same thing.

3) Go read about “object-oriented programming” online. Confused? I was too. Do not worry. You will learn enough to be dangerous, and you can slowly learn more later.

Wikipedia has a long but pretty thorough explanation of what OOP is. It’s certainly worth the read. TutorialsPoint has a good article on Ruby as an OOP.

4) Read up on what a “class” is in Ruby. Do not read about how other languages use the word “class.” That will only mess you up.

A class in Ruby basically holds information about an item or item(s) that make up something.

5) Do not worry If you do not have any idea what I’m talking about. Programmers like to feel smart so they invented object-oriented programming, named it OOP, and then used it way too much. If you think that’s hard, you should try to use “functional programming.”

6) Find 10 examples of things in the real world that would fit in an array. Try writing some scripts to work with them.

  1. a deck of cards
  2. kinds of toothpaste
  3. colors
  4. makes/models of cars
  5. address book
  6. menu
  7. types of dinosaurs
  8. eye color
  9. students
  10. game names

 

 

Learn Ruby The Hard Way Exercise 37: Symbol Review

For this exercise I have to go trough a list of symbols and words and try to recall what they are/do from memory. Then I have to look them up and test them out to make sure they do what I think they do.

KEYWORDS 

Explanations of all the keywords can be found on ruby-doc.org

KEYWORD DESCRIPTION EXAMPLE
BEGIN Run this block when the script starts. Before sequential execution of the program starts. BEGIN { puts "hi" }
END Run this block when the script is done. Prior to the termination of the program. END { puts "hi" }
alias Create another name for a function. alias X Y
and Logical and, but lower priority than &&. puts "Hello" and "Goodbye"
begin Start a block, usually for exceptions. begin end
break Break out of a loop right now. Prevents a continuous loop.  while true; break; end
case Case style conditional, like an if. case X; when Y; else; end
class Define a new class. class X; end
def Define a new function. def X(); end
defined? Is this class/function/etc. defined already? Used to test if the expression is being used. If not it returns nil defined? Class == "constant"
do Create a block that maybe takes a parameter. (0..5).each do |x| puts x end
else Else conditional. if X; else; end
elsif Else if conditional if X; elsif Y; else; end
end Ends blocks, functions, classes, everything. begin end # many others
ensure Run this code whether an exception happens or not. Code in an ensure clause is guaranteed to be executed. begin ensure end
for For loop syntax. The .each syntax is preferred. for X in Y; end
if If conditional. if X; end
in In part of for-loops. for X in Y; end
module Define a new module. Takes a constant as its argument. module X; end
next Skip to the next element of a .each iterator. (0..5).each {|y| next }
not Logical not. But use ! instead. not true == false
or Logical or. || has precedence. puts "Hello" or "Goodbye"
redo Rerun a code block exactly the same. (0..5).each {|i| redo if i > 2}
rescue Run this code if an exception happens. begin rescue X; end
retry In a rescue clause, says to try the block again. (0..5).each {|i| retry if i > 2}
return Returns a value from a function. Mostly optional. return X
self The current object, class, or module. defined? self == "self"
super The parent class of this class. super
then Can be used with if optionally. Allows you to make one-line conditionals without semi-colons.  if true then puts "hi" end     if a > b then puts "a wins!" end
undef Remove a function definition from a class. undef X
unless Inverse of if. unless false then puts "not" end
until Inverse of while, execute block as long as false. until false; end
when Part of case conditionals. case X; when Y; else; end
while While loop. while true; end
yield Pause and transfer control to the code block. yield

DATA TYPES

TYPE DESCRIPTION EXAMPLE
true True boolean value. true or false == true
false False boolean value. false and true == false
nil Represents “nothing” or “no value”. x = nil
strings Stores textual information. x = "hello"
numbers Stores integers. *note I couldn’t find ‘numbers’ specifically. Everything linked back to Integers. i = 100
floats Stores decimals. i = 10.389
arrays Stores a list of things. j = [1,2,3,4]
hashes Stores a key = value mapping of things. e = {'x' => 1, 'y' => 2}

ESCAPES

ESCAPE DESCRIPTION
\\ Backslash
\' Single-quote
\" Double-quote
\a Bell
\b Backspace
\f Formfeed
\n Newline
\r Carriage
\t Tab
\v Vertical tab

OPERATORS

OPERATOR DESCRIPTION EXAMPLE
+ Add 2 + 4 == 6
- Subtract 2 - 4 == 2
* Multiply 2 * 4 == 8
** Power of 2 ** 4 == 16
/ Divide 2 / 4.0 == 0.5
% Modulus 2 % 4 == 2
> Greater than 4 > 4 == false
. Dot access "1".to_i == 1
:: Colon access Module::Class
[] List brackets [1,2,3,4]
! Not !true == false
< Less than 4 < 4 == false
> Greater than 4 < 4 == false
>= Greater than equal 4 >= 4 == true
<= Less than equal 4 <= 4 == true
<=> Comparison 4 <=> 4 == 0
== Equal 4 == 4 == true
=== Equality 4 === 4 == true
!= Not equal 4 != 4 == false
&& Logical and (higher precedence) true && false == false
|| Logical or (higher precedence) true || false == true
.. Range inclusive (0..3).to_a == [0, 1, 2, 3]
... Range non-inclusive (0...3).to_a == [0, 1, 2]
@ Object scope @var ; @@classvar
@@ Class scope @var ; @@classvar
$ Global scope $stdin

LEARN RUBY THE HARD WAY EXERCISE 36: DESIGNING AND DEBUGGING PART 3

So, maybe I’m an overachiever but I’m done with my game and it did not take a week.

70f428341e

d0d6e8e851

For me the hardest part was coming up with ideas for the rooms. I had a few errors along the way, for example I forgot to define what happens with dead. I ran through every scenario and it seems to be running as expected. If you want to try your hand at it I put it on repl.it.

LEARN RUBY THE HARD WAY EXERCISE 36: DESIGNING AND DEBUGGING PART 2

I used draw.io to map out my game instead of writing it down on index cards. I work better with visuals so this is my preferred method.

ex36

Diagram of game for exercise 36

Now that I have a map of what I want the game to do I will start coding. I don’t expect much trouble with this until I get to the number part but we will see. 🙂