Question
Rewrite the following function so that it takes a single argument, an options Object, with all original arguments as properties: function formatPath(parent = . ,
Rewrite the following function so that it takes a single argument, an options Object, with all original arguments as properties:
function formatPath(parent = ., filename, ext, filesystem = windows) {
const sep = filesystem === windows "\\": "/";
ext = ext.startsWith(".") ? ext : `.${ext}`;
return parent + sep + filename + ext;
}
Your new version of the function should be callable in all of the following ways, and work the same as the original:
formatPath({ parent: home, filename: test, ext: js });
formatPath({ parent: home, filename: test, ext: .js });
formatPath({ filename: test, ext: js });
formatPath({ parent: home, filename: test, ext: js, filesystem: windows });
2.
Write a Constructor function named Distance. A Distances constructor should take a single argument, length, which is a Number of millimeters, and it should store this length on the Object instance being created. If length is missing, use a default of 0 (zero).
The Distance instance should have 2 methods defined on its Prototype:
function cm(raw) {}
function inches(raw) {}
All three work in the same way: if the raw argument is true, return the length converted to the number of cm or inches (i.e., depending on which function is called). If raw is false (not true) or undefined, return a formatted string, which gives the distance length in words. Here are some examples of how it should work:
let d = new Distance(0);
d.cm(true); // should return 0
d.cm(); // should return 0 cm
let d = new Distance(25.4);
d.inches(true); // should return 1
d.inches(); // should return 1 inch, note the singular inch
let d = new Distance(165.1);
d.inches(true); // should return ~6.5
d.inches(); // should return 6.5 inches, note the plural inches
d.cm(true); // should return ~16.51
d.cm(); // should return 16.51 cm
3.
Write a function, addColorPreference, which takes a user argument, an Object, and a colour string argument and returns the user Object with a new Color Preference property. Your function should work like this:
let user = { name: "Yan" };
user = addColorPreference(user, 'red');
The user object should now have a Color Preference property with the value red, that is, the user object would look like this:
{ name: Yan, Color Preference: red}
If the user object already has a Color Preference property, your function should do nothing, and simply return the user object unaltered.
Question 3.
Similar to Question 3 Part 1, write a function, removeAge, which takes a user argument, an Object, and returns the user Object with a the age property removed. Your function should work like this:
let user = { name: "Yan", age: 32 };
// Remove age from the user
user = removeAge(user);
After calling your function, the user object should NOT have an age property:
{ name: Yan }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started