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

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...
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...
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...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
Create a Java class with the name (identifier) MyProgram and place it in the Questions package....
Create a Java class with the name (identifier) MyProgram and place it in the Questions package. You will need to create a main method for this class and place all of your code inside of that method. Your program should complete the following steps: - Prompt the user to enter a name for a Student. - Use the Scanner class to read in the user input and store the result in a variable. - Use the Random class to generate...
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using...
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January" through "December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT