Replacing array Item
Given an array myArray, targetItem and a replace item write a JS program to replace the targetItem with the given replacement in myArray consists of more than one targetItem replace the first occurence.
sample input1
[1,2,3,'four'5,6]
'four'
4
sample output1
[1,2,3,4,5,6]
function readLine() {
return inputString[currentLine++];
}
function main() {
let myArray = JSON.parse(readLine().replace(/'/g, '"'));
let targetItem = JSON.parse(readLine().replace(/'/g, '"'));
let replaceItem = JSON.parse(readLine().replace(/'/g, '"'));
/* Please do not modify anything above this line */
/* Write your code here and log the output */
}
concatenate and remove Duplicates
Given two arrays arr1 and arr2 of positive integers write a JS program to concatenate two rows and remove duplicates from the concatenated array. Log the array with unique items in ascending order
input1
[1,4,7,3,7,3,3]
[2,4,5,5,3,2,1]
output1
[1,2,3,4,5,7]
"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");
let inputString = "";
let currentLine = 0;
process.stdin.on("data", (inputStdin) => {
inputString += inputStdin;
});
process.stdin.on("end", (_) => {
inputString = inputString
.trim()
.split("\n")
.map((str) => str.trim());
main();
});
function readLine() {
return inputString[currentLine++];
}
function main() {
let arr1 = JSON.parse(readLine());
let arr2 = JSON.parse(readLine());
/* Please do not modify anything above this line */
/* Write your code here and log the output */
}
Common arrays in three arrays
given three arrays arr1,arr2,arr3 write a JS program to find the common items in the among three arrays.
sample input1
[1,2,3,'cat']
[1,'cat',4]
[1,'cat,64]
output1
[1,'cat']
sample input2
['cat','rat','dog']
['lion','fox']
['kiwi','eagle']
output2
[]
function readLine() {
return inputString[currentLine++];
}
function main() {
let arr1 = JSON.parse(readLine().replace(/'/g, '"'));
let arr2 = JSON.parse(readLine().replace(/'/g, '"'));
let arr3 = JSON.parse(readLine().replace(/'/g, '"'));
/* Please do not modify anything above this line */
/* Write your code here */
}
Cumulative Sum
given an array integers, write a JS program to get the cumulative sum of the items in the array
sample input1
[1, 10, 100, 1000]
sample output1
[1, 11, 111, 1111]
"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");
let inputString = "";
let currentLine = 0;
process.stdin.on("data", (inputStdin) => {
inputString += inputStdin;
});
process.stdin.on("end", (_) => {
inputString = inputString
.trim()
.split("\n")
.map((str) => str.trim());
main();
});
function readLine() {
return inputString[currentLine++];
}
function main() {
let integers = JSON.parse(readLine());
/* Please do not modify anything above this line */
/* Write your code here */
}
Sports Data
write a JS program to consolidate the data so that each student should participate in only one sport if duplicates entries are found consider the latest entry
input1
[['Arjun','Cricket'],['Ronalodo','Football'],['pradeep','Volleyball']
output1
{Arjun: 'Cricket',Ronaldo:'Football',Pradeep:'Volleyball'}
let inputString = "";
let currentLine = 0;
process.stdin.on("data", (inputStdin) => {
inputString += inputStdin;
});
process.stdin.on("end", (_) => {
inputString = inputString
.trim()
.split("\n")
.map((str) => str.trim());
main();
});
function readLine() {
return inputString[currentLine++];
}
function main() {
let sportsData = JSON.parse(readLine().replace(/'/g, '"'));
/* Please do not modify anything above this line */
/* Write your code here */
}
Date Type Report
given an array myArray write a JS program to find the count of number,object,string,boolean. data values in the array
sample input1
[1, '2', true,{'a':'b'}, false]
sample output1
{number: 1, object:2, string:1, boolean: 2}
"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");
let inputString = "";
let currentLine = 0;
process.stdin.on("data", (inputStdin) => {
inputString += inputStdin;
});
process.stdin.on("end", (_) => {
inputString = inputString
.trim()
.split("\n")
.map((str) => str.trim());
main();
});
function readLine() {
return inputString[currentLine++];
}
function main() {
let myArray = JSON.parse(readLine().replace(/'/g, '"'));
/* Please do not modify anything above this line */
/* Write your code here */
}
Min & Max Values in Array
given an array myArray of integers, write a JS program to find the maximum and minimum values in the array.
input1
[1,4,2,7,9,3,5]
output1
{min: 1, max: 9}
"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");
let inputString = "";
let currentLine = 0;
process.stdin.on("data", (inputStdin) => {
inputString += inputStdin;
});
process.stdin.on("end", (_) => {
inputString = inputString
.trim()
.split("\n")
.map((str) => str.trim());
main();
});
function readLine() {
return inputString[currentLine++];
}
/* Please do not modify anything above this line */
function findMinAndMaxValuesInArray(arr) {
/* Write your code here */
}
/* Please do not modify anything below this line */
function main() {
let myArray = JSON.parse(readLine());
console.log(findMinAndMaxValuesInArray(myArray));
}
The function accepts two positive integers ‘r’ and ‘unit’ and a positive integer array ‘arr’ of size ‘n’ as its argument ‘r’ represents the number of rats present in an area, ‘unit’ is the amount of food each rat consumes and each ith element of array ‘arr’ represents the amount of food present in ‘i+1’ house number, where 0 <= i
Note:
Return -1 if the array is null
Return 0 if the total amount of food from all houses is not sufficient for all the rats.
Computed values lie within the integer range.
Example:
Input:
r: 7
unit: 2
n: 8
arr: 2 8 3 5 7 4 1 2
Output:
4
Explanation:
Total amount of food required for all rats = r * unit
= 7 * 2 = 14.
The amount of food in the 1st 4 houses = 2+8+3+5 = 18. Since, amount of food in 1st 4 houses is sufficient for all the rats. Thus, output is 4.
take a function to check no is prime or not from array and then is number is prime add 1 to element if not then multiply by 2..note: Use arrow function to add or multiply element part
The function accepts two positive integers ‘r’ and ‘unit’ and a positive integer array ‘arr’ of size ‘n’ as its argument ‘r’ represents the number of rats present in an area, ‘unit’ is the amount of food each rat consumes and each ith element of array ‘arr’ represents the amount of food present in ‘i+1’ house number, where 0 <= i