Exercise – Variables & Data Types

by patrickliang

JavaScript

/**
 * Exercise:
 * Get to know your data types and variables
 *
 * Follow the directions below but feel free to experiment and veer off course.
 * Work in the console directly OR use "console.log()" to output to the console from the script.
 */

// 1)
// Declare 2 to 5 variables (name them as you like)
// Make at least one string, one number and one boolean. 
// Log the typeof each to the console

var x = 'samplestring';
var y = 5;
var z = true;

console.log(typeof x, typeof y, typeof z);

// 2)
// Declare a new variable that stores the result of a mathematical expression
// You can add/multiply/divide/modulo any set of numbers 
// Log the result to the console

var result = 5 * 2;
console.log(result);

// 3)
// Combine at least three strings together
// Log the result to the console

var stringOne = 'design';
var stringTwo = 'user experience';

console.log(stringTwo + ' ' + stringOne);

// 4)
// Create an array with at least 5 values stored inside
// Log the result to the console

var array = [1, 'string', true, 'design', 15.2];
console.log(array);

// 5)
// Use Array.prototype.concat to add another array to your original array
// e.g. var arr3 = arr1.concat(arr2);
// Log the result to the console

var array2 = [6, 7];
var array3 = array. concat(array2);
console.log(array3);

// 6) 
// Create an object literal that represents a person (perhaps you!)
// Define several properties within the object (name, hairColor, age, etc..)
// Define at least one proprety that references an array
//  favColors, siblings, etc...
// Log the object to the console
// And Log just "name" property to the console
// Then... Change the "name" property to something else
// And once again Log the object to the console

var person = {
name: 'Patrick Liang',
hairColor: 'dark brown',
age: 23,
favColors: ['blue', 'black', 'grey', 'olive', 'maroon'],
speak: function(stringTwo) {
	console.log(this.name + "said: " + stringTwo);
}
}

person.speak = function() {
	console.log(this.name + "said: "...