Question

Exercise 2

Using javascript (visual studio code)

(a) Write code that creates 5 item objects (records)

(b) Place the five item objects into an array

(c) Write a function that prints an array of your items it has one parameter an array of items

(d) Write a function that counts items that match some attribute value.

(e) Write a function that filters an array of items. It has three parameter an array of item, an attribute name and a value. It return a new array that contains a ‘shallow copy’ of the relevant items. (f) Write code that stores an item array as JSON text

(g) Write code that loads an item array from JSON text

(h) Write a function that filters an array of item, but this time it has two parameters, an array of items and a function f that stores the filter condition. Write test data that contains several test cases. For your test data use both traditional functions and arrow-functions (lambdas).

Exercise 2 (a) Write code that creates 5 item objects (records) (b) Place the five item objects into an array (c) Write a fun

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

Hi,

As HOMEWORKLIB expert guidelines I can choose to answer first 4 subparts of the question.

Here I have answered all expect last subpart.

Please find the script along with description below.

//Write code that creates 5 item objects (records)

//and Place the five item objects into an array

// I have taken name and age as properties of the object.

let Items = [

{

name: "AMRUTH",

age: 5

},

{

name: "raksha",

age: 10

},

{

name: "bandit",

age: 2

},

{

name: "queen",

age: 13

},

{

name: "raaku",

age: 15

}

];

//Write a function that prints an array of your items it has one parameter an array of items

function listItems(items)

{

let items_names = [];

for (let i=0; i<items.length; i+=1)

{

items_names.push(items[i].name);

}

return items_names;

}

//Write a function that counts items that match some attribute value.

function countNoOfItemswithID(age)

{

var count = 0;

for(var i = 0; i < Items.length; ++i)

{

if(Items[i].age == age)

count++;

}

console.log('number of the found elements: ' + count);

}

//Write a function that filters an array of items. It has three parameter an array of item, an attribute name and a value. It return a new array

//that contains a ‘shallow copy’ of the relevant items.

function getCopiedObject(items,name, age)

{

this.name = name;

this.age = age;

this.printItemInfo = function()

{

console.log(this.name);

console.log(this.age);

}

}

//Write code that stores an item array as JSON text

function convertToJsonText()

{

var jsonText = "JSON_TEXT = '"+JSON.stringify(Items)+"'";

console.log(jsonText);

}

//Write code that loads an item array from JSON text

function convertJsonTextToObject()

{

var jsonObject = "Array = [" +Items+"]";

jsonObject = "JSON Object = "+JSON.stringify(Object.assign({}, Items));

console.log(jsonObject);

}

console.log(listItems(Items));

console.log(countNoOfItemswithID(2));

//this is not logged sure it will print as undefined in logs.

var copiedObject = new getCopiedObject(Items,"AMRUTH", 5);

copiedObject.printItemInfo();

convertToJsonText();

convertJsonTextToObject();

output:

kindly upvote

Add a comment
Know the answer?
Add Answer to:
Exercise 2 Using javascript (visual studio code) (a) Write code that creates 5 item objects (records)...
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
  • Javascript each item must have at least three categorical attributes and at least one numeric attribute....

    Javascript each item must have at least three categorical attributes and at least one numeric attribute. Attributes such as ID, name etc do not count as categorical or numeric attributes. Exercise 1 (a) Define a class for your item that meets the above three categorical and one numeric attribute requirements. (b) Create a text file that contains at least 5 such records in CSV format. (c) Create a HTML page that contains a table element the shows one item record...

  • Please write below code in C++ using Visual Studio. Write program that uses a class template...

    Please write below code in C++ using Visual Studio. Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) • Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 • Hint: Use vectors and vector functions to store the set of items 2. Get the number of items in the...

  • In C++ Write a header cashRegister.h and source cashRegister.cpp files for the a CashRegister class. The...

    In C++ Write a header cashRegister.h and source cashRegister.cpp files for the a CashRegister class. The class a CashRegister class has the following data members: 1) an array of 100 Item objects. 2) Cash Register Name and 3) Count of Item objects purchased. 4) Item Cash Total 5) State Tax Rate. The Item is represented as a class in an item.h file. The Item class has the following data members: 1) Name of the item 2) Cost of the item....

  • c++ Write a C++ program using the struct keyword to help a local restaurant automate its...

    c++ Write a C++ program using the struct keyword to help a local restaurant automate its breakfast billing system The program should do the following: a. Show the customer the different breakfast items offered by the restaurant. b. Allow the customer to select more than one item from the menu. c. Calculate and print the bill. Assume that the restaurant offers the following breakfast items (the price of each item is shown to the right of the item) Menu Plain...

  • Building Each Exercise Driver Download the drivers (ExerciseX.java) and start by reading the code. Notice in...

    Building Each Exercise Driver Download the drivers (ExerciseX.java) and start by reading the code. Notice in Exercise1.java’s main how each method for the problems below have already been invoked for you (Skeleton Level: Strong). As you complete additional exercises, notice the comments in each file. As exercises progress, you’ll be required to uncomment lines in main or even build parts of the main driver yourself. We’ll start with skeleton files that are almost complete, and then look at drivers where...

  • Write a modularized, menu-driven program to read a file with unknown number of records.

    ==============C++ or java================Write a modularized, menu-driven program to read a file with unknown number of records.Create a class Records to store the following data: first and last name, GPA , an Id number, and an emailInput file has unknown number of records; one record per line in the following order: first and last names, GPA , an Id number, and emailAll fields in the input file are separated by a tab (‘\t’) or a blank space (up to you)No error...

  • Data Structures and Algorithms. (C++ Language) 1. Write the definition code for a function that p...

    Data Structures and Algorithms. (C++ Language) 1. Write the definition code for a function that passes in a stack and returns (using a return statement) the number of items in the stack (the stack size). a. assume the this function is to be toolkit function in the implementation of the ADT stack. b. assume that this function is not a toolkit function. 2. Given the declaration: s = stack i = item struct STACK { INFO_RC i; int top; }...

  • In C++ Programming Write a program in Restaurant.cpp to help a local restaurant automate its breakfast...

    In C++ Programming Write a program in Restaurant.cpp to help a local restaurant automate its breakfast billing system. The program should do the following: Show the customer the different breakfast items offered by the restaurant. Allow the customer to select more than one item from the menu. Calculate and print the bill. Assume that the restaurant offers the following breakfast items (the price of each item is shown to the right of the item): Name Price Egg (cooked to order)...

  • IN C# Write a program to help a local restaurant automate its lunch billing system. The...

    IN C# Write a program to help a local restaurant automate its lunch billing system. The program should do the following: Show the customer the different lunch items offered by the restaurant. Allow the customer to select more than one item from the menu. Calculate and print the bill. Assume that the restaurant offers the following lunch items (the price of each item is shown to the right of the item): Ham and Cheese Sandwich $5.00 Tuna Sandwich $6.00 Soup...

  • write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy....

    write C code that uses pointers, arrays, and C strings. 3. Write a function called pow_xy. The function should be passed 2 parameters, as illustrated in the prototype below. int pow_xy(int *xptr, int y); Assuming that xptr contains the address of variable x, pow_xy should compute x to the y power, and store the result as the new value of x. The function should also return the result. Do not use the built-in C function pow. For the remaining problems,...

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