Product of values in the Sub-array(s)
Given an array
nestedArray of arrays, write a JS program to multiply the values in the sub-array if at least one of its values is even else return zero.
Quick Tip
You can use array methods map(), some() and reduce().
Sample Input 1
[ [ 12, 1, 2, 4, 1 ], [ 18, 20, 30, 45 ], [ 49, 11, 13, 21 ] ]
Sample Output 1
[ 96, 486000, 0 ]
Sample Input 2
[ [ 0, 1 ], [ 1, 3, 4 ] ]
Sample Output 2
[ 0, 12 ]
i want code in between write code here
"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 main() {
const nestedArray = JSON.parse(readLine());
/* Write your code here */
}
Split and Replace
Given three strings
inputString, separator and replaceString as inputs. Write a JS program to split the
inputString with the given separator and replace strings in the resultant array with the replaceString whose length is greater than 7.
Quick Tip
Sample Input 1
JavaScript-is-amazing
-
Programming
Sample Output 1
Programming is amazing
Sample Input 2
The&Lion&King
&
Tiger
Sample Output 2
The Lion King
i need code in between write code here
"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 main() {
const inputString = readLine();
const separator = readLine();
const replaceString = readLine();
/* Write your code here */
}
String Starts or Ends with given String
Given an array
stringsArray of strings, and startString, endString as inputs, write a JS program to filter the strings in stringsArray starting with startString or ending with endString.
Quick Tip
You can use the array method filter() and logical operator OR ( || ).
Sample Input 1
['teacher', 'friend', 'cricket', 'farmer', 'rose', 'talent', 'trainer']
t
r
Sample Output 1
[ 'teacher', 'farmer', 'talent', 'trainer' ]
Sample Input 2
['dream', 'player', 'read', 'write', 'trend']
p
d
Sample Output 2
[ 'player', 'read', 'trend' ]
i want code in between write code here
"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 main() {
const stringsArray = JSON.parse(readLine().replace(/'/g, '"'));
const startString = readLine();
const endString = readLine();
/* Write your code here */
}
Submarine
Given two numbers
totalTorpedos, torpedosFired as inputs, write a super class Submarine with property and methods as below,
PropertyDescriptionisSubmergedIt should contain a boolean value to indicate whether the submarine is submerged or not.
MethodDescriptiondiveWhen this method is called, it should set the value of isSubmerged to true and log "Submarine Submerged" text in the console.surfaceWhen this method is called, it should set the value of isSubmerged to false and log "Submarine Surfaced" text in the console.
Add a sub class weaponUnit which extends to Submarine with the below properties and methods,
PropertyDescriptiontorpedosIt should contain the totalTorpedos loaded.torpedosLaunchedIt should contain the torpedosFired.
MethodDescriptionfireTorpedosWhen this method is called, it should decrease the totalTorpedos by torpedosFired and log the number of torpedos fired and left, as shown in the sample outputs.
The sequence of operations is,
Sample Input 1
5
2
Sample Output 1
Submarine Submerged
2 Torpedos Fired, 3 Left
Submarine Surfaced
Sample Input 2
10
2
Sample Output 2
Submarine Submerged
2 Torpedos Fired, 8 Left
Submarine Surfaced
i want code in between write your code here
"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 */
class Submarine {
constructor() {
this.isSubmerged = false;
}
/* Write your code here */
}
class WeaponUnit extends Submarine {
/* Write your code here */
}
/* Please do not modify anything below this line */
function main() {
const totalTorpedos = parseInt(readLine());
const torpedosFired = parseInt(readLine());
const weaponUnit1 = new WeaponUnit(totalTorpedos, torpedosFired);
weaponUnit1.dive();
weaponUnit1.fireTorpedos();
weaponUnit1.surface();
}
Arithmetic Operations
Given a constructor function
ArithmeticOperations in the prefilled code and two numbers firstNumber and secondNumber as inputs, add the following methods to the constructor function using the prototype.MethodDescriptionratioOfNumbersIt Should return the ration of the numberssumOfCubesOfNumbersIt Should return the sum of cubes of the numbersproductOfSquaresOfNumbersIt Should return the product of squares of the numbers
The
secondNumber should not be equal to zero
Sample Input 1
8
4
Sample Output 1
2
576
1024
Sample Input 2
5
5
Sample Output 2
1
250
625
i want code in between write code here
"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 ArithmeticOperations(firstNumber, secondNumber) {
this.firstNumber = firstNumber;
this.secondNumber = secondNumber;
}
function main() {
const firstNumber = JSON.parse(readLine());
const secondNumber = JSON.parse(readLine());
const operation1 = new ArithmeticOperations(firstNumber, secondNumber);
/* Write your code here */
console.log(operation1.ratioOfNumbers());
console.log(operation1.sumOfCubesOfNumbers());
console.log(operation1.productOfSquaresOfNumbers());
}
Mobile
You are given an incomplete
Mobile class.A Mobile object created using the
Mobile class should have the properties like brand, ram, battery, isOnCall, and song.Implement the
Mobile class to initialize the mentioned properties and add the following methods,
MethodDescriptionchargingWhen this method is called, it should set the value of the battery to 100, if the battery is already 100 then log "Mobile Fully Charged" and call removeChargingremoveChargingIt should log "Please remove charging"playMusicIt should log a text with the song, as shown in the sample outputstopMusicIt should log "Music Stopped"makeCallWhen this method is called, it should set the value of the isOnCall to true and log "Calling ..."endCallWhen this method is called, it should log "No ongoing call to end" if isOnCall is false, else log "Call Ended" and set the value of the isOnCall to false
0 <=
battery <= 100
Sample Input 1
Apple
2 GB
90
Waka Waka
false
Sample Output 1
Mobile charged 90%
Mobile charged 100%
Playing Waka Waka song
Music stopped
No ongoing call to end
Calling...
Call Ended
Sample Input 2
Samsung
8 GB
100
Gangnam Style
true
Sample Output 2
Mobile charged 100%
Mobile is fully charged
Please remove charging
Playing Gangnam Style song
Music stopped
Call Ended
Calling...
Call Ended
i want code in between write code here
"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 */
class Mobile {
/*
* Write your code here
*/
}
/* Please do not modify anything below this line */
function main() {
const brand = readLine();
const ram = readLine();
const battery = parseInt(readLine());
const song = readLine();
const isOnCall = JSON.parse(readLine());
const myMobile = new Mobile(brand, ram, battery, isOnCall, song);
console.log(`Mobile charged ${myMobile.battery}%`); // The Mobile battery charged percentage
myMobile.charging(); // The Mobile charging
myMobile.playMusic(); // The Mobile will start playing a song
myMobile.stopMusic(); // The Mobile will stop playing a song
myMobile.endCall(); // The Mobile will end a call.
myMobile.makeCall(); // The Mobile will make a call.
myMobile.endCall(); // The Mobile will end a call.
}
Fare per Kilometer
Given total fare
fare and distance travelled in kilometers distance for a rental bike, write a JS constructor function with a method to calculate the fare per kilometer.
Quick Tip
The formula to calculate the fare per kilometer is,
fare per kilometer = fare / distance
The first line of input contains a number
The output should be a single line containing the fare per kilometer
Sample Input 1
120
6
Sample Output 1
20
Sample Input 2
5000
20
Sample Output 2
250
i want code in between write your code here
"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 Ride(fare, distance) {
/* Write your code here */
}
/* Please do not modify anything below this line */
function main() {
const fare = JSON.parse(readLine());
const distance = JSON.parse(readLine());
const ride1 = new Ride(fare, distance);
console.log(ride1.getFarePerKilometer());
}
Final Value with Appreciation
Given principal amount
principal as an input, time period in years time and appreciation percentage apprPercentage as optional inputs, write a JS function to return the final value finalValue with the given appreciation percentage and time period. The default values for time and apprPercentage are 2 and 5 respectively.
Quick Tip
The formula to calculate the final value with appreciation is,
finalValue = principal * (1 + time * appreciation / 100)
Input
The first line of input contains a number principal
The second line (optional) of input contains a number time
The third line (optional) of input contains a number apprPercentage
Output
The output should be a single line containing the finalValue
Sample Input 1
1000
2
3
Sample Output 1
1060
Sample Input 2
3000
Sample Output 2
3300.0000000000005
Square at Alternate Indices
Given an array
Sample Input 1
[ 1, 2, 3, 4, 5 ]
Sample Output 1
[ 1, 2, 9, 4, 25 ]
Sample Input 2
[ 2, 4 ]
Sample Output 2
[ 4, 4 ]
Objects with given Fruit
Given an array of objects
The input will be a single line containing a string
The output should be the array of objects matching the given
fruit
Sample Input 1
apple
Sample Output 1
[
{ fruit: 'apple', vegetable: 'broccoli' },
{ fruit: 'apple', vegetable: 'cauliflower' }
]
Sample Input 2
orange
Sample Output 2
[ { fruit: 'orange', vegetable: 'mushrooms' } ]
i want code in between write your code here
"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 main() {
const fruit = readLine();
const objectEntities = [
{
fruit: "apple",
vegetable: "broccoli"
},
{
fruit: "kiwi",
vegetable: "broccoli"
},
{
fruit: "apple",
vegetable: "cauliflower"
},
{
fruit: "orange",
vegetable: "mushrooms"
},
];
/* Write your code here */
}