A project aimed to help the user master Javascript with a test-driven approach. Each unit contains an annotated tutorial on the code itself and the ability to run Unit Tests to verify the expected behavior.
- Arrays
- Bad Practices and Solutions
- Booleans
- Built-in Functions
- Closure
- Comparisons
- Conditionals
- Exceptions
- Falsey Values
- Function Expressions
- Function Expressions as Parameters
- Functions
- General Performance
- Hoisting
- Logical Assignment
- Loops
- Namespaces
- Numbers
- Object Prototype
- Objects
- Objects Functionality
- Prototypes
- Strings
- Switch Block
- Ternary Conditionals
- Truthy Values
- Variables
/*Arrays*/
/* An array is a data structure with automatically indexed positions*/
it('Arrays can be accessed by indices', function () {
//The brackets indicate to the compiler to make an array and fill it with
//the comma-separated values between the brackets
var arrayOfStrings = [ "StringZero" , "StringOne" , "StringTwo"];
//We can access any location of the zero-based array
expect(arrayOfStrings[1]).toBe("StringOne");
});
it('You can reference and change specific cells with indices', function () {
var arrayOfStrings = [ "StringOne" , "StringOne" , "StringTwo"];
arrayOfStrings[0] = "StringZero"; //You can change the value contained at any index
expect(arrayOfStrings[0]).toBe("StringZero");
});
it('You can access the length of Arrays', function () {
var arrayOfStrings = [ "StringZero" , "StringOne" , "StringTwo"];
expect(arrayOfStrings.length).toBe(3);
});
it('Pop() function removes a cell from the back of the array', function () {
var arrayOfStrings = [ "StringZero" , "StringOne" , "StringTwo"];
//Pop() function deletes the last position and retrieves its value
expect(arrayOfStrings.pop()).toBe("StringTwo");
expect(arrayOfStrings.length).toBe(2);//The array length automatically adjusts
});
it('shift() function removes a cell from the beginning of the array', function () {
var arrayOfStrings = [ "StringZero" , "StringOne" , "StringTwo"];
//shift() function deletes the first position and retrieves its value
expect(arrayOfStrings.shift()).toBe("StringZero");
expect(arrayOfStrings.length).toBe(2);//The array length automatically adjusts
});
it('Push() function adds a cell and its contents to the back of the array', function () {
var arrayOfStrings = [ "StringZero" , "StringOne" ];
arrayOfStrings.push('StringTwo');
//Push() will add a cell onto the back of the arrays
expect(arrayOfStrings[2]).toBe("StringTwo");
expect(arrayOfStrings.length).toBe(3);//The array length automatically adjusts
});
it('Arrays can hold different types of variables ', function () {
var StringTwoVariable = "StringTwo";
var arrayOfStrings = [ "StringZero" , 1 , StringTwoVariable];
//When assigning variables to an entry of an array, the variable name is not
//used, instead the array uses the internal value
expect(arrayOfStrings[2]).toBe("StringTwo");
});
it('Arrays can hold Arrays ', function () {
var firstArrayOfStrings = [ "StringZero" , "StringOne" , "StringTwo"];
var secondArrayOfStrings = [ "StringThree" , "StringFour" , "StringFive"];
var firstAndSecondArrays = [firstArrayOfStrings , secondArrayOfStrings];
expect(typeof firstAndSecondArrays).toBe("object");//Arrays are treated as objects
//You can access any index of the composed array
expect(firstAndSecondArrays[1][0]).toBe("StringThree");
});
it('Arrays can hold undefined values ', function () {
var arrayOfStrings = [ "StringZero" , "StringOne" , "StringTwo"];
//to make a cell empty. we can use the keyword 'undefined', which means 'NO CONTENTS'
arrayOfStrings[1] = undefined;
expect(typeof arrayOfStrings[1]).toBe("undefined");
expect(arrayOfStrings[1]).toBe(undefined);
//The array length does not change when some of the values are undefined
expect(arrayOfStrings.length).toBe(3);
});
it('Arrays can be created with no values', function () {
var arrayOfStrings = [];
expect(typeof arrayOfStrings).toBe("object");
//When you access an index of the array with no value, the array returns 'undefined'
expect(arrayOfStrings[1]).toBe(undefined);
expect(arrayOfStrings.length).toBe(0);
});
npm install
bower install
grunt test
If you have any questions, please feel free to ask me: Martin Chavez Aguilar
- Martin Chavez Aguilar - Wrote the project