Skip to content

Commit 68f9484

Browse files
Studio functions and examples
1 parent fe9dd27 commit 68f9484

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

functions/studio/studio-functions.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@
22

33
// Part One: Reverse Characters
44

5-
// 1. Define the function as reverseCharacters. Give it one parameter, which will be the string to reverse.
6-
// 2. Within the function, split the string into an array, then reverse the array.
7-
// 3. Use join to create the reversed string and return that string from the function.
8-
// 4. Below the function, define and initialize a variable to hold a string.
9-
// 5. Use console.log(reverseCharacters(myVariableName)); to call the function and verify that it correctly reverses the characters in the string.
10-
// 6. Optional: Use method chaining to reduce the lines of code within the function.
5+
6+
function reverseCharacters(stringToReverse) {
7+
if (typeof stringToReverse === 'string') { }
8+
if (typeof stringToReverse === 'number') {
9+
stringToReverse = String(stringToReverse);
10+
return Number(stringToReverse.split('').reverse().join(''));
11+
}
12+
return stringToReverse.split('').reverse().join('');
13+
14+
}
15+
let testString = "apple";
16+
console.log(reverseCharacters(testString));
17+
18+
let testStringTwo = 12345;
19+
console.log(reverseCharacters(testStringTwo));
1120

1221
// Part Two: Reverse Digits
1322

@@ -30,6 +39,16 @@ let arrayTest1 = ['apple', 'potato', 'Capitalized Words'];
3039
let arrayTest2 = [123, 8897, 42, 1168, 8675309];
3140
let arrayTest3 = ['hello', 'world', 123, 'orange'];
3241

42+
function reverseEverything(reversingArr) {
43+
let startingArr = [];
44+
for (let index = 0; index < reversingArr.length; index++) {
45+
startingArr.push(reverseCharacters(reversingArr[index]))
46+
}
47+
return startingArr.reverse();
48+
}
49+
50+
console.log(reverseEverything(arrayTest1));
51+
3352
// Bonus Missions
3453

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

functions/try-it/reverse.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
function reverse(str) {
2-
let lettersArray = str.split('');
1+
function reverse(thisIsAString) {
2+
let lettersArray = thisIsAString.split('');
33
let reversedLettersArray = lettersArray.reverse();
4-
return reversedLettersArray.join('');
4+
let reversedLetterArrayJoined = reversedLettersArray.join('');
5+
6+
return reversedLetterArrayJoined;
57
}
8+
9+
let endInfo = reverse("Hiii! This is a test!");
10+
console.log(endInfo);
11+
12+
console.log(reverse("Hiii! This is a test!"));

0 commit comments

Comments
 (0)