LOGOCOMP1531 - Practice Exam

Q7. Objects (15 Marks)

Complete the functions in q7/objects.js based on the interface below.

Interface

FunctionDescriptionParametersErrorsSuccess
isSameLengthThe function should take in two arrays and returns true if they have the same length, and false otherwise.
  • a: list of any type,
  • b: list of any type
N/A
  • true: if same length
  • false: if different length
zipTakes in two arrays and combines them together index-wise into a list of lists.
  • a: list of any type,
  • b: list of any type
  • Error: throw exception if the lists are not of the same length

A list of inner lists, where each inner list pairs the input lists index pair-wise.

a = ['a', 'b', 'b']
b = [1, 2, 3]
> zip(a, b)
[['a', 1], ['b', 2], ['b', 3]]
constructObjectTakes two arrays and returns an object composed of keys from the first array and values from the second.
  • keys: list of keys to be in the object. Keys are guaranteed to be string-able.
  • values: list of values to be in the object. Can be of any type.
  • Error: throw exception if the lists are not of the same length

If the lists are of the same length:

  a1 = ['a', 'b', 'c']
  a2 = [1, 2, 3]

  > constructObject(a1, a2)
  {'a': 1, 'b': 2, 'c': 3}

If a key appears more than once in the first array, the LAST matching value from the second list is used:

> constructObject(['a', 'b', 'b', 'c'], [1, 2, 3, 4])
{'a': 1, 'b': 3, 'c': 4}

Marking

When you think your program is working, you can run some simple automated tests:

  • tests: npm run test

Submission

The submission will only collect the q7/objects.js file. All other files are replaced in automarking.

When you've finished working, submit your answer by running:

submit --question 7

If no submissions have been made for a question, we will take the saved file/s from your working directory.