Advertisement
Setarii

Untitled

Sep 18th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. //Variables
  2.  
  3. //Exercise #1
  4. //Create 3 variables named: myvar1 myvar2 myvar3
  5. //1.1: Do so using individual declarations
  6. var myvar1;
  7. var myvar2;
  8. var myvar3;
  9. //1.2: Do so using a single declaration
  10. var myvar1, myvar2, myvar3;
  11.  
  12. //Exercise #2
  13. //Create a variable called myvar4 with an initial value of 10
  14. var myvar4 = 10;
  15.  
  16. //Exercise #3
  17. //Multiple myvar4 by 2 and store the result in myvar4
  18. myvar4 = myvar4 * 2;
  19.  
  20. //Exercise #4
  21. //Divide myvar4 by 2 then subtract 2 from it. Store the result in myvar4
  22. myvar4 = myvar4 / 2 - 2;
  23.  
  24. //Exercise #5
  25. //Create 2 variables and give them the following values:
  26. //myvar5 should equal 10
  27. //myvar6 should equal 5
  28. //5.1: Do so using individual declarations
  29. var myvar5 = 10;
  30. var myvar6 = 5;
  31.  
  32. //5.2: Do so using a single declaration
  33. var myvar5 = 10, myvar6 = 5;
  34.  
  35. //Exercise #6
  36. //Multiple myvar5 by myvar6 and store in myvar7
  37. var myvar7 = myvar5 * myvar6;
  38.  
  39. //Strings
  40.  
  41. //Exercise #1
  42. //Store your username in a variable called myname
  43. var myname = "Robert"
  44.  
  45. //Exercise #2
  46. //Append the value of myname to the beginning of the text " is awesome."
  47. //store in the variable mycompliment
  48. //2.1: Do so using the + operator
  49. var mycompliment = myname + " is awesome"
  50. console.log(mycompliment);
  51.  
  52. //2.2: Do so using a string template
  53. console.log(`${myname} is awesome.`)
  54.  
  55. //Exercise #3
  56. //Create a variable called myfruit and store the name of a fruit in it
  57. var myfruit = "pineapple";
  58.  
  59. //Use a template string to tell how myname likes myfruit and store the result in myexclaimation
  60. var myexclaimation = console.log(`${myname} likes ${myfruit}.`)
  61.  
  62. //Exercise #4
  63. //Create a variable called alphabet listing all letters from a to z
  64. var alphabet = "abcdefghijklmnopqrstuvwxyz";
  65.  
  66. //Exercise #5
  67. //Get the first character from the alphabet variable
  68. /*var alphextract = alphabet.substring(0,1);
  69. console.log(alphextract); */
  70.  
  71. //Exercise #6
  72. //Replace 'abc' in the alphabet variable with the text 123
  73. var alphabet2 = alphabet.replace("abc","123");
  74. console.log(alphabet2);
  75.  
  76. //Exercise #7
  77. //Get the first 5 characters of the alphabet variable
  78. var alphabetstr = alphabet.substring(0,5);
  79. console.log(alphabetstr);
  80.  
  81. //Arrays
  82.  
  83. //Exercise #1
  84. //create an array of the numbers 0 through 9
  85. /*
  86. var array1 = [0,1,2,3,4,5,6,7,8,9];
  87. var array2 = [0,1,2,3,4,5,6,7,8,9];
  88. var array3 = [0,1,2,3,4,5,6,7,8,9];
  89. var array4 = [0,1,2,3,4,5,6,7,8,9];
  90. var array5 = [0,1,2,3,4,5,6,7,8,9];
  91. */
  92.  
  93.  
  94. //Exercise #2
  95. //Remove first 3 items from the array
  96. /* array1.splice(0,3);
  97. console.log(array1); */
  98.  
  99. //Exercise #3
  100. //Remove last 2 items from the array
  101. /* array2.splice(8,2);
  102. console.log(array2); */
  103.  
  104. //Exercise #4
  105. //Replace the 2nd item with the text 'example'
  106. /* array3.splice (1,2, "example");
  107. console.log(array3); */
  108.  
  109. //Exercise #5
  110. //Add the letters 'a', 'b', 'c' to the start of the array
  111. //Each letter should be its own item
  112. /* array4.splice(0,0, "a", "b", "c");
  113. console.log(array4); */
  114.  
  115. //Exercise #6
  116. //Add 'x' 'y' 'z' to the end of the array where each is a new Item then replace the item containing 'x'
  117. //with the text 'banana'
  118. /* array5.splice(10,2, "x", "y", "z");
  119. array5.splice();
  120. console.log(array5); */
  121.  
  122. //SReject04/16/2018
  123. //Arrays #2
  124.  
  125. //Exercise #1
  126. //create an array of the numbers 0 through 9
  127. var array1 = [0,1,2,3,4,5,6,7,8,9];
  128. var array2 = [0,1,2,3,4,5,6,7,8,9];
  129.  
  130. //Exercise #2
  131. //Remove first 3 items from the array without using .shift()
  132. /* array1.splice(0,3);
  133. console.log(array1);
  134.  
  135. //Exercise #3
  136. //Remove last 2 items from the array without using .pop()
  137. array2.splice(8, 2);
  138. console.log(array2); */
  139.  
  140. //Exercise #4
  141. //Replicate exercises #1-3 using a single statement (That is, do not use ;)
  142. removeValFromIndex = [0,1,2,8,9];
  143. for (var i= removeValFromIndex.length-1; i >=0; i--)
  144. array1.splice(removeValFromIndex[i],1);
  145. console.log(array1);
  146. //PLEASE TELL ME IF THERE'S AN EASIER WAY TO DO THIS CAUSE I DON'T THINK THIS
  147. //IS THE ANSWER YOU WANTED
  148.  
  149. //Exercise #5
  150. //Replace the 2nd item with the text 'example' without using the assignment operator(=)
  151. array1.splice(1,1,"example");
  152. console.log(array1);
  153.  
  154. //Exercise #6
  155. //Add the letters 'a', 'b', 'c' to the start of the array without using .unshift()
  156. //Each letter should be its own item
  157. array1.splice(0,0,"a","b","c");
  158. console.log(array1);
  159.  
  160. //Exercise #7
  161. //Add 'x' 'y' 'z' to the end of the array where each is a new item without using .push()
  162. //then replace the item containing 'x' with the text 'banana' without using the assignment operator(=)
  163. array1.splice(8,0,"x","y","z");
  164. console.log(array1);
  165. array1.splice(8,1,"banana");
  166. console.log(array1);
  167.  
  168. //SReject07/07/2018
  169. //PRACTICE: functions
  170.  
  171. //Exercise #1
  172. //Use code comments to detail what each line of the following does:
  173.  
  174. //function creating reverseText
  175. /* function reverseText(input) {
  176. var letters = input.split(''), //assigning input.split('') to letters and result to ""
  177. result = '';
  178.  
  179. //takes each character in letters and then adds chr to the result. Assuming chr = character?
  180. //I'm unfamiliar with this bit here.
  181. letters.forEach(function (chr) {
  182. result = chr + result;
  183. });
  184. //returns the result of the function from whatever was input into the function
  185. return result;
  186. }
  187. //logs the following text to the console using the reverseText function as
  188. //namredips doohrobhgien yldneirf ruoy tsuJ
  189. console.log(reverseText('Just your friendly neighborhood spiderman'));
  190. */
  191.  
  192.  
  193. //Exercise #2
  194. //Rewrite Exercise #1 so that instead of defining then calling the function, its uses an IIFE
  195. console.log((function ReverseText(input){
  196. var letters = input.split(''), //assigning input.split('') to letters and result to ""
  197. result = '';
  198.  
  199. //takes each character in letters and then adds chr to the result. Assuming chr = character?
  200. //I'm unfamiliar with this bit here.
  201. letters.forEach(function (chr) {
  202. result = chr + result;
  203. });
  204. //returns the result of the function from whatever was input into the function
  205. return result;
  206.  
  207. })("Hi reversetext, I'm dad."));
  208.  
  209. console.log('Finished');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement