Skip to content

Commit 039eb8c

Browse files
committed
Functions Studio
1 parent 96eb39c commit 039eb8c

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

functions/studio/studio-functions.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
// 5. Use console.log(reverseCharacters(myVariableName)); to call the function and verify that it correctly reverses the characters in the string.
1010
// 6. Optional: Use method chaining to reduce the lines of code within the function.
1111

12+
function reverseCharacters(str) {
13+
return str.split("").reverse().join("")
14+
}
15+
16+
console.log(reverseCharacters("apple"));
17+
18+
1219
// Part Two: Reverse Digits
1320

1421
// 1. Add an if statement to reverseCharacters to check the typeof the parameter.
@@ -17,6 +24,16 @@
1724
// 4. Return the reversed number.
1825
// 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.
1926

27+
function reverseCharacters(str) {
28+
if (typeof str == "string") {
29+
return str.split("").reverse().join("")
30+
} else if (typeof str == "number") {
31+
str = str.toString();
32+
return str.split("").reverse().join("")
33+
}
34+
}
35+
console.log(reverseCharacters(1234));
36+
2037
// Part Three: Complete Reversal
2138

2239
// 1. Define and initialize an empty array.
@@ -30,22 +47,57 @@ let arrayTest1 = ['apple', 'potato', 'Capitalized Words'];
3047
let arrayTest2 = [123, 8897, 42, 1168, 8675309];
3148
let arrayTest3 = ['hello', 'world', 123, 'orange'];
3249

50+
function reverseArrayAndCharacters(array) {
51+
let newArray = [];
52+
for (let i = 0; i < array.length; i++) {
53+
newArray.push(reverseCharacters (array[i]));
54+
}
55+
return newArray.reverse();
56+
}
57+
58+
console.log(reverseArrayAndCharacters(arrayTest2));
59+
3360
// Bonus Missions
3461

3562
// 1. Have a clear, descriptive name like funPhrase.
3663
// 2. Retrieve only the last character from strings with lengths of 3 or less.
3764
// 3. Retrieve only the first 3 characters from strings with lengths larger than 3.
3865
// 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.
3966

67+
function funPhrase(input) {
68+
let modifiedInput;
69+
if (input.length <= 3) {
70+
modifiedInput = input.slice(-1)
71+
return console.log(`We put the ${modifiedInput} in ${input}.`)
72+
} else modifiedInput = input.slice(0, 3)
73+
return console.log(`We put the ${modifiedInput} in ${input}.`)
74+
}
75+
funPhrase("Big Phrase");
76+
4077
// Test Function
4178

4279
// 1. Outside of the function, define the variable str and initialize it with a string (e.g. 'Functions rock!').
4380
// 2. Call your function and print the returned phrase.
4481

82+
let str = "Functions rock!";
83+
84+
funPhrase("Functions rock!");
85+
4586
// Area of rectangle equal to length x width
4687

4788
// 1. Define a function with the required parameters to calculate the area of a rectangle.
4889
// 2. The function should return the area, NOT print it.
4990
// 3. Call your area function by passing in two arguments - the length and width.
5091
// 4. If only one argument is passed to the function, then the shape is a square. Modify your code to deal with this case.
5192
// 5. Use a template literal to print, “The area is ____ cm^2.”
93+
94+
function areaOfRectangle(length, width) {
95+
let area;
96+
if (arguments.length === 2) {
97+
area = arguments[0] * arguments[1]
98+
return console.log(`The area is ${area} cm^2.`)
99+
} else area = arguments[0] * arguments[0]
100+
return console.log(`The area is ${area} cm^2.`)
101+
}
102+
103+
areaOfRectangle(2);

0 commit comments

Comments
 (0)