Text and Strings
JavaScript objects, APIs, and data formats
1.31.2018 // DataViz for Arch, Urbanism, Humanities
Some inspiration
Colored by topic: satellite / moon / R&D / rockets / space shuttle / russia
Representing the same data two-ways
Dataset:�
String manipulation
String Operators
var fname = "Harry ";
var lname = "Potter";
console.log(fname + lname);
>> "Harry Potter"
var name = "Snuffleupagus";
console.log(name.length);
>> 13
var str = "Apple, Banana, Kiwi";
var res = str.substring(7, 13);
>> "Banana"
var str = "Please locate where Waldo is!";
console.log(str.indexOf("Waldo"));
>> 20
var str = "Let’s go to LA!";
console.log(str.replace("LA", "Chicago"));
>> "Let’s go to Chicago!"
var str = "Hello! How are you?";
console.log(split(str, "!");
>> ["Hello", " how are you?"]
var str = "Once upon a time";
console.log(split(str, " ");
>> ["Once", "upon", "a", "time"]
var arr = ["Once", "upon", "a", "time"];
console.log(join(arr, "! "));
>> "Once! upon! a! time"
if-statements continued:
string comparison and logical operators
Relational operators, for strings
str | Less than |
str == "pizza rocks!" | == also tests equality of strings |
str != "tacos" | != also tests inequality on strings |
str <= "pizza rocks!" | Strings are compared in dictionary order (alphabetical) |
str > "24 apples" | Numbers in strings are also treated in dictionary order |
str.length == 12 | `length` returns number of characters in string |
str.indexOf("pie") == -1 | `indexOf` returns -1 if string isn’t found |
str.charAt(1) == "i" | `charAt` returns character at (zero-based) index |
Logical operators allow you to combine boolean values.
Sometimes you need more complicated logic that combines multiple requirements, e.g.
If today is a weekday, and the student is Sally, send a PB&J sandwich for lunch.
! operator
var hot = true;
var sunny = true;
if( !sunny ) {
console.log("bring an umbrella");
}
&& operator
var hot = true;
var sunny = true;
if( sunny && hot ) {
console.log("wear a hat");
}
|| operator
var cold = true;
var windy = false;
if( cold || windy ) {
console.log("wear a coat!");
}
Truth tables:
First input in orange
Second input in blue
Output in white
| true | false |
true | true | false |
false | false | false |
&& operator
| true | false |
true | true | true |
false | true | false |
|| operator
Examples of logical operators in action
(5 <= x) && (x <= 20) | X is between 5 and 20 (inclusive) |
(x < 10) || (x > 25) | X is not between 10 and 25 (inclusive) |
!((15 <= x) && (x <= 30)) | X is not between 15 and 30 (inclusive) |
One more data type: Objects
Objects are...
var bird = {
species: "bluejay",
color: "blue",
location: [34.23, 498.23],
fly: function() {...},
cry: function() {...}, ...
}
An object has two characteristics...
Object Syntax
var emptyObject = {};
var person = {
name: "Harry Potter",
age: 11,
gender: "Male",
currentLocation: "Surrey, England"
}
{ name: "Harry" }, where key = name and value = Harry
person.name;
// returns "Harry"
person.name = "Agnes";
// person now stores "Agnes" as the name
What’s an API?
API = Application Programming Interface
How It Works
Basic Components of working with an API
APIs are everywhere
Security and Authentication with APIs
Example Projects that Use 3rd Party APIs
How to talk to APIs: HTTP Requests
HTTP(S) = Hypertext Transfer Protocol (Secure)
Common HTTP Status Codes
Usually invisible: URL automatically redirects to another URL.
Your request was invalid: typo, or bad format.
You’re not allowed to access this (not owner, or else sign in?)
You’re asking for something that doesn’t exist.
Something broke in the server.
Can’t reach the server.
AJAX = Asynchronous Javascript And XML*
Ajax for asynchronous client-server communication. From "Head First jQuery" book.
Helpful Reading (via Mozilla)
Using NYTimes API
NYTimes API
Demo: JSON and Working with API Responses