Find Data Type of User Input in C++

Ayush Gupta
Updated on 01-Oct-2020 11:24:43

960 Views

In this problem, we are given input from the user. Our task is to create a program to find out the data type of user input in C++.Problem Description − We will take input from the user and check the data type of the input value.Let’s take an example to understand the problem, Example 1:Input − 34Output − It is an integerExample 2:Input − tutorialspointOutput − It is a stringSolution Approach:We will check if the input string is a number or not a number.If it is a number, we will check if it is an integer or a float value.If ... Read More

Find Perimeter and Circumference of Square and Rectangle in C++

Ayush Gupta
Updated on 01-Oct-2020 11:22:01

540 Views

In this problem, we are given the side of a square (A) and the length and breadth of a rectangle (L and B). Our task is to create a Program to find Perimeter / Circumference of Square and Rectangle in C++.Problem Description:To find the circumference of a square, we need the side of the square (a). For that, we will use the formula for the perimeter of the square which is 4a.To find the circumference of a rectangle, we need the Length (L) and breadth (B) of the rectangle. For that, we will use the formula for the perimeter of ... Read More

Sort Date Array in JavaScript

AmitDiwan
Updated on 01-Oct-2020 11:09:15

478 Views

Suppose we have an array that contains some dates like this −const arr = [    [ '02/13/2015', 0.096 ],    [ '11/15/2013', 0.189 ],    [ '05/15/2014', 0.11 ],    [ '12/13/2013', 0.1285 ],    [ '01/15/2013', 0.12 ],    [ '01/15/2014', 0.11 ],    [ '02/14/2014', 0.11 ],    [ '03/14/2014', 0.11 ],    [ '01/15/2015', 0.096 ],    [ '07/15/2015', 0.096 ],    [ '04/15/2013', 0.12 ],    [ '04/15/2014', 0.11 ],    [ '05/15/2013', 0.12 ],    [ '06/14/2013', 0.12 ],    [ '06/16/2014', 0.11 ],    [ '07/15/2013', 0.12 ],    [ '07/15/2014', 0.11 ], ... Read More

Recursive String Parsing into Object in JavaScript

AmitDiwan
Updated on 01-Oct-2020 11:06:42

554 Views

We are required to write a JavaScript function that takes in an array of strings and returns an object corresponding to the strings.For example −If the array is −const arr = [    "country.UK.level.1",    "country.UK.level.2",    "country.US.level.1",    "country.UK.level.3" ];Then the output should be −const output = {    "country": [        {"UK" : {"level" : ["1", "2", "3"]}},        {"US" : {"level" : ["1", "2"]}}   ] } ConditionsStrings stored in the str array will not be sorted and the code should be robust against that.Strings will follow the x.y.x.y... pattern, where x will be ... Read More

Join JavaScript Array of Strings

AmitDiwan
Updated on 01-Oct-2020 11:03:47

166 Views

We are required to write a JavaScript function that takes in an array of strings. The function should join all the strings of the array, replace all the whitespaces with dash "-", and return the string thus formed.For example: If the array is −const arr = ["QA testing promotion ", " Twitter  ", "Facebook ", "Test"];Then the output should be −const output = "QA-testing-promotion-Twitter-Facebook-Test";ExampleFollowing is the code −const arr = ["QA testing promotion ", " Twitter ", "Facebook ", "Test"]; const joinArr = arr => {    const arrStr = arr.join('');    let res = '';    for(let i = ... Read More

Remove Negatives from the Array in JavaScript

AmitDiwan
Updated on 01-Oct-2020 11:02:20

329 Views

Given an array X of multiple values (e.g. [-3, 5, 1, 3, 2, 10]), We are required to write a function that removes any negative values in the array.Once the function finishes its execution the array should be composed of just positive numbers. We are required to do this without creating a temporary array and only using pop method to remove any values in the array.ExampleFollowing is the code −// strip all negatives off the end while (x.length && x[x.length - 1] < 0) {    x.pop(); } for (var i = x.length - 1; i >= 0; i--) { ... Read More

Finding Number That Appears for Odd Times in JavaScript

AmitDiwan
Updated on 01-Oct-2020 11:00:01

214 Views

Given an array of integers, we are required to write a function that takes this array and finds the one element that appears an odd number of times. There will always be only one integer that appears an odd number of times.We will approach this problem by sorting the array. Once sorted, we can iterate over the array to pick the element that appears for odd number of times.ExampleFollowing is the code −const arr = [20, 1, -1, 2, -2, 3, 3, 5, 5, 1, 2, 4, 20, 4, -1, -2, 5]; const findOdd = arr => {    let ... Read More

Find the Biggest Number in an Array with Undefined Elements in JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:57:35

246 Views

We are required to write a JavaScript function that takes in an array that contains some numbers, some strings and some falsy values.Our function should return the biggest Number from the array.For example −If the input array is the following with some undefined values −const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii'];Then the output should be 65ExampleFollowing is the code −const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii']; const pickBiggest = arr => {    let max = -Infinity;    for(let i = 0; i < arr.length; i++){     ... Read More

Select the Middle of an Array in JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:55:02

644 Views

We are required to write a JavaScript function that takes in an array of Numbers.The function should return the middlemost element of the array.For example −If the array is −const arr = [1, 2, 3, 4, 5, 6, 7];Then the output should be 4ExampleFollowing is the code −const arr = [1, 2, 3, 4, 5, 6, 7]; const middle = function(){    const half = this.length >> 1;    const offset = 1 - this.length % 2;    return this.slice(half - offset, half + 1); }; Array.prototype.middle = middle; console.log(arr.middle()); console.log([1, 2, 3, 4, 5, 6].middle());OutputThis will produce the following output on console −[ 4 ] [ 3, 4 ]

Convert Array into Array of Subarrays in JavaScript

AmitDiwan
Updated on 01-Oct-2020 10:52:32

1K+ Views

We are required to write a JavaScript function that takes in an array of literals and returns a new array that have elements of the original array chunked into subarrays of length exactly 2.Now if the length of original array is not exactly divisible by 2, then the last subarray should have only one element.For example, If the input array is −const arr = [1, 2, 3, 4, 5, 6, 7];Then the output should be −const output = [[1, 2], [3, 4], [5, 6], [7]]ExampleFollowing is the code −const arr = [1, 2, 3, 4, 5, 6, 7]; const chunk ... Read More

Advertisements