Skip to content

A : FreeCodeCamp

Art dvp edited this page Dec 21, 2017 · 6 revisions

FreeCodeCamp

JavaScript

Manipulate Arrays With push

data.push()

// Example
var ourArray = ["Stimpson", "J", "cat"];
console.log(ourArray)
ourArray.push(["happy", "joy"]); 
// ourArray now equals ["Stimpson", "J", "cat", ["happy", "joy"]]

// Setup
var myArray = [["John", 23], ["cat", 2]];
console.log(myArray);
// Only change code below this line.
myArray.push(["dog",3]);

console.log(ourArray);
console.log(myArray);

/*
["Stimpson", "J", "cat"]
[["John", 23], ["cat", 2]]
["Stimpson", "J", "cat", ["happy", "joy"]]
[["John", 23], ["cat", 2], ["dog", 3]]
*/

demo

Manipulate Arrays With pop

// Example
var ourArray = [1,2,3];
console.log(ourArray);
var removedFromOurArray = ourArray.pop();
console.log(ourArray);
// removedFromOurArray now equals 3, and ourArray now equals [1,2]

// Setup
var myArray = [["John", 23], ["cat", 2]];
console.log(myArray);
// Only change code below this line.
var removedFromMyArray = myArray.pop();
console.log(myArray);

/*
[1, 2, 3]
[1, 2]
[["John", 23], ["cat", 2]]
[["John", 23]]
*/

demo

Manipulate Arrays With shift

// Example
var ourArray = ["Stimpson", "J", ["cat"]];
console.log(ourArray);
removedFromOurArray = ourArray.shift();
console.log(ourArray);
// removedFromOurArray now equals "Stimpson" and ourArray now equals ["J", ["cat"]].

// Setup
var myArray = [["John", 23], ["dog", 3]];
console.log(myArray);
// Only change code below this line.
var removedFromMyArray = myArray.shift();
console.log(myArray);

/*
["Stimpson", "J", ["cat"]]
["J", ["cat"]]
[["John", 23], ["dog", 3]]
[["dog", 3]]
*/

demo

Manipulate Arrays With unshift

// Example
var ourArray = ["Stimpson", "J", "cat"];
ourArray.shift(); // ourArray now equals ["J", "cat"]
ourArray.unshift("Happy"); 
// ourArray now equals ["Happy", "J", "cat"]

// Setup
var myArray = [["John", 23], ["dog", 3]];
myArray.shift();

// Only change code below this line.
myArray.unshift(["Paul",35]);

//
/*
["Stimpson", "J", "cat"]
["J", "cat"]
[["John", 23], ["dog", 3]]
[["dog", 3]]
[["Paul", 35], ["dog", 3]]
*/

demo

Shopping List

var myList = [["Chocolate Bar",13],["Lamud",44],["Durien",22],["Cheesebite",66],["Coke",33]];

Write Reusable JavaScript with Functions

// Example
function ourReusableFunction() {
  console.log("Heyya, World");
}

ourReusableFunction();

// Only change code below this line
function reusableFunction(){
  console.log("Hi World");
}
reusableFunction();

/*
"Heyya, World"
"Hi World"
*/

demo

Passing Values to Functions with Arguments

// Example
function ourFunctionWithArgs(a, b) {
  console.log(a - b);
}
ourFunctionWithArgs(10, 5); // Outputs 5

// Only change code below this line.
function functionWithArgs(a,b){
  console.log(a + b);
}
functionWithArgs(4,5);

/*
5
9
*/

demo

Global Scope and Functions

// Declare your variable here
var myGlobal = 10;

function fun1() {
  // Assign 5 to oopsGlobal Here
  oopsGlobal = 5;
}

// Only change code above this line
function fun2() {
  var output = "";
  if (typeof myGlobal != "undefined") {
    output += "myGlobal: " + myGlobal;
  }
  if (typeof oopsGlobal != "undefined") {
    output += " oopsGlobal: " + oopsGlobal;
  }
  console.log(output);
}

fun1();
fun2();

demo

Local Scope and Functions

function myLocalScope() {
  'use strict';
  var myVar = 4;
  
  console.log(myVar);
}
myLocalScope();

// Run and check the console
// myVar is not defined outside of myLocalScope
console.log(myVar);

// Now remove the console log line to pass the test

/*
"ReferenceError: myVar is not defined
    at null.js:11:38
    at https://static.jsbin.com/js/prod/runner-4.1.1.min.js:1:13850
    at https://static.jsbin.com/js/prod/runner-4.1.1.min.js:1:10792"
*/

demo

Global vs Local Scope in Functions

// Setup
var outerWear = "T-Shirt";

function myOutfit() {
  // Only change code below this line
  var outerWear = "sweater";
  // Only change code above this line
  return outerWear;
}

console.log(myOutfit());

/* 
"sweater"
*/

demo

Return a Value from a Function with Return

// Example
function minusSeven(num) {
  return num - 7;
}

function timesFive(val) {
  return val * 5;
}
// Only change code below this line
var an = timesFive(10);

console.log(an);

demo

Assignment with a Returned Value

// Example
var changed = 0;

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

changed = change(10);

// Setup
var processed = 0;

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

// Only change code below this line
processed = processArg(7);

/*
2
*/

demo

Stand in Line

function nextInLine(arr, item) {
  // Your code here
  arr.push(item);
  return arr.shift();  // Change this line
}

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

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

/*
"Before: [1,2,3,4,5]"
1
"After: [2,3,4,5,6]"
*/

demo

Understanding Boolean Values

function welcomeToBooleans() {

// Only change code below this line.
  
return true; // Change this line

// Only change code above this line.
}

/*
true
*/

Use Conditional Logic with If Statements

// Example
function ourTrueOrFalse(isItTrue) {
  if (isItTrue) { 
    return "Yes, it's true";
  }
  return "No, it's false";
}

// Setup
function trueOrFalse(wasThatTrue) {

  // Only change code below this line.
  if(wasThatTrue){
    return "Yes, that was true";
  }
  return "No, that was false";
  
  
  // Only change code above this line.

}

// Change this value to test
trueOrFalse(true);
trueOrFalse(false);

/*
"Yes, that was true"
"No, that was false"
*/

demo

Comparison with the Equality Operator

// Setup
function testEqual(val) {
  if (val == 12) { // Change this line
    return "Equal";
  }
  return "Not Equal";
}

// Change this value to test
testEqual(10);

/*
Not Equal

*/

Comparison with the Strict Equality Operator

// Setup
function testStrict(val) {
  if (val === 7) { // Change this line
    return "Equal";
  }
  return "Not Equal";
}

// Change this value to test
testStrict(10);
testStrict(7);
testStrict("7");

/*
"Not Equal"
"Equal"
"Not Equal"
*/

demo

Comparison with the Inequality Operator

// Setup
function testNotEqual(val) {
  if (val != 99) { // Change this line
    return "Not Equal";
  }
  return "Equal";
}

// Change this value to test
testNotEqual(10);

/*
Not Equal
*/

Comparison with the Strict Inequality Operator


// Setup
function testStrictNotEqual(val) {
  // Only Change Code Below this Line
  
  if (val !== 17) {

  // Only Change Code Above this Line

    return "Not Equal";
  }
  return "Equal";
}

// Change this value to test
testStrictNotEqual(10);

/*
Not Equal
*/

Comparison with the Greater Than Operator


function testGreaterThan(val) {
  if (val > 100) {  // Change this line
    return "Over 100";
  }
  
  if (val > 10) {  // Change this line
    return "Over 10";
  }

  return "10 or Under";
}

// Change this value to test
testGreaterThan(10);

/*
10 or Under
*/

Comparison with the Greater Than Or Equal To Operator


function testGreaterOrEqual(val) {
  if (val >= 20) {  // Change this line
    return "20 or Over";
  }
  
  if (val >= 10) {  // Change this line
    return "10 or Over";
  }

  return "9 or Under";
}

// Change this value to test
testGreaterOrEqual(10);

/*
10 or Over
*/

Comparison with the Less Than Operator


function testLessThan(val) {
  if (val < 25) {  // Change this line
    return "Under 25";
  }
  
  if (val < 55) {  // Change this line
    return "Under 55";
  }

  return "55 or Over";
}

// Change this value to test
testLessThan(10);

/*
Under 25
*/

Comparison with the Less Than Or Equal To Operator


function testLessOrEqual(val) {
  if (val <= 12) {  // Change this line
    return "Smaller Than or Equal to 12";
  }
  
  if (val <= 24) {  // Change this line
    return "Smaller Than or Equal to 24";
  }

  return "25 or More";
}

// Change this value to test
testLessOrEqual(10);

Comparisons with the Logical And Operator


function testLogicalAnd(val) {
  // Only change code below this line

  if (val <= 50 && val >= 25) {
      return "Yes";
  }

  // Only change code above this line
  return "No";
}

// Change this value to test
testLogicalAnd(10);

/*
No
*/

Comparisons with the Logical Or Operator


function testLogicalOr(val) {
  // Only change code below this line

  if (val < 10 ||  val > 20 ) {
    return "Outside";
  }

  // Only change code above this line
  return "Inside";
}

// Change this value to test
testLogicalOr(15);

/*
Inside
*/

Introducing Else Statements


function testElse(val) {
  var result = "";
  // Only change code below this line
  
  if (val > 5) {
    result = "Bigger than 5";
  }else{
    result = "5 or Smaller";
  }
  
  // Only change code above this line
  return result;
}

// Change this value to test
testElse(4);

/*
5 or Smaller
*/

Introducing Else If Statements


function testElseIf(val) {
  if (val > 10) {
    return "Greater than 10";
  } else if(val < 5){
    return "Smaller than 5";
  } else {
    return "Between 5 and 10";
  }
}

// Change this value to test
testElseIf(7);

/*
Between 5 and 10
*/