forked from devleagueprep/js-stringMethods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise.js
111 lines (52 loc) · 4.28 KB
/
exercise.js
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*String Methods allow you to work with strings. */
/*Let's practice some string methods. Don't forget to console.log your results and run node REPL in the terminal.*/
/*The string.length property returns the number of characters in the string.
*/
/*1. Declare a variable named `howManyLetters` and find out how many letters are in the given string below. Console.log your result.*/
var longestPlaceName = "Taumatawhakatangihangakoauauotamateaturipukakapiki- maungahoronukupokaiwhenuakitanatahu"; //Yes, this is a real place located in Porangahau, Central Hawke's Bay.
console.log(longestPlaceName);
/*Next, concatenate and console.log the following phrase:
"x is the longest place name in the world and has y letters in its name."
where x represents the value at `longestPlaceName` and y represents the value at `howManyLetters`*/
var longestPlaceNameNumberLetters = longestPlaceName.length;
console.log(longestPlaceName + " is the longest place name in the world and has " + longestPlaceNameNumberLetters + " letters.");
/*The string.charAt() method returns the character at the specified index (position).*/
/*2. Declare a variable named `fifthLetter` and find out which letter is in the 5th position in the string below. Console.log your result.*/
var iDidntKnow = "The national animal of Scotland is the Unicorn";
var fifthLetter = iDidntKnow.charAt(5);
console.log("The fifth letter of '" + iDidntKnow + "' is " + fifthLetter + ".");
/*The indexOf() method returns the index (position) of the first occurence of a specified text in a string.*/
/*3. Declare a variable named `worldLocator` and find the index (position) of the word "world" in the string below. Console.log your result.*/
var randomFact = "All pandas in the world are on loan from China.";
var worldLocator = randomFact.indexOf("world");
console.log("'world' appears as character number " + worldLocator + " in the phrase: " + randomFact);
/*The slice() method extracts a part of a string and returns the extracted part into a new string. This method takes 2 parameters: the starting index (position) and the ending index (position)*/
/*4. Declare a new variable named `scissorHand` that will generate a new string "are afraid" from the existing string below. Console.log your result.*/
var edward = "People are afraid of me because I am different";
var scissorHand = edward.slice(7, 17);
console.log(scissorHand);
/*The replace() method replaces a specified value with another value in a string.*/
/*5. Declare a new variable named `theSifu` that will change the current string from "In order to taste my cup of water you must first fill your cup." to "In order to taste my cup of water you must first empty your cup."; Console.log your result.*/
var grasshopper = "In order to taste my cup of water you must first fill your cup.";
var theSifu = grasshopper.replace("fill", "empty");
console.log(theSifu);
/*The toLowerCase() method converts the characters in a string to lower case.*/
/*6. Declare a variable named `smallKine` that will convert the string below to all lower case. Console.log your result.*/
var bigTime = "BRUuHHHH, I AM DA GreaTest!";
var smallKine = bigTime.toLowerCase();
console.log(smallKine);
/*The toUpperCase() method converts the characters in a string to upper case.*/
/*7. Declare a variable named `bigBand` that will convert the string below to all upper case. Console.log your result.*/
var bandName = "the beatles";
var bigBand = bandName.toUpperCase();
console.log(bigBand);
/*The split() method converts a string into an array, by separating the string into substrings.*/
/*8. Declare a variable named `oreoList` that will convert the string below into an array of strings. Console.log your result.*/
var oreos = "red velvet, cookie dough, peanut butter, banana split, birthday cake";
var oreoListArray = oreos.split();
console.log(oreoListArray);
/*The substr() method is similar to slice(). The difference is that the second parameter specifies the length of the extracted part. The 2 parameters: the starting index (position) and the length of the extracted part. */
/*9. Declare a variable named `favDrink` and extract "gin" from the string below. Console.log your result.*/
var drinkMenu = "rum, gin, vodka, kool-aid, haterade";
var favDrink = drinkMenu.substr(5, 5);
console.log(favDrink);