Question

In: Computer Science

JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must...

JavaScript - Create a class using "names" as the identifier.

Create a constructor. The constructor must have elements as follow:

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

The constructor will assign the values for the three elements and should use the "this" keyword

Create a function, using "printObject" as the identifier

printObject:

This function will have three input parameters:

allNames , sortType, message

Will sort the array of objects (allNames) using sortType as the key to how the arrays objects will be sorted.

These sort functions, are higher order functions, one required for each sort option except none. A higher order function type is calling a function with an argument that is a function. In this assignment, one would be using the builtin sort for arrays, allNames.sort( your function here ); This function will override the default sort order, built into array.sort()


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

Will print the message to the console


Will use a loop to print out the array by index ascending and display the values located in the elements: first last age, for each index accessed

Create a function main, 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 ( data is of your choosing, 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

Note: It is required to use the ES6 style of keywords => instead of the older function and for variables use the keyword let, not var, class and within the constructor, not function.

Solutions

Expert Solution

class Names {

  constructor(first, last, age) {

    this.first = first;

    this.last = last;

    this.age = age;

  }

}

function printObject(allNames, sortType, message) {

  if (sortType == "first") {

    allNames.sort(function(a, b) {

      return a.first.localeCompare(b.first);

    });

  }

  if (sortType == "last") {

    allNames.sort(function(a, b) {

      return a.last.localeCompare(b.last);

    });

  }

  if (sortType == "age") {

    allNames.sort(function(a, b) {

      return a.age - b.age;

    });

  }

  for (let i = 0; i < allNames.length; i++) {

    console.log(message);

    console.log(

      allNames[i].first + " " + allNames[i].last + " " + allNames[i].age

    );

  }

}

function main() {

  var allNames = [];

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

  allNames.push(new Names("Jhon", "Doe", 50));

  allNames.push(new Names("Liza", "Helen", 70));

  allNames.push(new Names("Anthony", "Jhonson", 30));

  allNames.push(new Names("Zakir", "Nayak", 25));

  printObject(allNames, "age", "Message");

}

main();


Related Solutions

This is for Javascript programming language: A. Class and Constructor Creation Book Class Create a constructor...
This is for Javascript programming language: A. Class and Constructor Creation Book Class Create a constructor function or ES6 class for a Book object. The Book object should store the following data in attributes: title and author. Library Class Create a constructor function or ES6 class for a Library object that will be used with the Book class. The Library object should be able to internally keep an array of Book objects. B. Methods to add Library Class The class...
Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that...
Using Java: Create a class called MyNumber with an integer private attribute. Create a constructor that defines an integer parameter to set the private integer attribute. Create a setter that validates the attribute does not accept a value lower than 2 or the method will throw a IllegalArgumetException. Create a getter to return the private integer attribute value. Define a public method that is called isPrime() that returns a boolean and implements the Sieve of Eratosthenes method. Define a public...
JAVASCRIPT: - Please create an object (grade) with 10 names and 10 grades. - Create a...
JAVASCRIPT: - Please create an object (grade) with 10 names and 10 grades. - Create a method (inputGrade) that can put a name and a grade to the grade object. - Create another method (showAlltheGrades) to show all the grade in that object. - Create the third method (MaxGrade) that can display the maximum grade and the student name. - Using “prompt” and inputGrade method input 10 student names and their grades. - Display all the grades and names by...
JAVASCRIPT: - Please create an object (grade) with 10 names and 10 grades. - Create a...
JAVASCRIPT: - Please create an object (grade) with 10 names and 10 grades. - Create a method (inputGrade) that can put a name and a grade to the grade object. - Create another method (showAlltheGrades) to show all the grade in that object. - Create the third method (MaxGrade) that can display the maximum grade and the student name. - Using “prompt” and inputGrade method input 10 student names and their grades. - Display all the grades and names by...
JAVASCRIPT: - Please create an object (grade) with 10 names and 10 grades. - Create a...
JAVASCRIPT: - Please create an object (grade) with 10 names and 10 grades. - Create a method (inputGrade) that can put a name and a grade to the grade object. - Create another method (showAlltheGrades) to show all the grade in that object. - Create the third method (MaxGrade) that can display the maximum grade and the student name. - Using “prompt” and inputGrade method input 10 student names and their grades. - Display all the grades and names by...
Using C++, Write a class KeywordsInFile. Create KeywordsInFile.h and KeywordsInFile.cpp. The only required constructor for this...
Using C++, Write a class KeywordsInFile. Create KeywordsInFile.h and KeywordsInFile.cpp. The only required constructor for this class is KeywordsInFile( filename_with_keywords, filename_with_text) filename_with_keywords is a name of a plain text file that contains the list of keywords. For the future reference, the number of words in this file is denoted by N. filename_with_text is a name of a plain text file that contains the text where the keywords must be found. For the future reference, the number of words in this...
JAVASCRIPT: Please create an array of student names and another array of student grades. - Create...
JAVASCRIPT: Please create an array of student names and another array of student grades. - Create a function that can put a name and a grade to the arrays. - Keep Read student name and grade until student name is “???”. And save the reading by using a function - Create another function to show all the grade in that object. - Create the third function that can display the maximum grade and the student’s name. - Create a sorting...
1. From the constructor, you infer that the name of the class containing the constructor must be_______ .
1. From the constructor, you infer that the name of the class containing the constructor must be_______ .2. Fill in the blanks so that the statement will instantiate a JButton object and assigns it to variable of JButton type:JButton btn= _______ ("OK");3. Fill in the blanks to complete the description of the constructor in the corresponding UML class diagram:+<>______( _____ : ______)
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at...
Class Exercise: Constructor using JAVA Let’s define a Class together and have a constructor while at it. - What should the Class object represent? (What is the “real life object” to represent)? - What properties should it have? (let’s hold off on the methods/actions for now – unless necessary for the constructor) - What should happen when a new instance of the Class is created? - Question: What are you allowed to do in the constructor? - Let’s test this...
in java Create a class City with x and y as the class variables. The constructor...
in java Create a class City with x and y as the class variables. The constructor with argument will get x and y and will initialize the city. Add a member function getDistanceFrom() to the class that gets a city as the input and finds the distance between the two cities.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT