forked from LaunchCodeEducation/javascript-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudio-functions.js
More file actions
85 lines (68 loc) · 3.98 KB
/
studio-functions.js
File metadata and controls
85 lines (68 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//We want to COMPLETELY reverse an array by flipping the order of the entries AND flipping the order of characters in each element.
// Part One: Reverse Characters
// 1. Define the function as reverseCharacters. Give it one parameter, which will be the string to reverse.
// 2. Within the function, split the string into an array, then reverse the array.
// 3. Use join to create the reversed string and return that string from the function.
// 4. Below the function, define and initialize a variable to hold a string.
// 5. Use console.log(reverseCharacters(myVariableName)); to call the function and verify that it correctly reverses the characters in the string.
// 6. Optional: Use method chaining to reduce the lines of code within the function.
let str = 'apple, LC101, Capitalized Letters, I love the smell of code in the morning.'
function reverseCharacters(str) {
if (typeof str === "string" ) {
let lettersArray = str.split('');
let reversedLetterArray = lettersArray.reverse();
return reversedLetterArray.join('');
} else if (typeof str === Number) {
str = String(str);
let numberArray = str.split('');
let reversedNumberArray = numberArray.reverse();
typeof reversedNumberArray === Number
return reversedNumberArray.join('');
}
}
console.log(reverseCharacters(str));
// Part Two: Reverse Digits
// 1. Add an if statement to reverseCharacters to check the typeof the parameter.
// 2. If typeof is ‘string’, return the reversed string as before.
// 3. If typeof is ’number’, convert the parameter to a string, reverse the characters, then convert it back into a number.
// 4. Return the reversed number.
// 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.
// Part Three: Complete Reversal
// 1. Define and initialize an empty array.
// 2. Loop through the old array.
// 3. For each element in the old array, call reverseCharacters to flip the characters or digits.
// 4. Add the reversed string (or number) to the array defined in part ‘a’.
// 5. Return the final, reversed array.
// 6. Be sure to print the results from each test case in order to verify your code.
let arrayTest1 = ['apple', 'potato', 'Capitalized Words'];
let arrayTest2 = [123, 8897, 42, 1168, 8675309];
let arrayTest3 = ['hello', 'world', 123, 'orange'];
let blankArray1 = [];
let blankArray2 = [];
let blankArray3 = [];
for (i = 0; i < arrayTest1.length; i++) {
blankArray1.push(reverseCharacters(arrayTest1[i]));
}
console.log(blankArray1.reverse());
for (i = 0; i < arrayTest2.length; i++){
blankArray2.push(reverseCharacters(arrayTest2[i]));
}
console.log(blankArray2.reverse());
for(i = 0; i < arrayTest3.length; i++){
blankArray3.push(reverseCharacters(arrayTest3[i]));
}
console.log(blankArray3.reverse());
// Bonus Missions
// 1. Have a clear, descriptive name like funPhrase.
// 2. Retrieve only the last character from strings with lengths of 3 or less.
// 3. Retrieve only the first 3 characters from strings with lengths larger than 3.
// 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.
// Test Function
// 1. Outside of the function, define the variable str and initialize it with a string (e.g. 'Functions rock!').
// 2. Call your function and print the returned phrase.
// Area of rectangle equal to length x width
// 1. Define a function with the required parameters to calculate the area of a rectangle.
// 2. The function should return the area, NOT print it.
// 3. Call your area function by passing in two arguments - the length and width.
// 4. If only one argument is passed to the function, then the shape is a square. Modify your code to deal with this case.
// 5. Use a template literal to print, “The area is ____ cm^2.”