Question

In: Computer Science

Using JavaScript for the following assignment Design and code a function named "intersection" that takes two...

Using JavaScript for the following assignment

Design and code a function named "intersection" that

takes two arrays of numbers in parameter and returns a new

array containing only the values that are both in the

two arrays. A value must not be found more than once in

the resulting array but can be found more than once in

the arrays in parameter. The order of the elements in the resulting array

must respect the order of the first parameter. For example :

      intersection ([3, 1, 4, 1, 5], [2, 4, 4, 3])

must return [3, 4].

You also need to design and code a unit test function and

call it. To test that the result of

      intersection ([3, 1, 4, 1, 5], [2, 4, 4, 3])

which returns [3, 4], you can use a test like this:

      assert (intersection ([3, 1, 4, 1, 5], [2, 4, 4, 3]) == "3,4");

This works because the == operator does the automatic conversion

from arrays to text (if one of the two operands are a text).

Solutions

Expert Solution

Code:

<!DOCTYPE html>
<html>
<head>
<script>
function intersection(arr1,arr2) //function which is taking two arrays as input
{
var result=[]; //result array for storing resultant elements
           var k=0;
for (var i = 0; i < arr1.length; i++) //iterate loop from 0 to length of array1
{
for (var z = 0; z < arr2.length; z++) //iterate loop from 0 to length of array2
{
if ((arr1[i]==arr2[z])) //if elements are equal
{
                       if (result.includes(arr1[i])==true) // if the elements are already present in result break the loop
                           break;
                       else
                           {
                           result[k]=arr1[i]; //otherwise put element in result array
                           k=k+1; //increment k value
                           }
}
}
}
for (i=0;i<result.length;i++)
               document.writeln(result[i]); //print the result list in page
           return result; //return the result array
}
var arr1 = [3, 1, 4, 1, 5];
var arr2 = [2, 4, 4, 3]; //declaration of two arrays
var res=intersection (arr1,arr2); // calling intersection() function and storing returned result in res
       console.assert (res == "3,4",'Intersection Elements are wrong'); //finally writing assert() to check intersection elements is correct or not.
</script>
</head>
<body>

</body>
</html>

Code and Output Screenshots:

Note: assert gives error when expression is false so when the returned elements is 3,4 it will not print anything

in second code we are writing assert(res==3,1) it is false so it prints intersection elements are wrong.

Note: if you have any queries please post a comment thanks a lot..always available to help you...


Related Solutions

Write a javascript code to Create a function called Hotel that takes Room no, Customer name....
Write a javascript code to Create a function called Hotel that takes Room no, Customer name. amount paid. Write a code to call hotel function for each customer and display details of customers lodging in rooms with even room numbers. I need only js and html code. no css pls take screenshot of output , else I might dislike thanks
JAVASCRIPT: /* Assignment 03: Complete this javascript */ // 1) Declare a variable named myValue //...
JAVASCRIPT: /* Assignment 03: Complete this javascript */ // 1) Declare a variable named myValue // Assign myValue the value of "Hello, how's it going?" // Print the value of myValue to the console // 2) Use the charAt() method to display the letter t // from the variable myValue in the console // 3) Use the indexOf() method to display the position of "going" // from the variable myValue in the console // 4) Use the slice() method to...
(Javascript) Modify the JavaScript file to implement a function named calculateTotalPrice. At the bottom of the...
(Javascript) Modify the JavaScript file to implement a function named calculateTotalPrice. At the bottom of the file are sample inputs and outputs to test your implementation. /* * The price per ticket depends on the number of tickets * purchased, there are 4 ticket pricing tiers. Given the * number of tickets return the total price, formatted to * exactly two decimal places with a leading dollar sign. * Tier 1: *   Minimum number of tickets: 1 *   Price per...
JavaScript Write a function called "first" that takes in two arguments - the first is an...
JavaScript Write a function called "first" that takes in two arguments - the first is an argument called arr that is an array of numbers, the second is an optional number argument called num(hint: you'll need a default value - look back at the slides). If "num" was not passed in (since it's optional), the "first" function will return an array containing the first item of the array. If a value for "num" was given, the "first" function will return...
Write a JavaScript program with a function named fives that reads two numbers from two text...
Write a JavaScript program with a function named fives that reads two numbers from two text fields and then outputs to a div "True" if both of the numbers are greater than 5 or if their sum is greater than 20. Otherwise your function should output "False" to the div. If you wish, you may use the following HTML code to begin your program.
Define a JavaScript function named prepend that has two parameters. The first parameter is an array...
Define a JavaScript function named prepend that has two parameters. The first parameter is an array of Strings. The second parameter is a String. Your function should use the accumulator pattern to return a newly created array whose elements are the concatenation of the second parameter with the element at the same index in the first parameter.
USING JAVASCRIPT Create a file name dayOfWeek.js and write an arrow function named dayOfWeek that accepts...
USING JAVASCRIPT Create a file name dayOfWeek.js and write an arrow function named dayOfWeek that accepts a Date object dateStr and returns a string that is the day of the week in English form (i.e. “Sunday”, “Monday”, etc.). Test your function by creating a date object that is a significant date to you (such as your birthday) and passing that date object to your function. Test your function at least twice with two different dates. Submit the dayOfWeek.js file to...
I need to have someone design a website using Code one JavaScript server application using Node.js...
I need to have someone design a website using Code one JavaScript server application using Node.js for routing, Express.jsfor framework and Handlebars.js for templating Necessary npm modules are: express, express-handlebars, path and body-parser - it willl have a drop down box with 5 names of titles of a picture. - highlite 1 name in dropdown box and there is a select button to make web site go to that picture stored on server.. this is my third question on this...
(Python) a) Using the the code below write a function that takes the list xs as...
(Python) a) Using the the code below write a function that takes the list xs as input, divides it into nss = ns/nrs chunks (where nrs is an integer input parameter), computes the mean and standard deviation s (square root of the variance) of the numbers in each chunk and saves them in two lists of length nss and return these upon finishing. Hint: list slicing capabilities can be useful in implementing this function. from random import random as rnd...
Write a function named "check_matrix" which takes two matrices as parameters and returns 1 if the...
Write a function named "check_matrix" which takes two matrices as parameters and returns 1 if the matrices are same or 0 otherwise. Set appropriate parameters and return type if necessary.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT