ameerrah9 / twilio-javascript-folder

https://lab.github.com/Bahburs/learning-the-basics-of-javascript-(bit-project-+-twilio)
0 stars 0 forks source link

Week 2 #4

Open github-learning-lab[bot] opened 3 years ago

github-learning-lab[bot] commented 3 years ago

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.

:airplane: 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.

:pencil: 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;
}

:red_car: 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

:pencil2: 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