Mid v2
Mid v2
Page 1 of 5
3. React enforces a uni-directional data flow. How, please explain.
In React, unidirectional data flow means that data within a React application flows in one
direction: from parent components (via props) down to child components. Props are immutable
in the child component, meaning the child component cannot modify the data it receives from
the parent. If child component want to modify data it can pass the value to state variable or can
inform the parent through callback function to update the value.
When a child component needs to communicate changes to its parent (for example, user
input), it uses callback functions. These functions are passed down from the parent as props
and allow the child to inform the parent about state changes.
The parent then updates its own state based on the child’s actions, which results in the updated
data being passed down to the child component again.
5. In JavaScript, Set, Array and Map are data types. Write the difference between
them.
Array:- JavaScript array is an object that represents a collection of similar type of elements. It is
a collection of multiple items under a single variable name.
Map:- Map is a collection of elements where each element is stored as a Key, value pair. Map
object can hold objects, functions and primitive values as either key or value. Map always use
the concept of key-value pair.
Set:- A JavaScript Set is a collection of unique values as compared to arrays. You can store any
type of data like Primitive values (number, string, boolean), objects, or functions.
Page 2 of 5
Question # 2: Descriptive (5) Marks
Design this App in class component and write code
for two files - calculator.js, and calculator.css
1. Given an integer array nums sorted in non-decreasing order, remove the duplicates in-
place such that each unique element appears only once. The relative order of the
elements should be kept the same. Then return the number of unique elements in
nums.
Example:-
Input: nums = [1,1,2]
Output: nums = [1,2]
Solution
let nums = [1,1,2];
let set = new Set(nums);
set.forEach(element => {
console.log(element);
});
Or
let setArray = [...set];
console.log(setArray)
//Output:
Page 3 of 5
2. Merge the two lists into one sorted list. The list should be made by splicing together the
nodes of the first two lists.
Example:-
Input: list1 = [1,2,4], list2 = [1,3,4]
Output: [1,1,2,3,4,4]
Solution
let arr1 = [1,2,3,4,5]
let arr2 = [2,4,5,6,7,8,9]
let sortArray = [...arr1, ...arr2];
console.log(sortArray.sort());
//Output: [1, 2, 2, 3, 4, 4, 5, 5, 6, 7, 8, 9]
]
Or
arr1.splice(arr1.length, 0, …arr2);
or
let combineArrays = arr1.concat(arr2) //concat does not update, it return
//new array with combining both arrays.
3. Given two strings needle and haystack, return the index of the first occurrence of needle
in haystack, or -1 if needle is not part of haystack.
Example:-
Input: str1 = "sadbutsad", str2 = "but"
Output: 3
Solution
let str = 'sadbutsad';
let substring = 'but';
let index = str.indexOf(substring);
console.log(index);
// Output: 3
4. Take an array of integers nums sorted in non-decreasing order, find the starting and
ending position of a given target value.
If target is not found in the array, return [-1, -1].
Example 1:
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
Solution-1
let arr = [5, 7, 7, 8, 8, 10];
// Finding the first occurrence of 8
let firstIndex = arr.indexOf(8);
console.log("First occurrence of 8:", firstIndex); // Output: 3
// Finding the last occurrence of 8
let lastIndex = arr.lastIndexOf(8);
Page 4 of 5
console.log("Last occurrence of 8:", lastIndex); // Output: 4
Second Solution
let arr1 = [1,2,8,3,4,8,2,3,8,9]
let val = 8
let indexArray = [];
let index = 0;
arr1.forEach(element => {
if(element === val){
indexArray.push(index);
}
index++;
});
console.log(indexArray);
Page 5 of 5