Introducing Else If Statements

If you have multiple conditions that need to be addressed, you can chain if statements together with else if statements.

if (num > 15) {
return “Bigger than 15”;
} else if (num < 5) {
return “Smaller than 5”;
} else {
return “Between 5 and 15”;
}

Instructions

Convert the logic to use else if statements.

My Solution

function myTest(val) {
if (val > 10) {
return “Greater than 10”;
} else if (val < 5) {
return “Smaller than 5”;
} else { return “Between 5 and 10”; }
}

Challenge: http://www.freecodecamp.com/challenges/introducing-else-if-statements

Introducing Else Statements

When a condition for an if statement is true, the block of code following it is executed. What about when that condition is false? Normally nothing would happen. With an else statement, an alternate block of code can be executed.

if (num > 10) {
return “Bigger than 10”;
} else {
return “10 or Less”;
}

Instructions

Combine the if statements into a single if/else statement.

My Solution

function myTest(val) {
var result = “”;

if (val > 5) {
result = “Bigger than 5”;
} else {
result = “5 or Smaller”;
}
return result;
}

Challenge: http://www.freecodecamp.com/challenges/introducing-else-statements

Comparison Operators in JavaScript

Comparison with the Equality Operator

function myTest(val) {
if (val == 12) {
return “Equal”;
}
return “Not Equal”;
}

Comparison with the Strict Equality Operator

“Strict equality (===) is the counterpart to the equality operator (==). Unlike the equality operator, strict equality tests both the data type and value of the compared elements.”

Examples

3 === 3 // true
3 === ‘3’ // false

In the second example, 3 is a Number type and '3' is a String type.

function myTest(val) {
if (val === 7) {
return “Equal”;
}
return “Not Equal”;
}

Comparison with the Inequality Operator

The inequality operator (!=) is the opposite of the equality operator. It means “Not Equal” and returns false where equality would return true and vice versa

function myTest(val) {
if (val != 99) {
return “Not Equal”;
}
return “Equal”;
}

Comparison with the Strict Inequality Operator

The strict inequality operator (!==) is the opposite of the strict equality operator. It means “Strictly Not Equal” and returns false where strict equality would return true and vice versa. Strict inequality will not convert data types.

function myTest(val) {
if (val !== 17) {
return “Not Equal”;
}
return “Equal”;
}

Comparison with the Greater Than Operator

The greater than operator (>) compares the values of two numbers. If the number to the left is greater than the number to the right, it returns true. Otherwise, it returns false.
Like the equality operator, greater than operator will convert data types of values while comparing.

function myTest(val) {
if (val > 100) {
return “Over 100”;
}
if (val > 10) {
return “Over 10”;
}
return “10 or Under”;
}

Comparison with the Greater Than Equal To Operator

The greater than equal to operator (>=) compares the values of two numbers. If the number to the left is greater than or equal to the number to the right, it returns true. Otherwise, it returns false.
Like the equality operator, greater than equal to operator will convert data types while comparing.

function myTest(val) {
if (val >= 20) {
return “20 or Over”;
}
if (val >= 10) {  // Change this line
return “10 or Over”;
}
return “9 or Under”;
}

Comparison with the Less Than Operator

The less than operator (<) compares the values of two numbers. If the number to the left is less than the number to the right, it returns true. Otherwise, it returns false. Like the equality operator, less than operator converts data types while comparing.

function myTest(val) {
if (val < 25) {
return “Under 25”;
}
if (val < 55) {
return “Under 55”;
}
return “55 or Over”;
}

Comparison with the Less Than Equal To Operator

The less than equal to operator (<=) compares the values of two numbers. If the number to the left is less than or equal the number to the right, it returns true. If the number on the left is greater than the number on the right, it returns false. Like the equality operator, less than equal to converts data types.

function myTest(val) {
if (val <= 12) {
return “Smaller Than or Equal to 12”;
}
if (val <= 24) {
return “Smaller Than or Equal to 24”;
}
return “25 or More”;
}

Comparisons with the Logical And Operator

The logical and operator (&&) returns true if and only if the operands to the left and right of it are true.

function myTest(val) {
if (val <= 50 && val >= 25) {
return “Yes”;
}
return “No”;
}

Comparisons with the Logical Or Operator

The logical or operator (||) returns true if either of the operands is true. Otherwise, it returns false.

function myTest(val) {
if (val < 10 || val > 20) {
return “Outside”;
}
return “Inside”;
}

Thanks to Free Code Camp for the lessons on JavaScript Comparison Operators!

Back to Basic JavaScript?

Ok, so FreeCodeCamp has added some new stuff, including a lot of new Basic JavaScript projects. I’m going back to that section and thought I’d post a few of my solutions here…

Return a Value from a Function with Return

function timesFive(n) {
return n * 5;
}
timesFive(5); // returns 25
timesFive(10); // returns 50

Assignment with a Returned Value

var processed = 0;

function process(num) {
return (num + 3) / 5;
}

processed = process(7); // returns a value of 2

Stand in Line

“In Computer Science a queue is an abstract Data Structure where items are kept in order. New items can be added at the back of the queue and old items are taken off from the front of the queue.

Write a function queue which takes an array (arr) and a number (item) as arguments. Add the number to the end of the array, then remove the first element of array. The queue function should then return the element that was removed.”

function queue(arr, item) {
arr.push(item);
return arr.shift();
}

// Test Setup
var testArr = [1,2,3,4,5];

// Display Code
console.log(“Before: ” + JSON.stringify(testArr));
console.log(queue(testArr, 6)); // Modify this line to test
console.log(“After: ” + JSON.stringify(testArr));

So I completely forget about using push() and had to use some help on this one…

Use Conditional Logic with If Statements

function myFunction(wasThatTrue) {
if (wasThatTrue) {
return “That was true”;
}
return “That was false”;
}

myFunction(true); // returns “That was true”

And I’m done for tonight! Tomorrow’s post will be about Comparison Operators.

Done with Basic Algorithms!

Waypoint: Get Set for Ziplines

So today I finally finished the Basic Algorithm Scripting section @ Free Code Camp.

Now I’m starting Basic Front End Dev Projects aka “Ziplines”…

“Ziplines are front end development projects that will give you a chance to apply the front end skills you’ve developed up to this point.”

So excited!!! 🙂

ohkamanda

P.S. Here’s a list of the resources I used to solve some of these problems:

http://wulkan.me/tag/free-code-camp/
http://ironion.com/bonfire-factorialize-a-number-solution/
http://michaellefkowitz.com/thoughts/first-round-of-bonfires-basic-algorithm-scripting-complete/
http://rafase282.github.io/My-FreeCodeCamp-Code

16 – Where do I belong.js

Bonfire: Where do I belong

Challenge: http://www.freecodecamp.com/challenges/bonfire-where-do-i-belong

Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted.

For example, where([1,2,3,4], 1.5) should return 1 because it is greater than 1 (index 0), but less than 2 (index 1).

Likewise, where([20,3,5], 19) should return 2 because once the array has been sorted it will look like [3,5,20] and 19 is less than 20 (index 2) and greater than 5 (index 1).

Remember to use Read-Search-Ask if you get stuck. Write your own code.

Here are some helpful links:
Array.sort()
http://wulkan.me/bonfire-where-do-i-belong/

Solution:

function where(arr, num) {
  // Find my place in this sorted array.
  arr.push(num);
  var sortArray = arr.sort(function(a,b){
    return a-b;
  });
  var newArray = sortArray.indexOf(num);
  return newArray;
}

where([40, 60], 50);

15 – Seek and Destroy.js

Bonfire: Seek and Destroy

Challenge: http://www.freecodecamp.com/challenges/bonfire-seek-and-destroy

You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.

Remember to use Read-Search-Ask if you get stuck. Write your own code.

Here are some helpful links:
Arguments object
Array.filter()
http://wulkan.me/bonfire-seek-and-destroy/

Solution:

function destroyer(arr) {
  // Remove all the values
  var newArray = arguments[0];
  var removeArgs = [];
  for (i=1; i < arguments.length; i++){
    removeArgs.push(arguments[i]);
  }
  var final = newArray.filter(function(value){
    return removeArgs.indexOf(value) === -1;
  });
  return final;
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);

14 – Falsy Bouncer.js

Bonfire: Falsy Bouncer

Challenge: http://www.freecodecamp.com/challenges/bonfire-falsy-bouncer

Remove all falsy values from an array.

Falsy values in javascript are false, null, 0, “”, undefined, and NaN.

Remember to use Read-Search-Ask if you get stuck. Write your own code.

Here are some helpful links:
Boolean Objects
Array.filter()
http://wulkan.me/bonfire-falsey-bouncer/

Solution:

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  return arr.filter(Boolean); // uses filter to get rid of false values
}

bouncer([7, "ate", "", false, 9]);

13 – Mutations.js

Bonfire: Mutations

Challenge: http://www.freecodecamp.com/challenges/bonfire-mutations

Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array.
For example, [“hello”, “Hello”], should return true because all of the letters in the second string are present in the first, ignoring case.

The arguments [“hello”, “hey”] should return false because the string “hello” does not contain a “y”.

Lastly, [“Alien”, “line”], should return true because all of the letters in “line” are present in “Alien”.

Remember to use Read-Search-Ask if you get stuck. Write your own code.

Here are some helpful links:
String.indexOf()
http://wulkan.me/bonfire-mutations/

Solution:

function mutation(arr) {
  var stringOne = arr[0].toLowerCase();
  var stringTwo = arr[1].toLowerCase();
  for (var i=0; i<stringTwo.length; i++){
    var check = stringOne.indexOf(stringTwo[i]);
    if (check === -1) {
      return false;
    }
  }
  return true;
}

mutation(["hello", "hey"]);

12 – Slasher Flick.js

Bonfire: Slasher Flick

Challenge: http://www.freecodecamp.com/challenges/bonfire-slasher-flick

Return the remaining elements of an array after chopping off n elements from the head.

The head meaning the beginning of the array, or the zeroth index

Remember to use Read-Search-Ask if you get stuck. Write your own code.

Here are some helpful links:
Array.slice()
Array.splice()
http://wulkan.me/bonfire-slasher-flick/

Solution:

function slasher(arr, howMany) {
  // it doesn't always pay to be first
  return arr.slice(howMany);
}

slasher([1, 2, 3], 2);