Skip to content

Commit d11cbba

Browse files
committed
Studio 5 try V1
1 parent e25edc4 commit d11cbba

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

functions/studio/studio-functions.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
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+
const { text } = require("stream/consumers");
4+
35
// Part One: Reverse Characters
46

57
// 1. Define the function as reverseCharacters. Give it one parameter, which will be the string to reverse.
@@ -9,6 +11,36 @@
911
// 5. Use console.log(reverseCharacters(myVariableName)); to call the function and verify that it correctly reverses the characters in the string.
1012
// 6. Optional: Use method chaining to reduce the lines of code within the function.
1113

14+
15+
/*function reverseCharacters(text) {
16+
newText = text.split("");
17+
newText.reverse();
18+
newText = newText.join("");
19+
return newText;
20+
}
21+
22+
let textSample = "Dorea"
23+
console.log(reverseCharacters(textSample));*/
24+
25+
let stringToReverse = "Tank";
26+
27+
function reverseCharacters(str) {
28+
let tmpArr = [];
29+
if(typeof str === "string"){
30+
tmpArr = str.split("");
31+
tmpArr.reverse();
32+
str = tmpArr.join("");
33+
} else if (typeof str === "number") {
34+
str = String(str);
35+
str = reverseCharacters(str);
36+
}
37+
return str;
38+
}
39+
40+
41+
//return str.split("").reverse().join("");
42+
43+
1244
// Part Two: Reverse Digits
1345

1446
// 1. Add an if statement to reverseCharacters to check the typeof the parameter.
@@ -17,6 +49,12 @@
1749
// 4. Return the reversed number.
1850
// 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.
1951

52+
53+
54+
55+
56+
57+
2058
// Part Three: Complete Reversal - Create a new function with one parameter, which is the array we want to change. The function should:
2159

2260
// 1. Define and initialize an empty array.
@@ -30,6 +68,26 @@ let arrayTest1 = ['apple', 'potato', 'Capitalized Words'];
3068
let arrayTest2 = [123, 8897, 42, 1168, 8675309];
3169
let arrayTest3 = ['hello', 'world', 123, 'orange'];
3270

71+
function reverseAll(arr) {
72+
73+
let tmpArr = [];//declare array
74+
75+
console.log(arr);//print parameter
76+
77+
for(let i = 0; i < arr.length; i++){//iterator i = 0 condition less than length and ++
78+
tmpArr.push(reverseCharacters(arr[i]);)//pushes whatever in bracket to the array in reverse characters
79+
}
80+
console.log(tmpArr);
81+
return tmpArr;
82+
}
83+
84+
85+
reverseAll(arrayTest1);
86+
reverseAll(arrayTest2);
87+
reverseAll(arrayTest3);
88+
89+
90+
3391
// Bonus Missions
3492

3593
// 1. Have a clear, descriptive name like funPhrase.

0 commit comments

Comments
 (0)