More JavaScript Code
- Keshav Batra
- Jul 28, 2022
- 4 min read
After a week of inactivity, I finally got back and resumed my JavaScript code. Im about a quarter of the way finished but I've still got some work to do. I've been going through this by doing them in chapters and that's helped me get this done faster. Below is some of the new code that I've put in.
//Adding
var sum=10+10;
console.log(sum) //shows that the answer is 20
//Subtraction
var difference = 45-33; //difference variable = 12
//Multiplication
var product = 8 * 10;
//Division
var quotient = 66 / 33;
//Incrementing Numbers
//Incrementing a number means to add 1 to it
var myVar = 87;
myVar=myVar+1; //87+1==88
myVar++; //The number has been incremented from 87 to 88 and can keep adding up
//Decrementing Numbers
//Decrementing a number means to subtract 1 from it
var myVar=11;
myVar=myVar-1; //11-1=10
myVar--; //Keeps subtacting
var ourDecimal=5.7;
var product= 2.0* 2.5;
console.log(product)
var remainder;
remainder = 11%3;
var quotient = 4.4 / 2.0;
var a = 3;
var b = 17;
var c = 12;
//Addition
a = a + 12;
b = 9 + b;
c= c + 7;
a += 12;
b += 9;
c += 7;
var a = 11;
var b = 9;
var c = 3;
//Subtracting
a = a - 6;
b = b - 15;
c = c - 1;
//Shortcuts
a -= 6;
b -= 15;
c -= 1;
//Multiplication
var a = 5;
var b = 12;
var c = 4.6;
a = a * 5;
b = 3 * b;
c = c * 10;
//Shortcuts
a *= 5;
b *= 3;
c *= 10;
//Division
var a = 48;
var b = 108;
var c = 33;
a = a / 12;
b = b / 4;
c = c / 11;
//Shortcuts
a /= 12;
b /= 4;
c /= 11;
var firstName = "Keshav"
var lastName = "Batra"
var myStr = "I am a \"double quoted\" string inside \ \"double quotes"
console.log(myStr);
var myStr = '<a href="http://www.example.com" target="_blank">Link</a>';
//Escape Sequences
/*****
//CODE OUTPUT
\'single quote
\"double quote
\\backslash
\n newline
\r carriage return
\t tab
\b backspace
\f form feed
*****/
var myStr = "FirstLine\n\t\\SecondLine\nThirdLine"
//Concatenating Strings with Plus Operator
//example
var ourStr = "I come first." + "I come second"
var myStr = "This is the start." + "This is the end."
console.log(myStr);
//Concatenating Strings with Plus Equals Operator
var ourStr = "I come first.";
ourStr += "I come second.";
var myStr = "This is the first sentence."
myStr += "This is the second sentence."
console.log(myStr);
//Constructing Strings with Variables
var ourName = "freeCodeCamp";
var ourStr = "Hello, our name is" + ourName + ", how are you?"; //ourName is freeCodeCamp
var myName = "Keshav";
var myStr = "My name is " + myName = " and I am well!";
console.log(myStr);
//Appending Variables to Stings
var anAdjective = "awesome!";
var ourStr = "freeCodeCamp is";
ourStr += anAdjective;
var someAdjective = "worthwhile";
var myStr = "Learning to code is";
myStr += someAdjective; //Learning to code is worthwhile
//Length of a String
var firstNameLength = 0;
var firstName = "Ada";
firstNameLength = 0;
var lastName = "Lovelace";
lastNameLength = lastName.length;
console.log(lastNameLength) //If the system is run, the number will appear as 8 since "Lovelace" has 8 letters
//Bracket Notation
var firstLetterOfFirstName = "";
var firstName = "Ada";
firstLetterOfFirstName = firstName [0]; //The number always starts counting from zero and if the bracket notation equals zero, firstName = A
var lastName = "Lovelace";
firstLetterOfLastName = lastName[0]; //The console will print whatever the first letter of the name is
console.log(firstLetterOfLastName) //L will appear since its the first letter of the last name
//String Immutability
var myStr = "Jello World";
myStr[0] = "H"// Fix me
myStr = "Hello World";
//Find the Nth Character
var firstName = "Ada";
var secondLetterOfFirstName = firstName[1]; //This will spawn "D"
//Setup
var lastName = "Lovelace";
var thirdLetterOfLastName = lastName[2]; // This will log V in the console
//Bracket Notation to find last Character in String
var firstName = "Ada";
firstLetterOfFirstName = firstName[firstName.length - 1]; //Since the length is 3 and 1 is subtracted, the length would then be 2 in which the letter would be "A"
var lastName = "Lovelace";
var lastLetterofLastName = lastName[lastName.length - 1]; //This will be the last letter of the last name which is E.
var thirdToLastLetterOfFirstName = firstname[firstName.length - 3] //This gets the third to last letter of the first name
var secondToLastLetterOfLastName = lastName[lastName.length - 2]; //This spawns the second to last letter of the last name in which this is C
//Word Blanks
function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) {
var result = "";
result += "The " + myAdjective + myNoun + myVerb + "to the store"
return result;
}
console.log(wordBlanks("dog", "big", "ran", "quickly"));
console.log(wordBlanks("bike", "slow", "flew", "slowly"));
//Arrays
var ourArray = ["John", 23];
var myArray = ["Jack", 1];
//Nest Arrays
var ourArray = [["the universe", 42], ["everything", 101010]]; //An array within an array
var myArray = [["Bulls", 23], ["White Sox", 34]];
//Access Array Data
var ourArray = [50,60,70];
var ourData = ourArray[0]; //Equals 50
var myArray = [50,60,70];
var myData = myArray[0];
console.log(myData);
//Modify Array Data
var ourArray = [18,64,99];
ourArray[1] = 45;
var myArray = [18, 64, 99];
myArray[0] = 45;
console.log(myArray)
//Access Multi-Dimensional Arrays
var myArray = [[1,2,3], [4,5,6], [7,8,9], [[10,11,12], 13,14]];
var myData = myArray[2][1]; //This will show the first element in the array
console.log(myData); //It will show 8
//Push
var ourArray = ["Stimpson", "J", "cat"]
ourArray.push(["happy", "joy"]);
//ourArray now equals ["Stimpson", "J", "cat", ["happy", "joy"]]
var myArray = [["John", 23], ["cat", 2]];
myArray.push(["dog", 3])
//Pop()
var ourArray = [1,2,3];
var removedFromOurArray = ourArray.pop(); //Removes the last element which is 3 and returns it to a variable
var myArray = [["John", 23], ["cat", 2]];
var removedFromOurArray = myArray.pop();
console.log(myArray)
//Shift
var ourArray = ["Stimpson", "J", ["cat"]]
var removedFromOurArray = ourArray.shift();
var myArray = [["John", 23], ["dog", 3]];
var removedFromMyArray = myArray.shift();
//Unshift
var ourArray = ["Stimpson", "J", ["cat"]]
ourArray.shift();
ourArray.unshift("Happy");
var myArray = [["John", 23], ["dog", 3]];
myArray.shift();
myArray.unshift(["Paul", 35])
//Shopping List
var myList = [["cereal", 3], ["milk", 2], ["bananas", 3], ["juice", 2], ["eggs", 1]];
//Write reusable with functions
function ourReusableFunction(){
console.log("Heyya, World");
}
ourReusableFunction();
function reusableFunction(){
console.log("Hi world");
}
reusableFunction();
function ourFunctionWithArgs(a,b) {
console.log(a - b);
}
ourFunctionWithArgs(10, 5); //Outputs 5
function ourFunctionWithArgs(a, b) {
console.log(a + b);
}
functionWithArgs (10, 5) //10 and 5 go together to make 15
Comentarios