Skip to content

csc123kazerouni/lab-ts-variables-data-types

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lab 1 — Variables, data types, and expressions in TypeScript.

If you can read this, press

Ctrl/Cmd + Shift + V

to see a better-formatted version of these instructions.

(See MANUAL.md for instructions on how to use this workspace.)

In this lab you'll get some experience declaring and using variables. You'll also get some experience using existing commands that help us do more complex computations, like using the Math.sqrt function to calculate square roots.

Write all your code in index.ts. Use comments to indicate when you begin each part. For example

// Starting part 1
your code here....

// Starting part 2
your code here....

Part 1: Variables and data types

Consider the expressions listed below. Recall that an expression is anything that can evaluated down to a value.

Your task in this part is to declare and initialize variables to hold each of the values below. For each variable, give it a name and the appropriate data type.

For example, here is the answer to the first one:

const ten: number = 10;
If you can see this, hit Ctrl/Cmd + Shift + V to see the formatted version of this assignment before proceeding. There are lots of characters here for "formatting only" that should not be seen as TypeScript!
  1. 10
  2. Math.PI
  3. "what am i"
  4. "false"
  5. true
  6. [12, 10.4, 6, Math.PI, 0.000001, 1/3]
  7. [true, false, false]
  8. Math.pow(2, 5)
  9. "TypeScript".toLowerCase()
  10. 0
  11. -273.15
  12. 1_000_000
  13. "Hello, world!"
  14. ''
  15. "123"
  16. "true"
  17. false
  18. "Result: " + ((5 * 12) - (48 / 2) + Math.pow(2, 3))
  19. 10 > 3
  20. 5 === 5
  21. "abc".length
  22. Math.sqrt(16)
  23. Math.round(2.8)
  24. "typescript".toUpperCase()
  25. "a" + "b" + "c"

Part 2: Boolean operators

Part 2.1

Consider the following expressions. For each one,

  • Create a const to store the expression's value, just like you did in Part 1.
  • Write the value of the expression in a comment next to the expression.
  1. 7 > 0
  2. true && (10 > 2)
  3. (3 < 1) || (4 >= 4)
  4. !(2 * 2 === 5)
  5. "Is 8 greater than 12? " + (8 > 12)

You may console.log the expressions to check that your expectations for the values match the actual values. But please remove the console.log statements before you submit.

Part 2.2

Suppose you have a const named score, representing a student's numerical score on an exam. For example:

const score: number = 87;

(Note that the value 87 is just an example—you don't necessarily know what the value of score will be.)

Use this score variable for the next two questions.

  1. Write a ternary expression that tells us if score is even or odd. Your expression should "fit" inside the evenOrOdd variable described below. evenOrOdd should have the value "even" if score is even, and "odd" if score is odd.
const evenOrOdd: string = ______________________; // Fill in this blank.

Then, console.log the value of evenOrOdd to confirm that your expression is working correctly.

  1. Write an expression to decide the student's letter grade based on their numerical score, according to the following rules:
  • If the score is greater than or equal to 90, the grade should be "A",
  • Otherwise, if the score is greater than or equal to 80, the grade should be "B"
  • Otherwise, if the score is greater than or equal to 70, the grade should be "C"
  • Otherwise, the score should be "D/F"

You will need to write a compound expression that combines several ternary expressions to accomplish this. I suggest that you begin by drawing a "decision tree" like we have discussed in class.

Your expression should fit in the following variable:

// Fill in this blank. Use multiple lines for clarity as needed.
const grade: string = ____________________________;

Submission

Make a Git commit with a meaningful message. Then push your code to GitHub to complete the submission.

If you run into errors due to git merge conflicts, let me know.

Go to your repository page on GitHub.com and confirm that your latest commit message is present.

About

Lab exercises on variables, data types, and expressions

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published