Keep your Google searches, short & sweet.
split() Javascript
The split method in Javascript, will take in a String as a parameter and then it will split the string to an array of substrings. If two “” are used wit no space in betweeen, it will separate each character including white space. However, if you use “ “ as a parameter to the method/ function then it will split the string by each word, based on the single whitespace character in between the parenthesis.
Here is an example below.
var stringBean = 'Jack and Jill went up the hill';
var splitBean = stringBean.split(' ');
console.log(splitBean);
//this would print out the array below.
[
'Jack', 'and',
'Jill', 'went',
'up', 'the',
'hill'
]
var splitAll = stringBean.split('');
console.log('\n' + splitAll);
//this would print out the array below.
J,a,c,k, ,a,n,d, ,J,i,l,l, ,w,e,n,t, ,u,p, ,t,h,e, ,h,i,l,l
This example shows what happens when someone splits a string with a single space, then it seprates the string into an array of all the individual words within the string. However, say there is a double space, if that's the case then it may be better to throw a regular expression in as the parameter of the string, e.g. var splitAll = stringBean.split(/\s+/, 3)
. For example, this line of code will allow the user to filter and split the string by any amount of whitespaces and then the second parameter the 3, will choose the first three splits essentially. If that's the case then the output would be something like [ 'Jack', 'and', 'Jill' ]
.)
- strings
- integers/floats
- booleans
- arrays
- objects/hashes
- String data:
//2 string examples
var railroadOne = "Reading Railroad";
var railroadTwo = "B & O Railroad";
- Integer and/or float data
//2 integer/float examples
var railroadRent = 25;
var passGo = 200.00;
- Boolean data
//2 boolean examples
var ownsHouse = true;
var ownsHotel = false;
- Array data
//2 array examples
var playerOneProperties = ["Marvin Gardens", "Park Place", "Boardwalk", "Baltic Ave"]
var playerTwoProperties = ["Virginia Avenue", "New York Avenue"]
- OPTIONAL: Hash or Object data
//2 hash/object examples
var boardProperties = {};
boardProperties["one"] = "Mediterranean Avenue";
boardProperties["two"] = "Community Chest";
boardProperties["three"] = "Baltic Avenue";
var interactiveSquares = {};
interactiveSquares["first"] = "Community Chest";
interactiveSquares["second"] = "Income Tax";
interactiveSquares["third"] = "Chance";
You can also make inline code snippets:
var myLuckyNumber = 12;
Also, in my object/ hash example above I learned that in JavaScript I can also assign and declare an object at the sametime.
For example:
var pieceGameplayOrder = {first: "Dog", second: "Car", third: "Ship", fourth: "Canon"};
Another object, Sammie could be defined as a Player object, with specific properties. An example implementation of Sammie could be defined like this.
var Sammie = {propertiesOwned: ['Marvin Gardens', 'Baltic Avenue', 'B & O Railroad'], cash: 2500, hotelsOwned: 0, housesOwned: 3, gamePiece: 'Thimble'}
Or at least, I'm thinking that could be a plausible implementation of an object representing a player who is playing monopoly.
I'd like to review going over hashes and objects, more and the best way to use them in JavaScript to represent a real-life scenario in Monopoly. It seems really cool that I can represent what's happening in the real world in code.
⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️⚡️