Code Monkey home page Code Monkey logo

twilio-javascript-folder's People

Contributors

bahburs avatar tanjinprity4 avatar

Watchers

 avatar

twilio-javascript-folder's Issues

Week 2

Roman Calculator

For this assignment, you will follow the instructions and complete a task showing your knowledge of the subject at the end. If at any moment you need help, feel free to contact your TAs.

You're a time traveller who is about to go back in time and want to build a bunch of cool devices for the people back in the day. You decide to build a basic calculator since math is the coolest subject in the world and you want to help the Romans become really efficient in their calculations.

โœˆ๏ธ Phase 1

Create a file called romancalc.js

Collecting user input from command line in JS

Create a function called get_args that, quite literally, gets arguments from the user and returns the arguments. The user will provide these arguments in the command line when they run their code: node romancalc.js 5 x 4. For the first phase, we will only focus on getting and storing those arguments for future use.

Knowing that functions can only return one element, you want to store these arguments in an array. You can then return the array so that other functions calling get_args can have access to the the arguments.

๐Ÿ“ Commenting your Code

Use in line comments to explain how your code works. Commenting your code helps ensure that you understand what is happening, and helps the code reviewer read through your code easily. For example:

Great example:

var i; // Initialize a variable
for (i = 0; i < nums.length; i++) { // Initialize a for loop that iterates from 0 to length of the nums array
  nums[i]+5; // At every iteration, add the i-th integer in the nums array to the text variable
} // End of loop

When you start writing more code (200+ lines) you will want to do the next example. For this course, your code should not extend past 50-100 lines and we want to use your comments to see how well you understand the concepts and language, so it's better to use the previous example.

Okay example:

// This code uses a for loop to iterate through the entire nums array and add 5 to each element
var i;
for (i = 0; i < nums.length; i++) {
  nums[i]+5;
}

Don't do this:

// Add 5 to all values in nums array
var i;
for (i = 0; i < nums.length; i++) {
  nums[i]+5;
}

๐Ÿš— Running your Code

  1. Save your file
  2. Make sure you're in the same directory in which you saved your romancalc.js file
  3. To run your fancy new program, type: node romancalc.js in terminal with any additional arguments

โœ๏ธ Testing

  1. Add let op = get_args(); to the end of your code to check that the get_args function returns an array
  2. Add the following to confirm if your array contains the right arguments
  • console.log("The first operand is: ", op[0]);
  • console.log("The second operand is: ", op[2]);
  • console.log("The operator is: ", op[1]);

Run node romancalc.js a b c

Test Case 1:

Input: node romancalc.js 4.4 / 4

Output:

The first operand is:  4.4
The second operand is:  4
The operator is:  /

Test Case 2:

Input: node romancalc.js 2 r to

Output:

The first operand is:  2
The second operand is:  to
The operator is:  r

โœ… Submit

Task 1: Complete a function that gets and stores user input!

Commit a file called romancalc.js

Week 1

FizzBuzz In SPACE

For this assignment, you will follow the instructions and complete a task showing your knowledge of the subject at the end. If at any moment you need help, feel free to contact your TAs.

โœˆ๏ธ Starting off

Create a file called fizzbuzz.js

FizzBuzz

FizzBuzz is a program used to demonstrate basic functionality of a language, often when switching between programing languages or as an introduction (that's us!) to first learning a language.

The goals of the program is to go through the numbers from 1 to 100 and print "fizz" whenever a number is divisible by 3, print "buzz" when it's divisible by 5, print "fizzbuzz" when it's divisible by both 3 and 5, and print the number when it's not divisible by either. For our program, you will do something very similar:

For the numbers from 1 to 100:

  • print ๐Ÿ‘ฝ and the number if it is divisible by 3
  • print ๐Ÿš€ and the number if it is divisible by 5
  • print ๐Ÿ›ฐ and the number if it is divisible by both 3 and 5
  • print ๐Ÿ’ฉ and the number if it is not divisible by either 3 or 5

Tip: copy paste the emojis to get it into your code!

Help:

๐Ÿ“ Commenting your Code

Use in line comments to explain how your code works. Commenting your code helps ensure that you understand what is happening, and helps the code reviewer read through your code easily. For example:

Great example:

var i; // Initialize a variable
for (i = 0; i < nums.length; i++) { // Initialize a for loop that iterates from 0 to length of the nums array
  nums[i]+5; // At every iteration, add the i-th integer in the nums array to the text variable
} // End of loop

When you start writing more code (200+ lines) you will want to do the next example. For this course, your code should not extend past 50-100 lines and we want to use your comments to see how well you understand the concepts and language, so it's better to use the previous example.

Okay example:

// This code uses a for loop to iterate through the entire nums array and add 5 to each element
var i;
for (i = 0; i < nums.length; i++) {
  nums[i]+5;
}

Don't do this:

// Add 5 to all values in nums array
var i;
for (i = 0; i < nums.length; i++) {
  nums[i]+5;
}

๐Ÿš— Running your Code

  1. Save your file
  2. Make sure you're in the same directory in which you saved your fizzbuzz.js file

In this program, you need to allow your program to take a second argument, so:
3. To run your fancy new program, type: node fizzbuzz.js with an integer between 1-100 in terminal

โœ๏ธ Testing

To test your code, run it with different integers in the program argument to see what happens.

Test Case 1:

If you run node fizzbuzz.js 5 the output should be:

๐Ÿ’ฉ 1
๐Ÿ’ฉ 2
๐Ÿ‘ฝ 3
๐Ÿ’ฉ 4
๐Ÿš€ 5

Test Case 2:

If you run node fizzbuzz.js 17 the output should be:

๐Ÿ’ฉ 1
๐Ÿ’ฉ 2
๐Ÿ‘ฝ 3
๐Ÿ’ฉ 4
๐Ÿš€ 5
๐Ÿ‘ฝ 6
๐Ÿ’ฉ 7
๐Ÿ’ฉ 8
๐Ÿ‘ฝ 9
๐Ÿš€ 10
๐Ÿ’ฉ 11
๐Ÿ‘ฝ 12
๐Ÿ’ฉ 13
๐Ÿ’ฉ 14
๐Ÿ›ฐ 15
๐Ÿ’ฉ 16
๐Ÿ’ฉ 17

Output example:

โœ… Submit

Task 1: Complete the FizzBuzz program as described above

Commit a file called fizzbuzz.js

Good job, you're fizzing and buzzing in space!

Week 3

Introduction to Recursion

This assignment will demonstrate the basics of recursion, and ask you to create a recursive function of your own. If at any moment you need help, feel free to contact your TAs.

In English, the term "recursive" means something is "characterized by recurrence or repetition." Many natural processes in the world are recursive. As an example, start with an equilateral triangle, and replace the middle 1/3rd of each side by another equilateral triangle. Continue this process over and over until you begin to see something like a snowflake. This is called a Koch snowflake.

Koch snowflake

Recursion is the process in which a function calls itself directly or indirectly. A function is said to be recursive if it calls itself. Let's look at an example.

function HelloWorld(count) {
   if (count < 1) return
   print("Hello World!")
   HelloWorld(count - 1)
}

Let's trace through an example of calling this function. We'll call HelloWorld(5).

HelloWorld(5)
// count of 5 is not less than 1. Print Hello World. Call HelloWorld(4).
// count of 4 is not less than 1. Print Hello World. Call HelloWorld(3).
// count of 3 is not less than 1. Print Hello World. Call HelloWorld(2).
// count of 2 is not less than 1. Print Hello World. Call HelloWorld(1).
// count of 1 is not less than 1. Print Hello World. Call HelloWorld(0).
// count of 0 IS less than 1. Return.

Notice how the command print("Hello World!") is run 5 times in total throughout this recursive call. Thus, our output should look something like this:

Hello World!
Hello World!
Hello World!
Hello World!
Hello World!

You may wonder why we don't simply use a for loop, in which case your argument would be completely valid! However, in this case, we used a relatively simple function in order to demonstrate how recursion works. Where recursion truly shines is in the more complex problems.

Let's take a look at the Fibonacci numbers. In mathematics, the Fibonacci numbers form a sequence called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. What's cool is that the Fibonacci sequence uses zero-based indexing, which lends itself very well to most programming languages. Here is the beginning of the Fibonacci sequence:

fibonacci sequence

How can we use code to find the nth Fibonacci number? For example, we would input the number 3 and the program should output 2, the 3rd Fibonacci number (remember that the Fibonacci sequence uses zero-based indexing!). It turns out that recursion lends itself very well to this particular problem.

function findFibonacci(n) {
    if (n < 2) return n
    return findFibonacci(n-1) + findFibonacci(n-2)
}

Let's trace through the code using an example of findFibonacci(3).

findFibonacci(3)
// n of 3 is not less than 2. return findFibonacci(2) + findFibonacci(1)
  // findFibonacci(2): n of 2 is not less than 2. return findFibonacci(1) + findFibonacci(0)
    // findFibonacci(1): n of 1 IS less than 2. return n (which is 1)
    // findFibonacci(0): n of 0 IS less than 2. return n (which is 0)
    // Thus, findFibonacci(1) + findFibonacci(0) = 1 + 0 = 1 
  // findFibonacci(1): n of 1 IS less than 2. return n (which is 1)
  // Thus, findFibonacci(2) + findFibonacci(1) = 1 + 1 = 2
Output: 2

Take some time to run through the process above and make sure it makes sense. There is something we should take care to note: notice how we run the function findFibonacci(1) twice! This actually becomes incredibly burdensome once we use larger numbers, and we can end up running the same functions hundreds, even thousands of times - and there is most definitely a faster way to compute the Fibonacci numbers. However, this is beyond the scope of the course, and we utilized this function just to show an example of what recursion can look like.

Pay attention to how our recursive functions have been structured so far. We first always identify a base case such that our recursive calls will always be stopped at some point. In findFibonacci(n), our base case was any n less than 2, to mark the first two numbers of the Fibonacci sequence: 0 and 1. In Hello World, our base case was count < 1 i.e. whenever our count got down to 0. This makes sense because once our count comes down to zero, we don't want to print any more Hello World!s. Thus, our first recommendation for writing recursive functions is to always identify the base case first!

base case

Your next step should then to make the recursive calls. This skill can only be polished with more practice, so don't worry if you don't get it the first time. Our best advice here is to trust that your code works.

trust and believe

Hopefully you now have a sense of what recursion is, and a vague idea of how to construct a recursive function. Don't worry if you're struggling a little bit with thinking recursively - it will start coming naturally with more and more practice. If you're looking to try out a few more problems by yourself, this is a great website with a list of incrementally harder recursion problems to solve! Although it only compiles Java (not JavaScript), you can use Node to run your code locally on your machine instead. Moreover, in the website, you can type a return statement in the given code box and click the Go button to find many more test cases to ensure your program runs correctly.

๐Ÿš— Challenge

To complete this assignment, create a recursive function that returns the factorial of an integer n (without using a for loop!). Recall that the factorial of a number n is denoted by n!, where:

factorial definition

For example, 5! = 5 x 4 x 3 x 2 x 1 = 120.

Hint: Identify a base case first! Then move onto the recursive call. Please contact your TA if you need any help at all!

โœ๏ธ Testing

Once you have your function set up, it's time to test it out! Here are a few test cases:

Function Call Output
factorial(1) 1
factorial(2) 2
factorial(3) 6
factorial(4) 24
factorial(5) 120
factorial(6) 720
factorial(7) 5040
factorial(8) 40320
factorial(12) 479001600

Here is a list of 100 factorials in case you'd like to check more.

โœ… Submit

Commit a file called factorial.js with your recursive factorial function!

Week 0

Welcome to Bit Project's Github Learning Lab on JavaScript.

This Learning Lab is going to teach you some of the fundamental coding skills you will need to start building awesome Javascript projects ๐Ÿš€

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.