Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                ///////////////////////////////// String Methods ////////////////////////////////

// 1. String Length
// syntax: .length
// examples:
let longestWord = "pneumonoultramicroscopicsilicovolcanoconiosis";
console.log(longestWord.length);

let sentence = "The longest word in the English language is " + longestWord;
console.log(sentence.length);

// 2. Finding the first occurance of text within a string
// syntax: .indexOf("")
// examples:
let repeating = "Dogs are the best. Dogs are the best. Dogs are the best.";
let index = repeating.indexOf("best");
console.log("The word best first shows up at index " + index);

let carmen = "Where in the world is Carmen San Diego?";
console.log(
  "I found her! She's at index " + carmen.indexOf("Carmen San Diego")
);

// we can also include a second parameter that tells the program to find the instance of the text after a specific index, like so:

console.log(
  "The word best shows up next at index " + repeating.indexOf("best", index)
);
//  or
console.log(
  "The word best shows up last at index " + repeating.indexOf("best", 14)
);

// 3. Finding the last occurance of text within a string
// syntax: .lastIndexOf("")
// examples:
repeating = "Dogs are the best. Dogs are the best. Dogs are the best.";
console.log(
  "The word best last shows up at index " + repeating.lastIndexOf("best")
);

let waldo =
  "Lorem ipsum dolor sit amet, where's waldo consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Where's waldo ut enim ad minim veniam, quis nostrud exercitation ullamco laboris where's waldo nisi ut aliquip ex ea commodo where's waldo consequat.";
console.log(
  "Waldo was last seen at index " + waldo.lastIndexOf("where's waldo")
);

// Just as we did for the indexOf() method, we can also include a second parameter that specifies the starting index, like so:
console.log(
  "Waldo was last seen at index " + waldo.lastIndexOf("where's waldo", 25)
);

// 4. Searching a string to see if text is in it, and returning the index if it is.
// If the text is not in the string, the return value is -1.
// syntax: .search("")
// examples:
let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
console.log(alphabet.search("K"));
console.log(alphabet.search("m"));

let randomStuff = "I can't find my purse anywhere, is it in this string?";
if (randomStuff.search("purse") != -1) {
  console.log(
    "I found your purse! It was at index " + randomStuff.search("purse")
  );
} else {
  console.log("No your purse wasn't in that string :( ");
}

//5. Instead of finding out the index of a specific character, this finds the character at a specific index number. 
//synax: charAt(index)
//examples: 

let myAnimal = "zebra";
if (myAnimal.charAt(0) == "t" || myAnimal.charAt(0) == "u" || myAnimal.charAt(0) == "v" || myAnimal.charAt(0) == "w" || myAnimal.charAt(0) == "x" || myAnimal.charAt(0) == "y" || myAnimal.charAt(0) == "z") {
  console.log("This animal begins with a letter between t-z");
} else {
  console.log("This animal begins with a letter between a-s");
}


// The next method deals with "cutting out" a portion of a string by specifying the start and end index of what we want to cut out. Three methods achievie this: slice, substring, and substr. These achieve the same results, so we will only review slice.

// If you'd like to see examples of the other two methods, substring and substr, visit: https://www.w3schools.com/js/js_string_methods.asp

// To find the start and end index, you can use the methods above, or simply count the characters. Each character in a string, including spaces, is one index. Remember that indexes begin at 0, not 1.

// 6. Slice
// syntax: .slice(start, end)
// examples:
repeating = "Dogs are the best. Dogs are 100% the best. Dogs are the best.";
let percentOfBestDogs = repeating.slice(19, 42);
console.log(percentOfBestDogs);

let cut = "Cut my life into pizzas, dis is my last resort";
console.log(cut.slice(0, 23));

// 7. Replacing content in a string. The replace method returns a new string with the changed values instead of altering the original string
// syntax: .replace("old", "new")
// examples:
let theTruth = "Dogs are the best.";
let betterDogs = repeating.replace("best", "all time greatest");
console.log(betterDogs);

// In the situation that we have multiple instances of the word being replaced, and we want to replace *all* of them, follow this syntax:
// Remove the quotation marks from the "old" string and place a forward slash (/) before and after the phrase
// After the last forward slash, include the letter 'g'. This tells JS to alter all instances of the phrase within the slashes.
// Ex:
theTruth = "Dogs are the best. Dogs are 100% the best. Dogs are the best.";
console.log(theTruth.replace(/best/g, "greatest"));

// 8. Converting string to uppercase
// syntax: .toUpperCase()
// examples:
let small = "i want to be in uppercase :( ";
console.log(small.toUpperCase());

// 9. Converting string to lowercase
// syntax: .toLowerCase()
// examples:
let large = "I WANT TO BE IN LOWERCASE :( ";
console.log(large.toLowerCase());

// 10. Convert a string into an array
// This is the final string method we will cover. This method is extremely handy when you are dealing with a file or list of data, such as a class roster that you need to place into an array, and doing so manually would take a long time.
// Strings are split by the specified character within the quotations of the split method:

let names =
  "Bob, Barb, Max, Daniel, Katie, Marie, Elle, Jack, Chloe, Smith, Jane";
let classRoster = names.split(", ");
console.log("The fifth student on the roster is " + classRoster[4]);
console.log("The seventh student on the roster is " + classRoster[6]);
console.log("The last student on the roster is " + classRoster[10]);

// Note that strings can be split by whatever you need them to be split by.
// If your string is a list of values separated by spaces, you simply put a space within the quotes:
let values = "12 13 14 15 16 17 18 19";
let arrayOfValues = values.split(" ");
console.log(arrayOfValues[3]);

///////////////////////// Exercises /////////////////////////
//1. Declare a new variable named text and assign it to the following : "It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair"

//2. Print the length of text to the console

//3. Print the following to the console, filling in blanks with the index of the corresponding word using the string methods:
//"In the preview of A Tale of Two Cities by Charles Dickens, the word 'it' first occurs at index _______, and last occurs at index ____."

//4. Use the slice method to cut out the phrase "it was the season of Light, it was the season of Darkness" from the text variable and save it to a new variable named "excerpt"

//5. Finally, print excerpt to the console in uppercase

//6. Convert the following string to an array, and print the forth element to the console in this format: "The forth item on today's menu is ___"
let dailyMenu =
  "Baked Chicken, Gumbo, Mashed Potatoes, Roasted Veggies, Buttered Roll, Cheesecake";

//7. Print the following statement to the console with the correct element from the array:
// "For dessert today, we have fresh baked _____".

////////////Extra Exercises:

//1. Create a variable that stores the string "There is not a single day this week that the weather is above 84 degrees"
//Log the index of the first instance of the word "is" to the console. 
//Log the index of the last instance of the word "is" to the console.

//2. Use the .search method to see if the letter "E" is in this string. Log your use of the .search method into the console. 
let myVariable = "fpeirfbpiryabphdbspjfbspasdgfyaehpirpiBPHBPHBVPGDFWYPBJDFBNK;SJD"

//3. Using the .slice method, print out the word "utter". 
myVariable = "Butterfly"

//4. Create a new variable, in it, store the string "I really love flowers"
//Create a second variable. In it, replace the word "flowers" from your first variable with something that you really love. 
//Log the second variable into the console. 

//5. Create an object called me that stores your age, full name, and favorite snack. 
//Print out the length of your name. You would do this by printing me.name.length. 
//Search for the letter a in your name. 
//Create a conditional statement that prints out "the letter a is in your name" if it is, and "the letter a is not in your name" if its not. Hint: The condition in your if statement will test whether me.name.search('a') is not equal to -1. 

//6. Create a string containing five colors that you like.  
// Create a new variable called colors, and within it, turn your string into an array by using the .split method. 
// Print out the third element of your array. 

// Uncomment this code! The color of the text should change to your third element!
// let changeColor = document.createElement('p');
// changeColor.innerHTML = colors[2]
// document.body.appendChild(changeColor);
// changeColor.style.color = colors[2];


              
            
!
999px

Console