JavaScript Array Checking for Multiple Values

AmitDiwan
Updated on 14-Oct-2020 07:22:36

257 Views

We are required to write a JavaScript function that takes in two arrays of Numbers and checks whether all the elements of the first array exist in the second or not.ExampleThe code for this will be −const arr1 = [34, 78, 89]; const arr2 = [78, 67, 34, 99, 56, 89]; const multipleIncludes = (first, second) => {    const indexArray = first.map(el => {       return second.indexOf(el);    });    return indexArray.indexOf(-1) === -1; } console.log(multipleIncludes(arr1, arr2));OutputThe output in the console −true

Padding a String with Random Lowercase Alphabets in JavaScript

AmitDiwan
Updated on 14-Oct-2020 07:20:47

280 Views

We are required to write a function that takes in two arguments, first is a string and second is a number. The length of string is always less than or equal to the number. We have to insert some random lowercase alphabets at the end of the string so that its length becomes exactly equal to the number and we have to return the new string.ExampleLet’s write the code for this function −const padString = (str, len) => {    if(str.length < len){       const random = Math.floor(Math.random() * 26);       const randomAlpha = String.fromCharCode(97 + ... Read More

Push NaN to Last of Array Using Sort in JavaScript

AmitDiwan
Updated on 14-Oct-2020 07:18:43

449 Views

We have an array that contains String and number mixed data types, we have to write a sorting function that sorts the array so that the NaN values always end up at the bottom.The array should contain all the valid numbers in the front, followed by string literals, followed by NaN.The code for this will be −const arr = [344, 'gfd', NaN, '', 15, 'f', 176, NaN, 736, NaN, 872, 859, 'string', 13, 'new', NaN, 75]; const sorter = (a, b) => {    if(a !== a){       return 1;    }else if(b !== b){       ... Read More

Concatenate Two Arrays in PHP

AmitDiwan
Updated on 13-Oct-2020 08:50:40

3K+ Views

Simply use, array_merge() to concatenate two arrays in PHP. Let’s say the following are out arrays −$nameArray1 = array('John','David'); $nameArray2 = array('Mike','Sam');Now, set both the above arrays in array_merge() to concatenate them.The syntax is as follows −array_merge($yourFirstArrayName, $yourSecondArrayName);ExampleThe PHP code is as follows  Live Demo OutputThis will produce the following outputArray ( [0] => John [1] => David [2] => Mike [3] => Sam )

Make Sure String Has No Whitespace in PHP

AmitDiwan
Updated on 13-Oct-2020 08:48:42

4K+ Views

To check whether a string has no whitespace, use the preg_match() in PHP.The syntax is as follows preg_match('/\s/',$yourVariableName);ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputThe name (John Smith) has the space

Add Time in Minutes to DateTime String in PHP

AmitDiwan
Updated on 13-Oct-2020 08:46:42

11K+ Views

For this, you can use the strtotime() method.The syntax is as follows −$anyVariableName= strtotime('anyDateValue + X minute');You can put the integer value in place of X.ExampleThe PHP code is as follows Live Demo OutputThis will produce the following output2020-10-30 10:15:20

Count Values from a PHP Array in a Foreach Loop

AmitDiwan
Updated on 13-Oct-2020 08:44:41

1K+ Views

Let’s say the following is our PHP array $listOfNames = array('John','David','Mike','David','Mike','David');We want the output to display the count of values in the above array like this −Array ( [John] => 1 [David] => 3 [Mike] => 2 )To get the count, use inbuilt function array_count_values().ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputArray ( [John] => 1 [David] => 3 [Mike] => 2 )

Remove Character in String Array and Display Result in One String in PHP

AmitDiwan
Updated on 13-Oct-2020 08:41:18

333 Views

Let’s say the following is our string array −$full_name= '["John Doe","David Miller","Adam Smith"]';We want the output in a single string −John Doe, David Miller, Adam SmithFor this, use json_decode().ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputThe Result in one string=John Doe, David Miller, Adam Smith

Instance Child Class in Abstract Static Method in PHP

AmitDiwan
Updated on 13-Oct-2020 08:40:06

581 Views

For this, use $anyObjectName=new static() along with self.ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputJohn Doe

Generate Array of Random Unique Numbers in PHP

AmitDiwan
Updated on 13-Oct-2020 08:37:21

984 Views

For random unique numbers array, use range() along with shuffle().ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputArray ( [0] => 11 [1] => 14 [2] => 15 [3] => 12 [4] => 18 )

Advertisements