Skip to content

Commit 1b81e49

Browse files
Quinton KornegayQuinton Kornegay
Quinton Kornegay
authored and
Quinton Kornegay
committed
Functions 1 Studio
1 parent c7de751 commit 1b81e49

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

functions/studio/studio-functions.js

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
//We want to COMPLETELY reverse an array by flipping the order of the entries AND flipping the order of characters in each element.
22

3-
let arrayTest1 = ['apple', 'potato', 'Capitalized Words'];
4-
let arrayTest2 = [123, 8897, 42, 1168, 8675309];
5-
let arrayTest3 = ['hello', 'world', 123, 'orange'];
3+
// TEST VALUES
4+
let str1 = "apple";
5+
let str2 = "LC101";
6+
let str3 = "Capitalized Letters";
7+
let str4 = "I love the smell of code in the morning.";
8+
9+
let num1 = 1234;
10+
let num2 = 8675309;
11+
12+
let arr1 = ['apple', 'potato', 'Capitalized Words'];
13+
let arr2 = [123, 8897, 42, 1168, 8675309];
614

715
// Part One: Reverse Characters
816

@@ -13,14 +21,18 @@ let arrayTest3 = ['hello', 'world', 123, 'orange'];
1321
// 5. Use console.log(reverseCharacters(myVariableName)); to call the function and verify that it correctly reverses the characters in the string.
1422
// 6. Optional: Use method chaining to reduce the lines of code within the function.
1523

16-
function reverseCharacters(arrayTest1) {
17-
let splitArray1 = arrayTest1.split('');
18-
let reversedArray1 = splitArray1.reverse();
19-
let joinArray1 = reversedArray1.join('');
20-
let newArrayTest1 = joinArray1;
21-
24+
function reverseCharacters(element) {
25+
if (typeof element === 'number') {
26+
return String(element).split('').reverse().join('');
27+
} else {
28+
return element.split('').reverse().join('');
29+
}
2230
}
23-
31+
32+
console.log(reverseCharacters(str1));
33+
console.log(reverseCharacters(str2));
34+
console.log(reverseCharacters(str3));
35+
console.log(reverseCharacters(str4));
2436

2537
// Part Two: Reverse Digits
2638

@@ -30,6 +42,9 @@ function reverseCharacters(arrayTest1) {
3042
// 4. Return the reversed number.
3143
// 5. Be sure to print the result returned by the function to verify that your code works for both strings and numbers. Do this before moving on to the next exercise.
3244

45+
console.log(reverseCharacters(num1));
46+
console.log(reverseCharacters(num2));
47+
3348
// Part Three: Complete Reversal
3449

3550
// 1. Define and initialize an empty array.
@@ -39,6 +54,18 @@ function reverseCharacters(arrayTest1) {
3954
// 5. Return the final, reversed array.
4055
// 6. Be sure to print the results from each test case in order to verify your code.
4156

57+
function reversedArr (arr) {
58+
let newArray = [];
59+
for (let i = 0; i < arr.length; i++) {
60+
newArray.push(reverseCharacters(arr[i]));
61+
}
62+
let reversedArray = newArray.reverse();
63+
return reversedArray;
64+
}
65+
66+
console.log(reversedArr(arr1));
67+
console.log(reversedArr(arr2));
68+
4269

4370

4471
// Bonus Missions

0 commit comments

Comments
 (0)