└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # JavaScript-Array-Interview-Practice 2 | 3 | #Question1 4 | ##Create an Array object. 5 | ``` 6 | Method 1 7 | 8 | var fruits = ['Apple', 'Banana']; 9 | console.log(fruits) // [ 'Apple', 'Banana' ] 10 | ``` 11 | ``` 12 | Method 2 13 | 14 | var msgArray = []; 15 | msgArray[0] = 'Hello'; 16 | console.log(msgArray) // [ 'Hello' ] 17 | ``` 18 | ``` 19 | Method 3 20 | 21 | var array = new Array('Hello'); 22 | console.log(array) // [ 'Hello' ] 23 | ``` 24 | ``` 25 | Method 4 26 | 27 | var another = Array.of(1, 2, 3); 28 | console.log(another) // [ 1, 2, 3 ] 29 | ``` 30 | ``` 31 | Method 5 32 | 33 | var b = arrayMaker({7: 1}, {2: 3}); 34 | 35 | function arrayMaker(n) { 36 | console.log(n); 37 | if (n !== typeof Array) { 38 | return Array.prototype.slice.call(arguments); 39 | } 40 | } 41 | 42 | console.log(b) // [ { '7': 1 }, { '2': 3 } ] 43 | ``` 44 | 45 | #Question 2 46 | 47 | ##Take this array var array = [1,2,3,4,5] and copy it using 48 | 49 | 50 | >the slice method and the for loop method 51 | 52 | 53 | ``` 54 | Method 1 - The slice method 55 | 56 | var array = [1,2,3,4,5,6]; 57 | 58 | var result = array.slice(); // to copy an array to new array 59 | 60 | console.log(array); // [1,2,3,4,5,6] 61 | console.log(result); // [1,2,3,4,5,6] 62 | ``` 63 | ``` 64 | Method 2 - The for loop method 65 | 66 | var array = [1, 2, 3, 4, 5, 6]; 67 | Var array2 = [ ]; 68 | 69 | for (var i = 0; i < array.length; ++i) { 70 | 71 | array2[i] = array[i]; 72 | } 73 | 74 | console.log (array2); // [ 1, 2, 3, 4, 5, 6 ] 75 | ``` 76 | 77 | #Question 3 78 | 79 | ##Empty this array var array = [1,2,3,4,5] 80 | 81 | ``` 82 | Method 1 83 | 84 | Var array = [1,2,3,4,5]; 85 | 86 | Array = [ ]; 87 | ``` 88 | N.B 89 | >This is only recommended if you don't have any other references to this array because it will actually create a new empty array and the other reference will still be available to others in memory. 90 | 91 | 92 | ``` 93 | EXAMPLE 94 | var array = [1,2,3,4,5]; 95 | var array2 = array; 96 | 97 | array = [ ]; 98 | 99 | console.log(array); // [ ]; 100 | console.log(array2); // [ 1, 2, 3, 4, 5 ] 101 | ``` 102 | 103 | ``` 104 | Method 2 105 | var array3 = [1,2,3,4,5]; 106 | array3.length = 0 107 | console.log(array3); // [ ]; 108 | ``` 109 | NB 110 | >This even empties to referenced arrays 111 | 112 | ``` 113 | var array3 = [1,2,3,4,5]; 114 | var array4 = array3; 115 | array3.length = 0; 116 | console.log(array3); // [ ]; 117 | console.log(array4); // [ ]; 118 | ``` 119 | ``` 120 | Method 3 121 | var array5 = [1,2,3,4,5]; 122 | array5.splice(0,array5.length); 123 | console.log(array5); // [ ]; 124 | ``` 125 | ``` 126 | Method 4 127 | var array6 = [1,2,3,4,5]; 128 | console.log(array6); // [1,2,3,4,5] 129 | 130 | function emptyArray(array){ 131 | 'use strict'; 132 | while(array.length){ 133 | array6.pop(); 134 | } 135 | } 136 | 137 | emptyArray(array6); // call function 138 | console.log(array6); // [ ] ; now empty 139 | ``` 140 | 141 | #Question 4 142 | ##What type is an Array set to? 143 | ``` 144 | Var array3 = [1,2,3,4,5]; 145 | console.log(typeof(array3)); // Object 146 | ``` 147 | 148 | #Question 5 149 | ##How can you check if something is an Array? 150 | 151 | ``` 152 | Method 1 153 | 154 | var check = [1, 2, 3]; 155 | var a = Array.isArray([1, 2, 3]); 156 | var b = Array.isArray({ 157 | foo: 123 158 | }); 159 | var c = Array.isArray('foobar'); 160 | var d = Array.isArray(undefined); 161 | var e = Array.isArray(check); 162 | 163 | console.log(a); // true 164 | console.log(b); // false 165 | console.log(c); // false 166 | console.log(d); // false 167 | console.log(e); // true 168 | ``` 169 | 170 | ``` 171 | Method 2 172 | 173 | function checkIfArray(array) { 174 | 'use strict'; 175 | 176 | if (Object.prototype.toString.call(array) === '[object Array]') { 177 | console.log('array it is '); 178 | } else { 179 | console.log('array it is Not '); 180 | } 181 | } 182 | 183 | var array2 = 'testing'; 184 | checkIfArray(array2); // array it is Not 185 | var array3 = [1,2,3,4,5]; 186 | checkIfArray(array3); //array it is 187 | ``` 188 | 189 | ``` 190 | Method 3 191 | 192 | var array = [1, 2, 3, 4, 5]; 193 | 194 | function checkIfArray(object) { 195 | 'use strict'; 196 | if (typeof object === 'string') { 197 | console.log('array it is NOT '); 198 | } else { 199 | console.log('array it is '); 200 | } 201 | } 202 | 203 | checkIfArray(array); //array it is 204 | ``` 205 | 206 | 207 | #Question 6 208 | ##Add an item to the end of an array. 209 | ``` 210 | Method 1 211 | var array = ['a','b','c']; 212 | 213 | array.push('d'); 214 | console.log(array); // [ 'a', 'b', 'c', 'd' ] 215 | ``` 216 | ``` 217 | Method 2 218 | array[array.length] = 'e'; 219 | console.log(array); // [ 'a', 'b', 'c', 'd', 'e' ] 220 | ``` 221 | 222 | #Question 7 223 | ##Find the index position of d in this array 224 | var arr= ['a','b','c','d']; 225 | 226 | `` 227 | Answer : console.log(arr.indexOf('d')); // 3 228 | `` 229 | 230 | #Question 8 231 | ##What will be returned if you look for the index of something that does not exist? 232 | 233 | `` 234 | var arr= ['a','b','c','d']; 235 | console.log(arr.indexOf(7)); // -1 === does not exist 236 | `` 237 | 238 | 239 | #Question 9 240 | ##Write a function to check if milk exists in your array 241 | var items = ['milk', 'bread', 'sugar']; 242 | 243 | Answer 244 | 245 | ``` 246 | var items = ['milk', 'bread', 'sugar']; 247 | 248 | function checkForProduct(item){ 249 | 250 | if (items.indexOf(item) === -1) { 251 | 252 | console.log('item does not exist'); 253 | } else { 254 | 255 | console.log('item is in your list'); 256 | 257 | } 258 | } 259 | 260 | checkForProduct('socks'); //item does not exist 261 | checkForProduct('milk'); //item is in your list 262 | ``` 263 | 264 | 265 | 266 | 267 | 268 | #Question 10 269 | ##Now you've found milk exists add some code to find the index of milk and remove that item. 270 | 271 | ``` 272 | var items = ['milk', 'bread', 'sugar']; 273 | 274 | //find index of item if it exists 275 | var a = items.indexOf('milk'); 276 | console.log(a); // 0 277 | 278 | //remove that index from array 279 | items.splice(0,1); 280 | console.log(items); // [ 'bread', 'sugar'] 281 | ``` 282 | 283 | #Question 11 284 | ##List the ways to loop over an array. 285 | 286 | 287 | >For Each 288 | 289 | >For in 290 | 291 | >For loop 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | #Question 12 300 | ##Write some code to put these numbers in order 301 | var numbers = [1, 12, 2 ,23,77,7,33,5,99,234,]; 302 | 303 | ``` 304 | var numbers2 = [1, 12, 2 ,23,77,7,33,5,99,234]; 305 | var numbers3 = numbers2.sort((a, b) => { 306 | return a - b; 307 | }); 308 | 309 | console.log(numbers3); // [ 1, 2, 5, 7, 12, 23, 33, 77, 99, 234 ] 310 | ``` 311 | 312 | #Question 13 313 | ##Write some code to place this list in alphabetical order 314 | var p = ['a','z','e','y']; 315 | ``` 316 | var p = ['a','z','e','y']; 317 | p.sort(); 318 | console.log(p); // [ 'a', 'e', 'y', 'z' ] 319 | ``` 320 | 321 | #Question 14 322 | ##What is the length of these arrays 323 | 324 | ``` 325 | A. var arr1 = [,,,]; 326 | 327 | B. var arr2 = new Array(3) 328 | 329 | C. var arr3 = [1,2,3,4,5] 330 | 331 | D. var array = [ [1,2,3], [4,5,6] ]; 332 | 333 | E. var array[0].length = [ [1,2,3], [4,5,6] ]; 334 | 335 | 336 | Results 337 | 338 | A. arr1.length = 3 339 | B. arr2.length = 3 340 | C. arr3.length = 5 341 | D. array.length = 2 counts the number of internal array 342 | E. array[0].length = 3 first internal array within the outer array 343 | ``` 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | #Question 15 353 | ##What are the results of these splice and slice methods 354 | 355 | ``` 356 | var a = ['zero', 'one', 'two', 'three']; 357 | var names = ['jason', 'john', 'peter', 'karen']; 358 | 359 | var sliced = a.slice(1, 3); 360 | var spliced = names.splice(1,3); 361 | 362 | 363 | The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). The original array will not be modified. 364 | 365 | console.log(sliced); // creates a new array ['one', 'two'] 366 | console.log(a); // main array remains untouched 367 | 368 | The splice() method changes the content of an array by removing existing elements and/or adding new elements. 369 | 370 | console.log(spliced); // it returns [ 'john', 'peter', 'karen' ] 371 | console.log(names); // however the array only contains jason now 372 | 373 | ``` 374 | 375 | #Question 16 376 | ##What are the console logs of these shift and unshift methods 377 | 378 | ``` 379 | Var a = [ ] ; 380 | 381 | We take an empty array and 382 | 383 | a.unshift(1); 384 | var a = console.log(a) 385 | a.unshift(22); 386 | var b = console.log(a) 387 | a.shift(); 388 | var c = console.log(a) 389 | a.unshift(3,[4,5]); 390 | var d = console.log(a) 391 | a.shift(); 392 | var e = console.log(a) 393 | a.shift(); 394 | var f = console.log(a) 395 | a.shift(); 396 | var g = console.log(a) 397 | Results 398 | 399 | Var a = [ 1 ] // we a.unshift(1) so added 1 to front 400 | 401 | Var b = [ 22, 1 ] // we a.unshift(22) so added 22 to front 402 | 403 | Var c = [ 1 ] // we a.shift() so removed the first element 404 | 405 | Var d = [ 3, [ 4, 5 ], 1 ] // we a.unshift(3,[4,5]) so added 406 | these to front 407 | Var e = [ [ 4, 5 ], 1 ] // we a.shift() so remove first element 408 | 409 | Var f = [ 1 ] // we a.shift() so remove first element 410 | 411 | Var g = [ ] // we a.shift() so remove first element leaving it 412 | empty 413 | ``` 414 | 415 | 416 | #Question 17 417 | 418 | ##Using reduce add all these numbers 419 | var numbers = [1, 2, 3, 4, 5, 6]; 420 | 421 | ``` 422 | var numbers = [1, 2, 3, 4, 5, 6]; 423 | 424 | var total = numbers.reduce((a, b) => { 425 | return a + b; 426 | }); 427 | 428 | console.log(total); // Total returned is : 21 429 | ``` 430 | 431 | #Question 18 432 | ##Flatten this array to one single array using reduce 433 | Var array = [[0, 1], [2, 3], [4, 5]]; 434 | 435 | ``` 436 | Var array = [[0, 1], [2, 3], [4, 5]]; 437 | 438 | var flattened = array.reduce(function(a, b) { 439 | return a.concat(b); 440 | },[ ]); 441 | 442 | console.log(flattened); // [ 0, 1, 2, 3, 4, 5 ] 443 | ``` 444 | 445 | #Question 19 446 | ##Filter this array to return just the dogs 447 | 448 | ``` 449 | var animals = [ 450 | { name: "Jason", species:"rabbit"}, 451 | { name: "Jessica", species:"dog"}, 452 | { name: "Jacky", species:"owl"}, 453 | { name: "Luke", species:"fish"}, 454 | { name: "Junior", species:"rat"}, 455 | { name: "Thomas", species:"cat"} 456 | ] 457 | Answer 458 | 459 | /****************************************** 460 | filter method with callback function 461 | ******************************************/ 462 | 463 | var dogs = animals.filter(function(animals){ 464 | return animals.species === "dog"; 465 | }); 466 | 467 | console.log(dogs); 468 | 469 | Returns 470 | 471 | [ { name: 'Jessica', species: 'dog' }] 472 | 473 | The filter() method creates a new array with all elements that pass the test implemented by the provided function. 474 | ``` 475 | 476 | 477 | 478 | 479 | #Question 20 480 | ##Using array in question 19 use map function to return all the species 481 | ``` 482 | var types = animals.map(function(animals){ 483 | return animals.species; 484 | }); 485 | console.log(types); // [ 'rabbit', 'dog', 'owl', 'fish', 'rat', 'cat' ] 486 | ``` 487 | --------------------------------------------------------------------------------