Skip to content

Commit 3abb935

Browse files
Update studio-functions.js
done
1 parent 773b9fa commit 3abb935

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

functions/studio/studio-functions.js

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
// 4. Return the reversed number.
1818
// 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.
1919

20-
// Part Three: Complete Reversal - Create a new function with one parameter, which is the array we want to change. The function should:
20+
// Part Three: Complete Reversal
2121

2222
// 1. Define and initialize an empty array.
2323
// 2. Loop through the old array.
@@ -30,6 +30,34 @@ let arrayTest1 = ['apple', 'potato', 'Capitalized Words'];
3030
let arrayTest2 = [123, 8897, 42, 1168, 8675309];
3131
let arrayTest3 = ['hello', 'world', 123, 'orange'];
3232

33+
function reverseCharacters(input) {
34+
let reversed = "";
35+
if (typeof input === "string") {
36+
reversed = input.split("").reverse().join("");
37+
} else if (typeof input === "number") {
38+
reversed = input.toString().split("").reverse().join("");
39+
} else {
40+
console.log("Please enter a string or number");
41+
}
42+
43+
return reversed;
44+
}
45+
46+
// console.log(reverseCharacters('radar'));
47+
48+
function reverseArray(array) {
49+
let reversed = [];
50+
for (let i = 0; i < array.length; i++) {
51+
reversed.unshift(reverseCharacters(array[i]));
52+
}
53+
54+
return reversed;
55+
}
56+
57+
console.log(reverseArray(arrayTest1));
58+
console.log(reverseArray(arrayTest2));
59+
console.log(reverseArray(arrayTest3));
60+
3361
// Bonus Missions
3462

3563
// 1. Have a clear, descriptive name like funPhrase.
@@ -42,10 +70,40 @@ let arrayTest3 = ['hello', 'world', 123, 'orange'];
4270
// 1. Outside of the function, define the variable str and initialize it with a string (e.g. 'Functions rock!').
4371
// 2. Call your function and print the returned phrase.
4472

73+
function funPhrase(phrase) {
74+
let extract = "";
75+
if (typeof phrase !== "string") {
76+
return false;
77+
} else if (phrase.length <= 3) {
78+
extract = phrase.charAt(phrase.length - 1);
79+
} else if (phrase.length > 3) {
80+
extract = phrase.substr(0, 3);
81+
}
82+
83+
return `We put the ${extract} in ${phrase}.`
84+
}
85+
86+
let str = "Functions rock!";
87+
console.log(funPhrase(str));
88+
4589
// Area of rectangle equal to length x width
4690

4791
// 1. Define a function with the required parameters to calculate the area of a rectangle.
4892
// 2. The function should return the area, NOT print it.
4993
// 3. Call your area function by passing in two arguments - the length and width.
5094
// 4. If only one argument is passed to the function, then the shape is a square. Modify your code to deal with this case.
5195
// 5. Use a template literal to print, “The area is ____ cm^2.”
96+
97+
function calculateRectangleArea(length, width) {
98+
let area = 0;
99+
if (width === undefined) {
100+
area = length * length;
101+
} else {
102+
area = length * width;
103+
}
104+
105+
return area;
106+
}
107+
108+
let rectangleCalc = calculateRectangleArea(20);
109+
console.log(`The area is ${rectangleCalc} cm^2.`);

0 commit comments

Comments
 (0)