Exercises: JavaScript Strings
Standard Exercises
0. What does this code print out? (Validate your result by typing it in the editor).
var title = "My Awesome Webpage";
console.log( "Page Title: " + title );1. And what does this code print out? (Validate your result by typing it in the editor).
var studentName = "Emily";
var studentHeight = 150;
console.log( studentName + " is " + studentHeight + "cm tall" );2. And what does this code print out? (Validate your result by typing it in the editor).
var studentName = "Alexander";
console.log( studentName.charAt(0) );3. Create a string variable called firstName containing your first name and print the length of your name using firstName.length.
4. Create a second variable containing your last name and then create var fullName by concatenating your first and last name together (include a space between the names). Print your full name to the console.
5. Print the string "Your height is: X" where X is the value of the variable ‘height’ below.
var height = 120;
console.log(...);6. Convert 'name' to UPPERCASE and print that to the console. (Tip. Check back for the functions available on a string).
var name = "Alice";
console.log( ... ); // ALICE7. Write the function textStartsWithA(text) that returns true if the supplied parameter starts with the letter ‘A’ else return false. Test it with the following code.
console.log( textStartsWithA("Alex") ); // true
console.log( textStartsWithA("Betty") ); // false8. Write the function containsLetter(text, letter) that returns true if "text" contains the letter described in "letter" otherwise it should return false. Test it with the following code.
console.log( containsLetter("Erika", "T") ); // false
console.log( containsLetter("Francis", "s") ); // true9. Write a script which replaces all spaces in your full name with dashes '-' and print the result to the console. Note that the string.replace() function returns a new string but does not modify the original.
10. Print characters 0, 4 and 8 from the string ‘alphabet’ below;
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";Advanced Exercises
11. Extract the word 'mad' from this string using the substr(start, length) method.
var fullText = "He was a mad scientist";
var mid = fullText.substr(…);
console.log(mid); // Should print "mad"12. Write a function to take the first 3 letters from a given string and place them on the end of the string.
console.log(rotateString("Hello")); // Should print loHel13. Write the function getFileExtension(text); that prints the file extension for a given filename. ie. For "index.html" it should print "hmtl". You will probably want to use ‘find’ and ‘substr’ to do this.
console.log(getFileExtension("index.html")); // Should print "html"
console.log(getFileExtension("photo.jpg")); // Should print "jpg"14. Write the function pyWord that prepends the letters "Py" to a word but only if it doesn’t already start with Py.
console.log(pyWord("Wizard")); // Should print "PyWizard"
console.log(pyWord("PyMonkey")); // Should print "PyMonkey"15. Write the function isEmail(text); that takes a string and returns ‘true’ if it’s a valid email address or false otherwise. Tip. You should check whether the string contains an "@" symbol and, later on, a dot ‘.’.
console.log( isEmail("john@gmail.com") ); // Should print true
console.log( isEmail("johngmail.com") ); // Should print false
console.log( isEmail("john@gmail") ); // Should print false16. Spot the error in this code. Try running it to see the error. Once you have found the error - have a think about how you might fix it.
var name = "John";
var result = 3 + ". " + name;console.log("Hello World");