Week 1: Basics and for loops
How to do this
- Go into your homework directory and run the command:
git clone https://github.com/aethertap/typescript-template hw1
- Enter the
hw1
directory and typenpm i
- Type
npm run br
- If it prints a bunch of stuff then ends up with "It works." you're good to go.
- If there are problems, let me know.
- The starting point for your program will be in
src/app.ts
, but you can add other files and import them as needed. For now,app.ts
is probably all you'll need.
Notes
- Remember to
export
your functions so that they can be called in the tests - Remember that a new line can only be inserted in a string by using the
\n
character (that is a BACKslash, not the one by the question mark!)
Questions
- What do these loops print?
for(let i = 0; i < 10; i = i + 2) {
console.log(i);
}
for(let i = 100; i >= 0; i = i - 7) {
console.log(i);
}
for(let i = 1; i <= 10; i = i + 1) {
console.log(i);
}
for(let i = 2; i < 100; i = i * 2) {
console.log(i);
}
- Write a function called
squares
to print the numbers from 1 to 10 and their squares:
1 1
2 4
3 9
...
10 100
- Write a function called
star_triangle
that generates a string that looks like the star triangle below, and returns it to the caller. The function should take one argument, which is the number of lines that the triangle should include (so star_triangle(3) would return a triangle with 1 star, 2 stars, and finally 3 stars).
*
**
***
****
*****
******
*******
********
*********
**********
Don't use ten printf statements; use two nested loops instead. You'll have to use braces around the body of the outer loop if it contains multiple statements:
for(let i = 1; i <= 10; i = i + 1) {
/* multiple statements */
/* can go in here */
}
(Hint: a string you hand to printf does not have to contain the newline character \n.)