Question

JavaScript - Sorting Assignment (arrays, classes, functions and higher order functions)

Note: Please make sure to use the ES6 keyword style => instead of the older function. For variables please use keyword let, not var, class and within the constructor, not function.

Instructions:

- Create a new JavaScript file and name it “Objects.js”

- Create a class and make sure to use “names” as the identifier.

- Create a constructor with the following elements:

first ( value passed will be String )
last ( value passed will be String )
age ( value passed will be Numeric )

Note: Constructor will assign values for the three elements and use the keyword “this”.

- Create a function and use “printObject” as the identifier.

The function will have the following three input parameters:

allNames, sortType, message

Note: It will sort the array of objects (allNames) using sortType as the key to how the array objects will be sorted.

The sort options are first , last , age, none.

Will print the message to the console

- Use a loop to print out the array by index ascending and display the values located in the elements: first, last, age for each accessed index (refer to the example on the bottom of the page).

- Create a function main with no input parameters.

main:

Create the empty array using the identifier "allNames"

Using the push() method, load 4 new objects of names to the array allNames, populating the fields first , last , age (for example: allNames.push( new names("Cammo", "Hammer", 39));

- Call printObject, passing the array , sort method as a string ( options are first, last, age, none )

- Last line of the script, would be the call to the main

Example output (all console messages should match):

C:\Users\ Desktop\JavaScript>node Objects.js Objects by Index: Cammo Hammer 39 Zoe Cicco 21 Piper Balmer 49 Jasper Thorn 10 O

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//declare the class

class names {

  //declare the constructor

  constructor(_first, _last, _age) {

    //initialise the attributes of the class

    this.first = _first;

    this.last = _last;

    this.age = _age;

  }

}

let printObject = (allNames, sortType, message) => {

  console.log("\n" + message);

  //if sortType is first

  if (sortType == "first") {

    allNames.sort((a, b) => {

      if (a.first < b.first)

        // ascending

        return -1;

      if (a.first > b.first) return 1;

      return 0;

    });

    //print the objects

    for (name of allNames) {

      console.log(`${name.first} ${name.last} ${name.age}`);

    }

  }

  //if sortType is last

  if (sortType == "last") {

    allNames.sort((a, b) => {

      if (a.last < b.last)

        // ascending

        return -1;

      if (a.last > b.last) return 1;

      return 0;

    });

    //print the objects

    for (name of allNames) {

      console.log(`${name.first} ${name.last} ${name.age}`);

    }

  }

  // sort by age

  if (sortType == "age") {

    allNames.sort((a, b) => {

      return a - b;

    });

    //print the objects

    for (name of allNames) {

      console.log(`${name.first} ${name.last} ${name.age}`);

    }

  }

  // sort type is none

  if (sortType == "none") {

    for (name of allNames) {

      console.log(`${name.first} ${name.last} ${name.age}`);

    }

  }

};

let main = () => {

  //create the array

  let allNames = [];

  //insert the objects

  allNames.push(new names("Cammo", "Hammer", 39));

  allNames.push(new names("Zoe", "Cicco", 21));

  allNames.push(new names("Piper", "Balmer", 49));

  allNames.push(new names("Jasper", "Thorn", 10));

  //call the print function

  printObject(allNames, "none", "Objects by Index:");

  printObject(allNames, "first", "Objects by First Name:");

  printObject(allNames, "last", "Objects by Last Name:");

  printObject(allNames, "age", "Objects by Age:");

};

main();

Objects by Index: Cammo Hammer 39 Zoe Cicco 21 Piper Balmer 49 Jasper Thorn 10 Objects by First Name: Cammo Hammer 39 Jasper

Add a comment
Know the answer?
Add Answer to:
JavaScript - Sorting Assignment (arrays, classes, functions and higher order functions) Note: Please make sure to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • CODE THE FOLLOWING FUNCTIONS IN JAVASCRIPT (USE OF THE ABOVE MENTIONED IN BUILT FUNCTIONS IS NOT...

    CODE THE FOLLOWING FUNCTIONS IN JAVASCRIPT (USE OF THE ABOVE MENTIONED IN BUILT FUNCTIONS IS NOT ALLOWED) Write a function that accepts an array as argument. The function should loop through the array elements and accumulate the sum of ASCII value of each character in element and return the total. For example: function([‘A’, ‘bc’, 12]); // returns 361 which is the sum of 65 + 98 + 99 + 49 + 50 Write a function that accepts two arguments (a...

  • Can someone please help me with this javascript - Javascript - do at least 10 of...

    Can someone please help me with this javascript - Javascript - do at least 10 of the following questions below ----------------------------------------------------------------------------------- Create an HTML file that takes in input and carries out of the following functions and manipulates the DOM to show the outcome. Please put the question itself as a comment above each answer. Use either JavaScript, jQuery, or both (but know the difference!). ----------------------------------------------------------------------------------- 1. Fibonacci Define function: fib(n) Return the nth number in the fibonacci sequence. 2....

  • This project will allow you to practice with one dimensional arrays, function and the same time...

    This project will allow you to practice with one dimensional arrays, function and the same time review the old material. You must submit it on Blackboard and also demonstrate a run to your instructor. General Description: The National Basketball Association (NBA) needs a program to calculate the wins-losses percentages of the teams. Write a complete C++ program including comments to do the following: Create the following: •a string array that holds the names of the teams •an integer array that...

  • Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In...

    Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In this project, we combine the concepts of Recursion and Merge Sorting. Please note that the focus of this project is on Merging and don't forget the following constraint: Programming Steps: 1) Create a class called Art that implements Comparable interface. 2) Read part of the file and use Merge Sort to sort the array of Art and then write them to a file. 3)...

  • SHORT ANSWER QUESTIONS Part 1 Classes Abstraction: What is Abstraction in terms of representation? Specifically what...

    SHORT ANSWER QUESTIONS Part 1 Classes Abstraction: What is Abstraction in terms of representation? Specifically what choices does one make when creating a class that is an example of Abstraction? Encapsulation: What is encapsulation? What is information hiding? What does a public interface to a class consist of (not in the sense of actual java interfaces but what the client uses to manipulate objects of the class) What is an object of a class? What is the constructor? How do...

  • Purpose: Demonstrate the ability to create and manipulate classes, data members, and member functions. This assignment...

    Purpose: Demonstrate the ability to create and manipulate classes, data members, and member functions. This assignment also aims at creating a C++ project to handle multiple files (one header file and two .cpp files) at the same time. Remember to follow documentation and variable name guidelines. Create a C++ project to implement a simplified banking system. Your bank is small, so it can have a maximum of 100 accounts. Use an array of pointers to objects for this. However, your...

  • Problem Set 2: Inheritance and Method Overriding The goal of this problem set is to apply...

    Problem Set 2: Inheritance and Method Overriding The goal of this problem set is to apply inheritance and method overriding in order to create a hierarchy of array sorters. There exist many algorithms to sort arrays. In this problem set, we are especially interested in “in-place” sorting algorithms like Selection Sort and Insertion Sort. In-place sorting refers to an approach in which we perform the sorting steps on the underlying array rather than using extra storage. When using object-oriented design,...

  • code in java please:) Part I Various public transporation can be described as follows: A PublicTransportation...

    code in java please:) Part I Various public transporation can be described as follows: A PublicTransportation class with the following: ticket price (double type) and mumber of stops (int type). A CityBus is a PublicTransportation that in addition has the following: an route number (long type), an begin operation year (int type), a line name (String type), and driver(smame (String type) -A Tram is a CityBus that in addition has the following: maximum speed (int type), which indicates the maximum...

  • ***JAVA: Please make "Thing" movies. Objective In this assignment, you are asked to implement a bag...

    ***JAVA: Please make "Thing" movies. Objective In this assignment, you are asked to implement a bag collection using a linked list and, in order to focus on the linked list implementation details, we will implement the collection to store only one type of object of your choice (i.e., not generic). You can use the object you created for program #2 IMPORTANT: You may not use the LinkedList class from the java library. Instead, you must implement your own linked list...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT