AmitDiwan has Published 10744 Articles

Finding how many times a specific letter is appearing in a sentence in JavaScript

AmitDiwan

AmitDiwan

Updated on 14-Oct-2020 07:36:40

476 Views

We are required to write a JavaScript function that finds how many times a specific letter is appearing in the sentence.ExampleThe code for this will be −const string = 'This is just an example string for the program'; const countAppearances = (str, char) => {    let count = 0; ... Read More

Consecutive elements sum array in JavaScript

AmitDiwan

AmitDiwan

Updated on 14-Oct-2020 07:26:09

510 Views

We are required to write a JavaScript function that takes in an array of Numbers and returns a new array with elements as the sum of two consecutive elements from the original array.For example, if the input array is −const arr1 = [1, 1, 2, 7, 4, 5, 6, 7, ... Read More

JavaScript Array: Checking for multiple values

AmitDiwan

AmitDiwan

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

235 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, ... Read More

Padding a string with random lowercase alphabets to fill length in JavaScript

AmitDiwan

AmitDiwan

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

261 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 ... Read More

Pushing NaN to last of array using sort() in JavaScript

AmitDiwan

AmitDiwan

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

433 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 ... Read More

The easiest way to concatenate two arrays in PHP?

AmitDiwan

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 ... Read More

PHP make sure string has no whitespace?

AmitDiwan

AmitDiwan

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

3K+ 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

How to add time in minutes in datetime string PHP?

AmitDiwan

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

How to count values from a PHP array and show value only once in a foreach loop?

AmitDiwan

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 ... Read More

How to remove a character (‘’) in string array and display result in one string php?

AmitDiwan

AmitDiwan

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

322 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 ... Read More

Advertisements