CS 220 Midterm 2, Fall 2020 2. Write functions Success and Failure that meet the same goals of the Success and Failure classes seen in class, but that achieve protecting data members from being accessed in any way other than through member functions. 3. In this exercise you will create a function that returns a JavaScript object used to determine if a person meets certain requirements for being president of the USA. To be eligible for being president of the USA, a person needs to: 1. be at least 35 years old 2. be a US citizen 3. Have lived in the USA for at least 14 years. 4. Not have been president for two or more terms. 3.1 Write a function called requirements that returns an object with the following data members: age, of type number usCitizen, of type Boolean yearsOfResidence, of type number termsAsPresident, of type number. The function takes four arguments. The first argument is a number representing the age of a person. The second argument is a boolean value representing if the person was born in the USA. The third argument represents how many years the person has lived in the USA. The fourth argument indicates how many turns the person has already served as President of the USA. 3.2 Write four member functions as part of the object returned by the function above that check each of the conditions that a person needs to have in order to qualify to serve as President of the USA. Function checkAge() checks that the person is at least 35 years old. Function checkCitizenship() checks that the person is a USA citizen. Function checkResidency() checks the number of years the person has lived in the USA. Function checkTerms() checks that the person has not served two or more terms as USA President. Each of these four functions takes no inputs, and returns either a Success or Failure object of the type seen in class, depending on the condition they are checking being met or not by the values they operate on. If they return Success, they store a value of true in their value field. If they return a Failure object the reason field stores the test that failed. 3.3 Finally, the object also has a function which makes use of the objects returned by the four functions declared above, their then() functions, to output true if the person for whom information is being stored in the object meets all of the requirements to serve as USA President, or false otherwise. Successive function calls should be chained via the then() function of the promises. None of the object data members should be accessible by means other than the member functions. 3.4 Write four test cases for the code you wrote in the problem above. Explain what each of your tests is checking for, and what the expected output is. 3.5 Using the object you developed previously in this exercise, write a function that receives an array of objects like the one you designed, and uses high order functions to compute and return how many of the candidates in the array qualify to be president of the USA. 4. Write a function that takes as parameter an array and returns a list with every other element of the array (i.e., it alternates including and not including an element), so that it ends with the last array element. For implementation, use reduceRight and an accumulator that includes a boolean flag. Do not use other higher-order functions or loops. Lists are defined as follows: 5. Write a function that takes as argument a memoized stream (finite or infinite), and two natural numbers b and p (the latter positive). The function should return the stream of elements of the original stream situated at positions b, b+p, b+2p, b+3p, ... The first element is considered at position 0. 6.1 Write a function that takes a memoized stream (finite or infinite) of numbers and returns the stream of local maxima. A number is a local maximum in a sequence if no larger number is immediately before it or after it. 6.2 Write five tests for this function. Explain what each is testing for and give the expected output. You may simply enumerate stream elements: 1, 2, 4, ... 7. Write a function that takes as parameter a value x and functions c, f, and g. When called with argument x, these functions return Failure, or Success (with numeric values for f and g, and boolean for c). If c(x), f(x) and g(x) are all successful, your function should return Success, with the product of the values for f and g, if c's value is true, and with the sum of f and g's values if c's value is false. Otherwise, your function should return Failure. 8. Indicate which objects are accessible in memory, and how, after executing the following code. Indicate which objects can be garbage collected. let a = [0, 1]; let b = [2, 3]; let c = [a, 1, b]; a = b[0]; b = c[0]; c[1] = c; let d = [4, 5]; d = [d, 6]; d = [d, 7]; d = c[1];
欢迎咨询51作业君