JavaScript Crash Course
JavaScript Crash Course
// Write, Edit and Run your Javascript code using JS Online Compiler
/*console.log("Welcome to Programiz!");
console.log("Hello World!");*/
/* ------------------------------------------------------ */
/* data types - undefined, null, boolean, string, symbol,
number and object */
var a
var b = 9;
a = b; //assignment to a variable
var a = 5;
var b = 10;
var c = "This is a ";
a = a+1;
b = b+5;
c = c + "String!";
console.log(a);
console.log(b);
console.log(c);
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------
/* ---------------------------------------------- */
var StringVar = "I am a \"double quoted string\" " // use double quotes inside
double quotes by using \ in front of them
console.log(StringVar)
var NewStr = "This is the start. "+"This is the end" //concatenate string
console.log(NewStr)
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
------------------
/* ---------------------------------- */
var lastNameLength = 0;
var lastname = "Basak";
lastNameLength = lastname.length; // string length
console.log(lastNameLength);
/*---------------------------------------------------------*/
// Setup
let firstLetterOfLastName = "";
const lastName = "Lovelace";
console.log(firstLetterOfLastName)
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------------------------------
/*----------------------------------------------------------------*/
/* WordBlank game */
function wordBlanks(myNoun,myAdjective,myVerb,myAdverb) {
var result = "";
result = "My "+ myNoun
return result;
}
console.log(wordBlanks("dog","big","runs","fast"));
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
----------
/*-----------------------------------------------------------*/
//arrays
var myArray=["Quincy",23];
var myArray=[["bulls",12],["red sox",15]];
var myArray=["Quincy",23];
var ArrayVal= myArray[0]
console.log(ArrayVal)
// Arrays can be changed by entering in their index value //
var myArray=[["bulls",12],["red sox",15]];
var ArrayVal= myArray[0][1];
console.log(ArrayVal);
function FunctionName(){
console.log("Hello gain JS!")
};
FunctionName();
function FunctionName(a,b){
console.log("Hello gain JS!" +" "+ (a-b))
};
FunctionName(10,5);
—----------------------------------------------------------------------------------
------------------------------------------
/* Variables defined within a function have local scope
Variable outside a function have global scope */
var processed;
function processedArg(num){ //passing an arguement with return example
return (num+3)/5
}
processed = processedArg(7)
console.log(processed)
/* create a function to perform que i.e. remove an item from start in array
and add an item at the end of array */
function queueArray(arr,num){
arr.push(num);
return arr.shift();
}
var testArr = [1,2,3,4,5];
console.log("Before: "+ JSON.stringify(testArr));
console.log(queueArray(testArr,6));
console.log("After: "+ JSON.stringify(testArr));
/* ------------------------------------------------------------------------- */
function isitTrue(val){
if(val) {
return "Yes, it is true";
}
return "No, its false";
}
console.log(isitTrue());
function isitTrue(val){
if(val != 10) {
return "Yes, it is true";
}
return "No, its false";
}
console.log(isitTrue(5));
console.log(isitTrue('12'));
function checkLogic(val){
if (val<=25 && val>= 10){
return "True";
}
return "False";
}
console.log(checkLogic(15));
—----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--
console.log("Welcome to Programiz!");
console.log(golfscore(5,4));
/*------------------------------------------------------------*/
function caseInSwitch(val) {
var answer = "";
switch (val) {
case 1:
answer = "Alpha";
break;
case 2:
answer = "Beta";
break;
case 3:
answer = "Gamma";
break;
case 4:
answer = "Delta";
break;
default:
answer =" Stuff";
break;
}
return answer;
}
console.log(caseInSwitch(1));
//Muliple inputs with same output in a switch statement
function caseInSwitch(val) {
var answer = "";
switch (val) {
case 1:
case 2:
case 3:
answer = "Gamma";
break;
case 4:
case 5:
case 6:
answer = "Delta";
break;
default:
answer =" Stuff";
break;
}
return answer;
}
console.log(caseInSwitch(2));
/* Multiple if else if statements can be replaced with switch cases as well for
efficiency */
/* ------------------------------------------------------------- */
}
console.log(returnBooleanVal(5,7));
}
cc(2), cc(3), cc("K");
console.log(cc(3));
/* ------------------------------------------------------ */
var ourDog = {
"name" : "rocky",
"legs" : 4,
"tails" : 1,
"friends" : ["everything"]
};
// Nested Objects
var myStorage = {
"car": {
"inside":{
"glovebox":"maps",
"passenger":"crumbs"
},
"outside": {
"trunk":"jack"
}
}
};
var gloveBoxContents = myStorage.car.inside["glovebox"];
console.log(gloveBoxContents);
/* ------------------------------------------------------ */
// Record Collection Object Code
var collection = {
2548: {
albumTitle: 'Slippery When Wet',
artist: 'Bon Jovi',
tracks: ['Let It Rock', 'You Give Love a Bad Name']
},
2468: {
albumTitle: '1999',
artist: 'Prince',
tracks: ['1999', 'Little Red Corvette']
},
1245: {
artist: 'Robert Palmer',
tracks: []
},
5439: {
albumTitle: 'ABBA Gold'
}
};
updateRecords(2468,"tracks","test");
console.log(updateRecords(5439, 'artist', 'ABBA'));
/*
-----------------------------------------------------------------------------------
-- —--------------------------------- */
//Iterate using while loops
var myArray =[];
i = 0;
while (i<5){
myArray.push(i);
i++;
}
console.log(myArray);
var product = 1;
function multiplyAll(arr){
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr[i].length; j++) {
product = product * arr[i][j];
}
}
return product;
}
var obj = multiplyAll([[1, 2], [3, 4], [5, 6]]);
console.log(obj);
// Profile Lookup Challenge using Javascript
var contacts = [
{
firstName: "Akira",
lastName: "Laine",
number: "0543236543",
likes: ["Pizza", "Coding", "Brownie Points"],
},
{
firstName: "Harry",
lastName: "Potter",
number: "0994372684",
likes: ["Hogwarts", "Magic", "Hagrid"],
},
{
firstName: "Sherlock",
lastName: "Holmes",
number: "0487345643",
likes: ["Intriguing Cases", "Violin"],
},
{
firstName: "Kristian",
lastName: "Vos",
number: "unknown",
likes: ["JavaScript", "Gaming", "Foxes"],
},
];
console.log(lookUpProfile("Akira", "likes"));
/*
-----------------------------------------------------------------------------------
--------------- */
// ParseInt function convert any string value to an integer of any base with an
additional arguement that would be the base of the number
function ConvertToBaseTwo(str){
return parseInt(str,2);
}
console.log(ConvertToBaseTwo("111"));
/*
-----------------------------------------------------------------------------------
--------------- */
/*
-----------------------------------------------------------------------------------
---------------- */
//Class syntax
function makeClass(){
class vegetable {
constructor(name){
this.name = name;
}
}
return vegetable;
}
const Vegetable = makeClass();
const carrot = new Vegetable('carrot');
console.log(carrot.name);