Open Visual Studio Code At the top click on View->Integrated Terminal (if not already open) In the terminal, first run: npm run pull After this finishes run: npm start
Strings Lecture 05
“No Strings Attached”
Announcements • PS1 releasing this afternoon • Split into 3 parts: walk, jog, sprint
• Start early!!!!!!!!!!!!!!! • Will take significantly longer than PS0
• Office Hours in Sitterson 008 • Mon-Thurs 10am-8pm, Fri 10am-6pm, Sun 12pm-5pm
• Quiz on Friday • Come to tutoring! Thurs 2-6PM in Sitterson 115
Warm-up let foo: number = 1; let bar: number = foo + 7; let baz: number = bar – foo * 2; baz = baz + 1; print(baz); • What is the value printed to the screen? (Submit answer on Poll Everywhere)
Recap - Variables • Variables are places in memory where data can be stored • Two steps: 1. Declaration – reserve a spot in memory • let : ;
2. Assignment – give some variable a value • = ;
*The very first assignment of a variable is known as initialization
Introducing: Strings • So far, we have only used variables to manipulate numerical data • Other data types are common in programming • Strings – textual data • “A string of characters”
String Rules • We’ve worked with strings already! print(“Welcome to the COMP101 Calculator”);
• Strings are denoted by double quotes • Variable declaration • let : ;
• let day: string = “Wednesday”; • print(day); • print(“day”);
• Typescript is case sensitive! String ≠ string
Types • In Typescript we must explicitly specify the type of data our variable will hold during declaration • let : ;
• The type of the data being stored must match the type of the variable • let temp: string = “four”; • let temp: string = 4; • let temp: string = “4”;
+ • We’ve already seen how to add two numbers in Typescript • let num1: number = 5; • let num2: number = 7; • let num3: number = num1 + num2; • print(num3); // prints 12 • The + operator can also be applied to strings • What does it mean to “add” strings?
+ with Strings (String Concatenation) • Same syntax as working with numbers, different outcome • let first: string = “Jeffrey ”; • let last: string = “Young”; • let name: string = first + last; • print(name); • Concatenation – combining one or more things
String Concatenation • We can also concatenate strings and numbers • + will only perform addition if used with two numbers
• Can perform multiple concatenations in a single line • Processed from left to right
let age: number = await promptNumber(“How old are you”); print(“You are ” + age + “ years old”);
promptString • We can similarly prompt a user for string input let dayOfWeek: string = await promptString(“What day is it”);
let dayOfWeek: number = await promptString(“What day is it”); • ***Always double check that types match!***
Hands-On: Compliment Generator • We are going to make a compliment generator! 1. Open 00-compliment-app.ts inside the lec05-strings folder 2. Prompt the user for their name and store this in a variable called name let name: string = await promptString(“What is your name?”);
3. Use string concatenation to print out: “Welcome ” 4. Print out a compliment, also including the name of the user • Ex. “ is A++!”
5. Check in on Poll Everywhere when finished!
VSCode Errors • One error in your code prevents the program from running! • Similar to Word, you’ll see a red underline where VSCode thinks the error is • Just like Word, this isn’t always perfect
• You will also see this error message in the browser
Line number where error occurs
Variable Naming • The name of a variable can be anything you want • Typically, as programmers we name the variable something that will clue us in as to the purpose of that variable • Especially useful as programs grow larger
• Rules: 1. Must not start with a number 2. Cannot contain spaces 3. Cannot contain a special character other than _
• Convention 1. For multi-word variables use camelCase • dayOfWeek, conversionRate, heightInInches, etc.
Hands-on: Mad Libs (Part 1) 1. Open 01-mad-libs-app.ts inside the lec05-strings folder 2. Prompt the user for a positive and negative adjective using two promptString statements. • Store these in two variables named positiveAdj and negativeAdj
3. Prompt the user for a violent verb. Store in a variable named verb 4. Prompt the user for a large number and a small number using two promptNumber statements • Store these in two variables named uncScore and dookScore
5. Compute the difference of these two variables and store in a third variable called winBy 6. Run your code to make sure the prompts work and then check in on Poll-Everywhere. Our game is halfway complete!
Hands-on: Mad Libs (Part 2) 1. Declare a string called madLib1 and initialize it by concatenating the positive adjective • "On February 9th, the " + positiveAdj + " UNC basketball team"
2. Declare a string called madLib2 and initialize it by concatenating the verb and the negative adjective • " is going to " + verb + " the " + negativeAdj + " Dook team"
3. Declare a string called madLib3 and initialize it by concatenating winBy variable • " and win by " + winBy + " points!"
4. Declare a string called finalMadLib and assign it the concatenation of madLib1, madLib2, and madLib3 5. Print your finalMadLib!! 6. Check in on Poll Everywhere when finished!