Exercise – Variables & Data Types

by rangnathellur

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 name="hello"; x=5;  value=true; y=10; sum=x+y;
console.log(sum);
console.log(typeof name);
console.log(typeof value);
console.log(typeof sum);


// 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 x=10; y=20; 
var a=x+y
var b=x/y
var c=x*y
var d=y-x
console.log(a, b, c, d);

// 3)
// Combine at least three strings together
// Log the result to the console
var a="Hello"; b="my"; c="world"
var result= a+b+c;
console.log(result);
// 4)
// Create an array with at least 5 values stored inside
// Log the result to the console
var arr=[10,20,'red',40,50]
console.log(arr);
// 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 myarr= arr.concat([6,7,8])
console.log(myarr)

// 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={

}

// 7) Bonus
// Add a function (method) to the previous object you created to allow it to "speak" to the console. The function should expect a string argument and output "<object.name> said: <string>" to the console.