Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Variables
- //Exercise #1
- //Create 3 variables named: myvar1 myvar2 myvar3
- //1.1: Do so using individual declarations
- var myvar1;
- var myvar2;
- var myvar3;
- //1.2: Do so using a single declaration
- var myvar1, myvar2, myvar3;
- //Exercise #2
- //Create a variable called myvar4 with an initial value of 10
- var myvar4 = 10;
- //Exercise #3
- //Multiple myvar4 by 2 and store the result in myvar4
- myvar4 = myvar4 * 2;
- //Exercise #4
- //Divide myvar4 by 2 then subtract 2 from it. Store the result in myvar4
- myvar4 = myvar4 / 2 - 2;
- //Exercise #5
- //Create 2 variables and give them the following values:
- //myvar5 should equal 10
- //myvar6 should equal 5
- //5.1: Do so using individual declarations
- var myvar5 = 10;
- var myvar6 = 5;
- //5.2: Do so using a single declaration
- var myvar5 = 10, myvar6 = 5;
- //Exercise #6
- //Multiple myvar5 by myvar6 and store in myvar7
- var myvar7 = myvar5 * myvar6;
- //Strings
- //Exercise #1
- //Store your username in a variable called myname
- var myname = "Robert"
- //Exercise #2
- //Append the value of myname to the beginning of the text " is awesome."
- //store in the variable mycompliment
- //2.1: Do so using the + operator
- var mycompliment = myname + " is awesome"
- console.log(mycompliment);
- //2.2: Do so using a string template
- console.log(`${myname} is awesome.`)
- //Exercise #3
- //Create a variable called myfruit and store the name of a fruit in it
- var myfruit = "pineapple";
- //Use a template string to tell how myname likes myfruit and store the result in myexclaimation
- var myexclaimation = console.log(`${myname} likes ${myfruit}.`)
- //Exercise #4
- //Create a variable called alphabet listing all letters from a to z
- var alphabet = "abcdefghijklmnopqrstuvwxyz";
- //Exercise #5
- //Get the first character from the alphabet variable
- /*var alphextract = alphabet.substring(0,1);
- console.log(alphextract); */
- //Exercise #6
- //Replace 'abc' in the alphabet variable with the text 123
- var alphabet2 = alphabet.replace("abc","123");
- console.log(alphabet2);
- //Exercise #7
- //Get the first 5 characters of the alphabet variable
- var alphabetstr = alphabet.substring(0,5);
- console.log(alphabetstr);
- //Arrays
- //Exercise #1
- //create an array of the numbers 0 through 9
- /*
- var array1 = [0,1,2,3,4,5,6,7,8,9];
- var array2 = [0,1,2,3,4,5,6,7,8,9];
- var array3 = [0,1,2,3,4,5,6,7,8,9];
- var array4 = [0,1,2,3,4,5,6,7,8,9];
- var array5 = [0,1,2,3,4,5,6,7,8,9];
- */
- //Exercise #2
- //Remove first 3 items from the array
- /* array1.splice(0,3);
- console.log(array1); */
- //Exercise #3
- //Remove last 2 items from the array
- /* array2.splice(8,2);
- console.log(array2); */
- //Exercise #4
- //Replace the 2nd item with the text 'example'
- /* array3.splice (1,2, "example");
- console.log(array3); */
- //Exercise #5
- //Add the letters 'a', 'b', 'c' to the start of the array
- //Each letter should be its own item
- /* array4.splice(0,0, "a", "b", "c");
- console.log(array4); */
- //Exercise #6
- //Add 'x' 'y' 'z' to the end of the array where each is a new Item then replace the item containing 'x'
- //with the text 'banana'
- /* array5.splice(10,2, "x", "y", "z");
- array5.splice();
- console.log(array5); */
- //SReject04/16/2018
- //Arrays #2
- //Exercise #1
- //create an array of the numbers 0 through 9
- var array1 = [0,1,2,3,4,5,6,7,8,9];
- var array2 = [0,1,2,3,4,5,6,7,8,9];
- //Exercise #2
- //Remove first 3 items from the array without using .shift()
- /* array1.splice(0,3);
- console.log(array1);
- //Exercise #3
- //Remove last 2 items from the array without using .pop()
- array2.splice(8, 2);
- console.log(array2); */
- //Exercise #4
- //Replicate exercises #1-3 using a single statement (That is, do not use ;)
- removeValFromIndex = [0,1,2,8,9];
- for (var i= removeValFromIndex.length-1; i >=0; i--)
- array1.splice(removeValFromIndex[i],1);
- console.log(array1);
- //PLEASE TELL ME IF THERE'S AN EASIER WAY TO DO THIS CAUSE I DON'T THINK THIS
- //IS THE ANSWER YOU WANTED
- //Exercise #5
- //Replace the 2nd item with the text 'example' without using the assignment operator(=)
- array1.splice(1,1,"example");
- console.log(array1);
- //Exercise #6
- //Add the letters 'a', 'b', 'c' to the start of the array without using .unshift()
- //Each letter should be its own item
- array1.splice(0,0,"a","b","c");
- console.log(array1);
- //Exercise #7
- //Add 'x' 'y' 'z' to the end of the array where each is a new item without using .push()
- //then replace the item containing 'x' with the text 'banana' without using the assignment operator(=)
- array1.splice(8,0,"x","y","z");
- console.log(array1);
- array1.splice(8,1,"banana");
- console.log(array1);
- //SReject07/07/2018
- //PRACTICE: functions
- //Exercise #1
- //Use code comments to detail what each line of the following does:
- //function creating reverseText
- /* function reverseText(input) {
- var letters = input.split(''), //assigning input.split('') to letters and result to ""
- result = '';
- //takes each character in letters and then adds chr to the result. Assuming chr = character?
- //I'm unfamiliar with this bit here.
- letters.forEach(function (chr) {
- result = chr + result;
- });
- //returns the result of the function from whatever was input into the function
- return result;
- }
- //logs the following text to the console using the reverseText function as
- //namredips doohrobhgien yldneirf ruoy tsuJ
- console.log(reverseText('Just your friendly neighborhood spiderman'));
- */
- //Exercise #2
- //Rewrite Exercise #1 so that instead of defining then calling the function, its uses an IIFE
- console.log((function ReverseText(input){
- var letters = input.split(''), //assigning input.split('') to letters and result to ""
- result = '';
- //takes each character in letters and then adds chr to the result. Assuming chr = character?
- //I'm unfamiliar with this bit here.
- letters.forEach(function (chr) {
- result = chr + result;
- });
- //returns the result of the function from whatever was input into the function
- return result;
- })("Hi reversetext, I'm dad."));
- console.log('Finished');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement