HTML/JavaScript Web Application Answers

Questions: 680

Answers by our Experts: 648

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Search & Filtering

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().

Input

  • The input will be a single line containing an array nestedArray

Output

  • The output should be a single line containing an array

Constraints

  • Each value in the array must be a number


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

  • You can use the string method split()
  • You can use the array method map()

Input

  • The first line of input contains a string inputString
  • The second line of input contains a string separator
  • The third line of input contains a string replaceString

Output

  • The output should be a single line containing the strings separated by a space


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 ( || ).


Input

  • The first line of input contains a string stringsArray
  • The second line of input contains a string startString
  • The third line of input contains a number endString

Output

  • The output should be a single line containing an array of filtered strings


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,

  1. Submerge the Submarine
  2. Fire torpedos
  3. Surface the Submarine

Input

  • The first line of input contains a number totalTorpedos
  • The second line of input contains a number torpedosFired

Output

  • The first line of output is a string with Submarine Submerged text
  • The second line of output is a string with the number of torpedos fired and left, as shown in sample outputs
  • The third line of output is a string with Submarine Surfaced text

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

Input

  • The first line of input contains a number firstNumber
  • The second line of input contains a number secondNumber

Output

  • The first line of output should contain the ratio of firstNumber and secondNumber
  • The second line of output should contain the sum of cubes of firstNumber and secondNumber
  • The third line of output should contain the product of squares of firstNumber and secondNumber

Constraints

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

Input

  • The first line of input contains a string brand
  • The second line of input contains a string ram
  • The third line of input contains a number battery
  • The fourth line of input contains a string song
  • The fifth line of input contains a boolean isOnCall

Output

  • The first line of output is a string containing
  • battery before charging, as shown in the sample outputs
  • The second line of output is a string based on
  • battery, as shown in the sample outputs
  • The third line of output is a string containing
  • song, as shown in the sample outputs
  • The fourth line of output is a string "Music stopped"
  • The fifth line of output is a string "No ongoing call to end" or "Call Ended"
  • The sixth line of output is a string "Calling..."
  • The seventh line of output is a string "Call Ended"

Constraints

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

Input

The first line of input contains a number

fare The second line of input contains a number distance
Output

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


myArray of numbers, write a function to square the alternate numbers of the myArray, starting from index 0 using the array method map.Input

  • The input will be a single line containing an array myArray

Output

  • The output should be a single line containing an array with alternate numbers squared

Constraints

  • Each value in the array must be a number


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

objectEntities in the prefilled code and fruit as an input, write a JS program to log the array of objects containing the given fruit.
Input

The input will be a single line containing a string

fruitOutput

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 */


}


LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS