Skip to content

Commit fdf04c6

Browse files
committed
studio completed
1 parent 27036c2 commit fdf04c6

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

functions/studio/studio-functions.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
// Part One: Reverse Characters
44

55
// 1. Define the function as reverseCharacters. Give it one parameter, which will be the string to reverse.
6+
function reverseCharacters(stringToReverse) {
7+
8+
if (typeof stringToReverse === 'string') {
9+
return stringToReverse.split("").reverse().join('');
10+
} else if (typeof stringToReverse === 'number') {
11+
return Number(String(stringToReverse).split("").reverse().join(''));
12+
}
13+
14+
}
15+
let myString = 8675309;
16+
console.log(reverseCharacters(myString));
17+
618
// 2. Within the function, split the string into an array, then reverse the array.
719
// 3. Use join to create the reversed string and return that string from the function.
820
// 4. Below the function, define and initialize a variable to hold a string.
@@ -30,9 +42,30 @@ let arrayTest1 = ['apple', 'potato', 'Capitalized Words'];
3042
let arrayTest2 = [123, 8897, 42, 1168, 8675309];
3143
let arrayTest3 = ['hello', 'world', 123, 'orange'];
3244

45+
function completeReversal(arrToReverse) {
46+
let arrReverse = [];
47+
for (let i = 0; i < arrToReverse.length; i++) {
48+
arrReverse.unshift(reverseCharacters(arrToReverse[i]));
49+
}
50+
return arrReverse;
51+
}
52+
console.log(completeReversal(arrayTest1));
53+
console.log(completeReversal(arrayTest2));
54+
console.log(completeReversal(arrayTest3));
3355
// Bonus Missions
3456

3557
// 1. Have a clear, descriptive name like funPhrase.
58+
function funPhrase(str) {
59+
if (str.length <= 3) {
60+
return str.slice(str.length-1);
61+
} else {
62+
return str.slice(0,3);
63+
}
64+
}
65+
let str1 = "functions Rock!";
66+
console.log(funPhrase('apple'));
67+
console.log(funPhrase('be'));
68+
console.log(`We put the ${funPhrase(str1)} in ${str1}.`);
3669
// 2. Retrieve only the last character from strings with lengths of 3 or less.
3770
// 3. Retrieve only the first 3 characters from strings with lengths larger than 3.
3871
// 4. Use a template literal to return the phrase We put the '___' in '___'. Fill the first blank with the modified string, and fill the second blank with the original string.
@@ -45,6 +78,11 @@ let arrayTest3 = ['hello', 'world', 123, 'orange'];
4578
// Area of rectangle equal to length x width
4679

4780
// 1. Define a function with the required parameters to calculate the area of a rectangle.
81+
function areaOfRectangle(length, width=length) {
82+
return length * width;
83+
}
84+
console.log(`The area is ${areaOfRectangle(2,4)}cm^2.`);
85+
console.log(`The area is ${areaOfRectangle(2)}cm^2.`);
4886
// 2. The function should return the area, NOT print it.
4987
// 3. Call your area function by passing in two arguments - the length and width.
5088
// 4. If only one argument is passed to the function, then the shape is a square. Modify your code to deal with this case.

0 commit comments

Comments
 (0)