frederick-uy / twilio-javascript-folder

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

Week 2 #3

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

frederick-uy commented 3 years ago

Done!

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 2

Continue working in romancalc.js

Building a basic calculator

Create a function called calculate() that calculates using operands and operand from the user and returns the result.

  1. Initialize variables with the second, third, and fourth arguments from user input. You have already created a function called get_args(), which you can use in calculate to get the operators and operand.
  2. Initialize variable to hold result of operations.
  3. Use conditions to determine what the operation is from the user input and calculate the result:
    • Addition: +
    • Subtraction: -
    • Multiplication: x
    • Division: /
    • Exponents: ^
    • Modulus: %
  4. Return the result (don't print them other than for testing).

Assumptions:

Challenge

This calculator is really simple and doesn't handle error management very well. Try adding some error messages for the user so they know when their input in invalid. Consider what valid input should look like.

Can you think of any more operations you want to implement? Try adding them!

Consider developing your calculator to handle more complicated equations.

Hints

: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 console.log(calculate()); to the end of your code

Run node romancalc.js a b c

Test Cases:

Input: node romancalc.js 98 + 94

Output: 192

Input: node romancalc.js 22 - 95

Output: -73

Input: node romancalc.js 53 x 48

Output: 2544

Input: node romancalc.js 25 / 84

Output: 0.2976190476190476

Input: node romancalc.js 5 ^ 4

Output: 625

Input: node romancalc.js 143 % 7

Output: 3

✅ Submit

Phase 2: Complete a calculator that can do basic math equations!

Commit a file called romancalc.js

frederick-uy commented 3 years ago

done!

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 3

Continue working in romancalc.js

Converting from Roman Numerals to Integers

You have now successfully created a calculator and a way to get the users's input. The issue is that since you're taking this calculator to the Romans, your calculator needs to be able to work with Roman Numerals.

Create a function called romanToInt() that takes a string of characters as input and converts it to an integer, and returns the converted integer.

: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

Add console.log(romanToInt("process.argv[2]")); to the end of your code and run node romancalc.js a where 'a' is the Roman Numeral you want to convert

Test Case 1:

Input: node romancalc.js 4

Output: NaN (error)

Test Case 1:

Input: node romancalc.js IV

Output: 4

Test Case 1:

Input: ```node romancalc.js CMXXVII```

Output: 927

✅ Submit

Phase 3: Complete a function to convert Roman Numerals to Integers

Commit a file called romancalc.js