Created
October 18, 2016 01:19
-
-
Save brendalong/0c9a1026aec955398eaa1d28798aa31d to your computer and use it in GitHub Desktop.
String Methods and Array Methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////////// Strings /////////// | |
// In JavaScript, the first position of a string(called the index) is 0. The same is true of arrays. | |
var phrase = "The quick brown fox and the brown cat ate lunch at the Taco House"; | |
console.log(phrase.indexOf("T")); | |
//can also apply to full words | |
var indexOfFox = phrase.indexOf("fox"); | |
console.log("indexOfFox", indexOfFox); | |
console.log("phrase length", phrase.length); | |
newPhrase = phrase.replace("brown", "yellow"); // makes a new string - old one still exists | |
//notice that brown is only replaced on the first one. for global, use the g | |
newPhrase = newPhrase.replace(/the/g, "a really tiny"); | |
console.log("the new phrase", newPhrase); | |
//three methods to extract parts of string | |
var greeting = "Hello world!"; | |
var newGreeting = greeting.slice(3, 8); //Beginning pos and end pos but not end item. | |
console.log("newGreeting", newGreeting); | |
// slice() can use negative to go from end | |
// substring() - The difference is that substring() cannot accept negative indexes. | |
var phraseSplit = phrase.split(" "); //creates an array | |
// If you want to read a string as an array, convert it to an array first - otherwise unpredictable. | |
var text1 = "Hello World!"; // String | |
var text2 = text1.toUpperCase(); // text2 is text1 converted to upper | |
var text1 = "Hello"; | |
var text2 = "World"; | |
text3 = text1.concat(" ",text2); // adds space and then text2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//////// Basic Array Methods ////////// | |
var colors = ["red", "yellow", "blue", "green", "orange", "purple"]; | |
var numbers = [25, 3, 9, 97, 61, 104, 0]; | |
var reversedColors = colors.reverse(); | |
console.log("reversedColors",reversedColors); | |
var sortedColors = colors.sort(); | |
console.log("sortedColors", sortedColors); | |
var sortedNumbersSort = numbers.sort(); | |
console.log("sortedNumbersSort", sortedNumbersSort); | |
var sortedNumbers = numbers.sort(function(first, second) { | |
//console.log(first, second, "and", first - second); | |
return first - second; // <---- Is this true?, If so then sort it | |
}); | |
console.log("sortedNumbers",sortedNumbers); | |
// more details, search mdn array methods and w3 array methods | |
var joinedColors = colors.join(", "); //creates a string | |
console.log("joinedColors",joinedColors, typeof(joinedColors)); | |
var newColorArray = joinedColors.split(", "); | |
console.log("newColorArray", newColorArray, typeof(newColorArray)); | |
// Slice takes items out of an array, makes new one. Beginning pos and end pos but not end item. | |
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango", "Peaches"]; | |
var citrus = fruits.slice(1,3); // ["Orange", "Lemon"] | |
console.log("citrus", citrus); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment