Skip to content

Commit b53eb8a

Browse files
committed
Basic Data Structures (ex. 1 to 18)
1 parent 0685592 commit b53eb8a

19 files changed

+424
-18
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
Use an Array to Store a Collection of Data:
3+
We have defined a variable called yourArray.
4+
Complete the statement by assigning an array of at least 5 elements in length to the yourArray variable.
5+
Your array should contain at least one string, one number, and one boolean.
6+
7+
- yourArray should be an array.
8+
- yourArray should be at least 5 elements long.
9+
- yourArray should contain at least one boolean.
10+
- yourArray should contain at least one number.
11+
- yourArray should contain at least one string.
12+
*/
13+
let yourArray = [1, 2, true, false, "string"]; // Changed this line
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
Access an Array's Contents Using Bracket Notation:
3+
In order to complete this challenge, set the 2nd position (index 1) of myArray to anything you want, besides the letter b.
4+
5+
- myArray[0] should be equal to the letter a
6+
- myArray[1] should not be equal to the letter b
7+
- myArray[2] should be equal to the letter c
8+
- myArray[3] should be equal to the letter d
9+
*/
10+
let myArray = ["a", "b", "c", "d"];
11+
// Only changed code below this line
12+
myArray[1] = "not b"
13+
// Only changed code above this line
14+
console.log(myArray);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Add Items to an Array with push() and unshift():
3+
We have defined a function, mixedNumbers, which we are passing an array as an argument.
4+
Modify the function by using push() and unshift() to add 'I', 2, 'three' to the beginning of the array and 7, 'VIII', 9
5+
to the end so that the returned array contains representations of the numbers 1-9 in order.
6+
7+
- mixedNumbers(["IV", 5, "six"]) should now return ["I", 2, "three", "IV", 5, "six", 7, "VIII", 9]
8+
- The mixedNumbers function should utilize the push() method
9+
- The mixedNumbers function should utilize the unshift() method
10+
*/
11+
function mixedNumbers(arr) {
12+
// Only changed code below this line
13+
arr.unshift("I", 2, "three")
14+
arr.push(7, "VIII", 9)
15+
// Only changed code above this line
16+
return arr;
17+
}
18+
19+
console.log(mixedNumbers(['IV', 5, 'six']));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
Remove Items from an Array with pop() and shift():
3+
We have defined a function, popShift, which takes an array as an argument and returns a new array.
4+
Modify the function, using pop() and shift(), to remove the first and last elements of the argument array, and assign the
5+
removed elements to their corresponding variables, so that the returned array contains their values.
6+
7+
- popShift(["challenge", "is", "not", "complete"]) should return ["challenge", "complete"]
8+
- The popShift function should utilize the pop() method
9+
- The popShift function should utilize the shift() method
10+
*/
11+
function popShift(arr) {
12+
let popped = arr.pop(); // Changed this line
13+
let shifted = arr.shift(); // Changed this line
14+
return [shifted, popped];
15+
}
16+
17+
console.log(popShift(['challenge', 'is', 'not', 'complete']));
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
Remove Items Using splice():
3+
We've initialized an array arr. Use splice() to remove elements from arr, so that it only contains elements that sum to the value of 10.
4+
5+
- You should not change the original line of const arr = [2, 4, 5, 1, 7, 5, 2, 1];.
6+
- arr should only contain elements that sum to 10.
7+
- Your code should utilize the splice() method on arr.
8+
- The splice should only remove elements from arr and not add any additional elements to arr.
9+
*/
10+
const arr = [2, 4, 5, 1, 7, 5, 2, 1];
11+
// Only changed code below this line
12+
arr.splice(1, 4);
13+
// Only changed code above this line
14+
console.log(arr);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Add Items Using splice():
3+
We have defined a function, htmlColorNames, which takes an array of HTML colors as an argument.
4+
Modify the function using splice() to remove the first two elements of the array and add 'DarkSalmon' and 'BlanchedAlmond'
5+
in their respective places.
6+
7+
- htmlColorNames should return ["DarkSalmon", "BlanchedAlmond", "LavenderBlush", "PaleTurquoise", "FireBrick"]
8+
- The htmlColorNames function should utilize the splice() method
9+
- You should not use shift() or unshift().
10+
- You should not use array bracket notation.
11+
*/
12+
function htmlColorNames(arr) {
13+
// Only changed code below this line
14+
arr.splice(0, 2, "DarkSalmon", "BlanchedAlmond")
15+
// Only changed code above this line
16+
return arr;
17+
}
18+
19+
console.log(htmlColorNames(['DarkGoldenRod', 'WhiteSmoke', 'LavenderBlush', 'PaleTurquoise', 'FireBrick']));
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Copy Array Items Using slice():
3+
We have defined a function, forecast, that takes an array as an argument.
4+
Modify the function using slice() to extract information from the argument array and return a new array that contains
5+
the string elements warm and sunny.
6+
7+
- forecast should return ["warm", "sunny"]
8+
- The forecast function should utilize the slice() method
9+
*/
10+
function forecast(arr) {
11+
// Only changed code below this line
12+
const extractedArr = arr.slice(2, 4);
13+
console.log(`original array after slice: ${arr}`);
14+
console.log(`extracted array: ${extractedArr}`);
15+
return extractedArr;
16+
}
17+
18+
// Only changed code above this line
19+
console.log(forecast(['cold', 'rainy', 'warm', 'sunny', 'cool', 'thunderstorms']));
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Copy an Array with the Spread Operator:
3+
We have defined a function, copyMachine which takes arr (an array) and num (a number) as arguments.
4+
The function is supposed to return a new array made up of num copies of arr. We have done most of the work for you, but
5+
it doesn't work quite right yet. Modify the function using spread syntax so that it works correctly (hint: another method
6+
we have already covered might come in handy here!).
7+
8+
- copyMachine([true, false, true], 2) should return [[true, false, true], [true, false, true]]
9+
- copyMachine([1, 2, 3], 5) should return [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
10+
- copyMachine([true, true, null], 1) should return [[true, true, null]]
11+
- copyMachine(["it works"], 3) should return [["it works"], ["it works"], ["it works"]]
12+
- The copyMachine function should utilize the spread operator with array arr
13+
*/
14+
function copyMachine(arr, num) {
15+
let newArr = [];
16+
while (num >= 1) {
17+
// Only changed code below this line
18+
newArr.push([...arr]);
19+
// Only changed code above this line
20+
num--;
21+
}
22+
return newArr;
23+
}
24+
25+
console.log(copyMachine([true, false, true], 2));
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
Combine Arrays with the Spread Operator:
3+
We have defined a function spreadOut that returns the variable sentence.
4+
Modify the function using the spread operator so that it returns the array ['learning', 'to', 'code', 'is', 'fun'].
5+
6+
- spreadOut should return ["learning", "to", "code", "is", "fun"]
7+
- The spreadOut function should utilize spread syntax
8+
*/
9+
function spreadOut() {
10+
let fragment = ['to', 'code'];
11+
return ['learning', ...fragment, 'is', 'fun']; // Changed this line
12+
}
13+
14+
console.log(spreadOut());
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Check For The Presence of an Element With indexOf():
3+
indexOf() can be incredibly useful for quickly checking for the presence of an element on an array.
4+
We have defined a function, quickCheck, that takes an array and an element as arguments. Modify the function using
5+
indexOf() so that it returns true if the passed element exists on the array, and false if it does not.
6+
7+
- The quickCheck function should return a boolean (true or false), not a string ("true" or "false")
8+
- quickCheck(["squash", "onions", "shallots"], "mushrooms") should return false
9+
- quickCheck(["onions", "squash", "shallots"], "onions") should return true
10+
- quickCheck([3, 5, 9, 125, 45, 2], 125) should return true
11+
- quickCheck([true, false, false], undefined) should return false
12+
- The quickCheck function should utilize the indexOf() method
13+
*/
14+
function quickCheck(arr, elem) {
15+
// Only changed code below this line
16+
return arr.indexOf(elem) !== -1;
17+
// Only changed code above this line
18+
}
19+
20+
console.log(quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'));

0 commit comments

Comments
 (0)