Front End Technology Articles

Page 476 of 652

Finding the only out of sequence number from an array using JavaScript

AmitDiwan
AmitDiwan
Updated on 20-Apr-2021 502 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers. The array is sorted in ascending / increasing order and only one element in the array is out of order.Our function should find and return that element.ExampleFollowing is the code −const arr = [1, 2, 3, 4, 17, 5, 6, 7, 8]; const findWrongNumber = (arr = []) > {    for(let i = 0; i < arr.length - 1; i++){       const el = arr[i];       if(el - arr[i + 1] < 0 && arr[i + 1] - arr[i + 2] > 0){          return arr[i + 1];       }    }; }; console.log(findWrongNumber(arr));Output17

Read More

Representing number as the power and product of primes in JavaScript

AmitDiwan
AmitDiwan
Updated on 20-Apr-2021 263 Views

ProblemWe are required to write a JavaScript function that takes in a positive integer. Our function should represent this number as the sum of some powers of prime numbers.Therefore, for the number n, our function should return a string like this −n = "(p1**n1)(p2**n2)...(pk**nk)"Where p1, p2, p3..pk are prime numbers and n1, n2, ..nk are their non-negative powers and a ** b stands for a raised to the power b.ExampleFollowing is the code −const isPrime = num => {     for(let i = 2; i < num; i++){         if(num % i === 0){     ...

Read More

Next multiple of 5 and binary concatenation in JavaScript

AmitDiwan
AmitDiwan
Updated on 19-Apr-2021 150 Views

ProblemWe are required to write a JavaScript function that takes in a number n. Our function should return the next higher multiple of five of that number, obtained by concatenating the shortest possible binary string to the end of this number's binary representation.ExampleFollowing is the code −const generateAll = (num = 1) => {    const res = [];    let max = parseInt("1".repeat(num), 2);    for(let i = 0; i {    const numBinary = num.toString(2);    let i = 1;    while(true){       const perm = generateAll(i);       const required = perm.find(binary => {          return parseInt(numBinary + binary, 2) % 5 === 0;       });       if(required){          return parseInt(numBinary + required, 2);       };       i++;    }; }; console.log(smallestMultiple(8));Output35

Read More

Encoding a number string into a string of 0s and 1s in JavaScript

AmitDiwan
AmitDiwan
Updated on 19-Apr-2021 364 Views

ProblemWe are required to write a JavaScript function that takes in a string that represents a decimal number.Our function should convert/encode this decimal into binary based on the following rules.For each digit d of nLet k be the number of bits of dWe write k-1 times the digit 0 followed by the digit 1We write digit d as a binary string, with the rightmost bit being the least significantLastly, we concatenate the result of b) and c) to get the coding of dAt last, we concatenate all the results got for the digits of n.Thus, code 2 as 0110 and ...

Read More

Numbers and operands to words in JavaScript

AmitDiwan
AmitDiwan
Updated on 17-Apr-2021 242 Views

ProblemWe are required to write a JavaScript function that takes in a string of some mathematical operation and return its literal wording.ExampleFollowing is the code −const str = '5 - 8'; const convertToWords = (str = '') => {    const o = {       "+" : "Plus",       "-" : "Minus",       "*" : "Times",       "/" : "Divided By",       "**" : "To The Power Of",       "=" : "Equals",       "!=" : "Does Not Equal",    }    const n = {   ...

Read More

Length of longest string chain in JavaScript

AmitDiwan
AmitDiwan
Updated on 07-Apr-2021 416 Views

Word ChainLet's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For example, "abc" is a predecessor of "abac".A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.ProblemWe are required to write a JavaScript function that takes in an array of strings, arr, as the first and the only argument.Each string in the array arr consists of English lowercase letters. Our ...

Read More

Rearranging array elements in JavaScript

AmitDiwan
AmitDiwan
Updated on 07-Apr-2021 236 Views

ProblemJavaScript function that takes in an array of literals, arr, as the first and the only argument. This array contains some duplicates placed adjacently.Our function should rearrange the elements of the array such that no two elements in the array are equal. Our function should return the rearranged array, given that it's guaranteed that there exists at least one possible way of such arrangement.For example, if the input to the function is −const arr = [7, 7, 7, 8, 8, 8];Then the output should be −const output = [7, 8, 7, 8, 7, 8];Output Explanation:There may be other correct possible ...

Read More

Path with smallest sum in JavaScript

AmitDiwan
AmitDiwan
Updated on 07-Apr-2021 341 Views

ProblemJavaScript function that takes in a 2-D array of numbers as the first and the only argument.Our function should find paths from the 2-D array by picking exactly one element from each row, and no two elements picked from adjacent rows should be in the same column. Out of all these paths, our function should return the sum of that path that has the minimum sum.For example, if the input to the function is −const arr = [    [4, 7, 1],    [2, 8, 3],    [5, 6, 9] ]Then the output should be −const output = 9;Output ExplanationBecause ...

Read More

Difference Between GET and POST Method in HTML

AmitDiwan
AmitDiwan
Updated on 23-Mar-2021 5K+ Views

In this post, we will understand the difference between GET and POST methods in HTML.GET MethodThe parameters are placed inside the URL.Its main goal is to retrieve the data/documents present inside it.It has the ability to bookmark the results of the query.It is vulnerable, and not secure enough.This is because the data and credentials are present as plain text.It can be seen by anyone.The data constraint is that only ASCII characters are allowed.It can be up to 2000 characters only.It is also said to keep the length of the data to a minimum value.Data can be cached when GET method ...

Read More

Limiting duplicate character occurrence to once in JavaScript

AmitDiwan
AmitDiwan
Updated on 20-Mar-2021 226 Views

ProblemWe are required to write a JavaScript function that takes in a string, str, as the only argument.The function should prepare a new string based on the input string in which the only one appearance of each character is kept and the character kept is the one which makes the result string lexicographically the smallest.For example, if the input to the function is −const str = 'cbacdcbc';Then the output should be −const output = 'acdb';Output Explanation:Note that we could have removed any occurrence of ‘c’ from the string but we removed the very first, which makes the string lexicographically the ...

Read More
Showing 4751–4760 of 6,518 articles
« Prev 1 474 475 476 477 478 652 Next »
Advertisements