Closed github-learning-lab[bot] closed 3 years ago
Done!
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.
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.
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
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;
}
node romancalc.js
in terminal with any additional argumentsconsole.log(calculate());
to the end of your codeRun 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
Phase 2: Complete a calculator that can do basic math equations!
Commit a file called romancalc.js
Built a basic calculator including two more operators and added error management for unknown operators from user. Any suggestion would be helpful to improve the program!
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.
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.
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;
}
node romancalc.js
in terminal with any additional argumentsAdd 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
Phase 3: Complete a function to convert Roman Numerals to Integers
Commit a file called romancalc.js
Completed. I commented out the previous tests for Phase 1 and 2.
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.
Continue working in romancalc.js
Converting from Integers to Roman Numerals
If you get user input in the form of Roman Numerals, you will need to convert them into integers to do the calculations. But once the calculations are done, you will want to convert them back into Roman Numerals so that the user can read them.
Create a function called intToRoman() that takes an integer as input and converts it to Roman Numerals, and returns the converted Roman Numerals.
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;
}
node romancalc.js
in terminal with any additional argumentsAdd console.log(intToRoman("process.argv[2]"));
to the end of your code and run node romancalc.js a
where 'a' is the integer you want to convert
Test Case 1:
Input: node romancalc.js M
Output: NaN (error)
Test Case 1:
Input: node romancalc.js 4
Output: IV
Test Case 1:
Input: ```node romancalc.js 927```
Output: CMXXVII
Phase 4: Complete a function to convert integers to Roman Numerals
Commit a file called romancalc.js
Done!
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.
Continue working in romancalc.js
Before starting, add this function to the top of your code:
function isNumber(n) { return /^-?[\d.]+(?:e-?\d+)?$/.test(n); }
this function returns true (0) for input that is a number and false for anything else, and will help you identify if your user input is an integer or a Roman Numeral.
Putting it all together
Now that you have your functions all set, all you have to do it put them together:
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;
}
node romancalc.js
in terminal with any additional argumentsAdd console.log(intToRoman("process.argv[2]"));
to the end of your code and run node romancalc.js a
where 'a' is the integer you want to convert
Test Case 1:
Input: node romancalc.js 4 + X
Output: 14, XIV
Test Case 1:
Input: node romancalc.js 4732 / 34
Output: 139.1764705882353
(There will be no Roman Numeral for this)
Test Case 1:
Input: ```node romancalc.js CCCXCIII % LXXVII```
Output: 8, VIII
Phase 5: Complete a function to convert integers to Roman Numerals
Commit a file called romancalc.js
Completed
When a section is completed, a new issue will be created with the next step. Click here to start!
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:
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:
Don't do this:
:red_car: Running your Code
node romancalc.js
in terminal with any additional arguments:pencil2: Testing
let op = get_args();
to the end of your code to check that the get_args function returns an arrayconsole.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:
Test Case 2:
Input:
node romancalc.js 2 r to
Output:
✅ Submit
Task 1: Complete a function that gets and stores user input!
Commit a file called romancalc.js