Convert XML to JSON and JSON Back to XML using Newtonsoft.Json

Nizamuddin Siddiqui
Updated on 25-Nov-2020 11:32:31

3K+ Views

Json.NET supports converting JSON to XML and vice versa using the XmlNodeConverter.Elements, attributes, text, comments, character data, processing instructions, namespaces, and the XML declaration are all preserved when converting between the twoSerializeXmlNodeThe JsonConvert has two helper methods for converting between JSON and XML. The first is SerializeXmlNode(). This method takes an XmlNode and serializes it to JSON text.DeserializeXmlNodeThe second helper method on JsonConvert is DeserializeXmlNode(). This method takes JSON text and deserializes it into an XmlNode.Example 1static void Main(string[] args) {    string xml = @"Alanhttp://www.google1.com Admin1";    XmlDocument doc = new XmlDocument();    doc.LoadXml(xml);    string json = JsonConvert.SerializeXmlNode(doc); ... Read More

Finding Number of Days Between Two Dates in JavaScript

AmitDiwan
Updated on 25-Nov-2020 07:54:45

250 Views

We are required to write a JavaScript function that takes in two dates in the 'YYYY-MM-DD' format as the first and second argument respectively. The function should then calculate and return the number of days between the two dates.For example −If the input dates are −const str1 = '2020-05-21'; const str2 = '2020-05-25';Then the output should be −const output = 4;Exampleconst str2 = '2020-05-25'; const daysBetweenDates = (str1, str2) => {    const leapYears = (year, month) => {       if (month (year * 365) + leapYears(year, month) + monthDays[month] + d; let p = days(...str1.split('-').map(Number));   ... Read More

Lucky Numbers in a Matrix in JavaScript

AmitDiwan
Updated on 25-Nov-2020 07:01:27

626 Views

Given a m * n matrix of distinct numbers, we have to return all lucky numbers in the 2-D array (matrix) in any order.A lucky number is an element of the matrix such that it is the minimum element in its row and maximum in its column.For example − If the input array is −const arr = [    [3, 7, 8],    [9, 11, 13],    [15, 16, 17] ];Then the output should be −const output = [15];because 15 is the only lucky number since it is the minimum in its row and the maximum in its column.ExampleThe code ... Read More

Find Distance Between Items in Array using JavaScript

AmitDiwan
Updated on 25-Nov-2020 06:49:52

352 Views

Suppose, we have a sorted (increasing order) array of Numbers like this −const arr = [2, 5, 7, 8, 9];We are required to write a JavaScript function that takes in one such array. The function should construct a new subarray for each element of the input array.The sub-array should contain the difference (difference between that very element and the succeeding elements one by one) elements.Therefore, for the first array element, the differences are −5 - 2 = 3 7 - 2 = 5 8 - 2 = 6 9 - 2 = 7Therefore, the subarray for the first element should ... Read More

Finding Number of Spaces in a String in JavaScript

AmitDiwan
Updated on 25-Nov-2020 05:23:36

1K+ Views

We are required to write a JavaScript function that takes in a string containing spaces. The function should simply count the number of spaces present in that string.For example −If the input string is −const str = 'this is a string';Then the output should be −const output = 4;Exampleconst str = 'this is a string'; const countSpaces = (str = '') => {    let count = 0;    for(let i = 0;    i < str.length; i++){       const el = str[i];       if(el !== ' '){          continue; };          count++; };       return count; }; console.log(countSpaces(str));OutputThis will produce the following output −4

Sort Array of Numbers by Increasing Frequency in JavaScript

AmitDiwan
Updated on 25-Nov-2020 05:14:30

1K+ Views

We are required to write a JavaScript function that takes in an array of numbers that might contain some repeating numbers.The function should sort the array such that the elements that are repeated for the least number of times appears first followed by the elements with increasing frequency.For example −If the input array is −const arr = [1, 1, 2, 2, 2, 3];Then the sorted array should be −const output = [3, 1, 1, 2, 2, 2];Exampleconst arr = [1, 1, 2, 2, 2, 3]; const frequencySort = (arr = []) => {    let map = {};    for ... Read More

Can Form Target Array from Source Array in JavaScript

AmitDiwan
Updated on 25-Nov-2020 05:13:24

212 Views

We are given an array of distinct integers, let’s say arr, and another array of integer arrays, let say sourceArr.In the sourceArr array, the integers are distinct. We should write a function that forms arr by concatenating the arrays in sourceArr in any order.However, we cannot reorder the integers inside of any subarray in the soureArr. We should return true if it is possible to form the array arr from sourceArr, false otherwise.For example −const arr = [23, 67, 789]; const sourceArr = [[23], [789, 67]];The function should return false because we cannot reorder the elements inside a subarray and ... Read More

Compare Strings in JavaScript and Return Percentage of Likeliness

AmitDiwan
Updated on 25-Nov-2020 05:12:03

1K+ Views

We are required to write a JavaScript function that can compare two strings and return the percentage likeliness of how much they are alike. The percentage will be nothing but a measure of many characters the two strings have in common.If they are completely similar the output should be 100, and if they contain no common character at all, the output should be 0.Exampleconst calculateSimilarity = (str1 = '', str2 = '') => {    let longer = str1;    let shorter = str2;    if (str1.length < str2.length) {       longer = str2; shorter = str1;   ... Read More

Check If a String Is Entirely Made of the Same Substring in JavaScript

AmitDiwan
Updated on 25-Nov-2020 05:08:14

383 Views

We are required to write a JavaScript function that takes in a string. It should return true or false based on whether the input consists of a repeated character sequence.The length of the given string is always greater than 1 and the character sequence must have at least one repetition.For example −"aa" should return true because it entirely contains two strings "a""aaa" should return true because it entirely contains three string "a""abcabcabc" should return true because it entirely containas three strings "abc""aba" should return false because it at least there should be two same substrings and nothing more"ababa" should return ... Read More

Insert a Number into a Sorted Array of Numbers in JavaScript

AmitDiwan
Updated on 25-Nov-2020 05:05:49

1K+ Views

We are required to write a JavaScript function that takes in a sorted array of numbers as the first argument and a single number as the second argument.The function should push the number specified as the second argument into the array without distorting the sorting of the elements.We are required to do this without creating another array.Exampleconst arr = [6, 7, 8, 9, 12, 14, 16, 17, 19, 20, 22]; const num = 15; const findIndex = (arr, val) => {    let low = 0, high = arr.length;    while (low < high) {       let mid ... Read More

Advertisements