Found 8894 Articles for Front End Technology

CSS Loader in ReactJS

Rahul Bansal
Updated on 18-Mar-2021 10:33:47

2K+ Views

In this article, we will learn how to provide dynamic class names to our React application.StepsNpm run ejectThis command will allow us to customize the configurations and the options that were predefined when we run the command of create-react-app. It is a one-sided operation that can’t be undone later on.Change Webpack configurationsWe need to configure the webpack loaders so as to enable css-loader which will then provide dynamic naming to our classes.Set the configurations as −{    test: /\.css$/,    loader: 'style-loader' }, {    test: /\.css$/,    loader: 'css-loader',    query: {       modules: true,     ... Read More

Context API in ReactJS

Rahul Bansal
Updated on 18-Mar-2021 10:33:01

1K+ Views

In this article, we are going to see how to access the data without passing it through every parent component in the class-based component.Context APIs are used to set the global data and this data can now be accessed in any of the children's components without the need to pass it through every parent component.ExampleSuppose there are three components namely, A, B and C. A is the parent component of B and B is the parent component of C. So, our component structure is as follows − A→B→C.We have defined some data in A and want to handle it in ... Read More

Conditional Rendering in ReactJS

Rahul Bansal
Updated on 18-Mar-2021 10:32:01

452 Views

In this article, we are going to see how to conditionally render a component based on some conditions in a React applicationIn ReactJS, we can render only the desired components based on certain given conditions by using the if-else statements or by using the logical && operator of JavaScript. When the provided condition is satisfied, then React will match the UI and update it accordingly.Using if-else conditionsExampleIn this example, we will build a React application that has an App component as a parent component which will conditionally re-render the Form component and update the UI accordingly.App.jsximport React, { useState } ... Read More

Counting n digit Numbers with all unique digits in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:23:20

200 Views

ProblemWe are required to write a JavaScript function that takes in a number, let’s say num, as the only argument. The function should count all such numbers that have num digits and all of their digits are unique.For example, if the input to the function is −const num = 1;Then the output should be −const output = 10;Output Explanation:The numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 all have 1 digit and all are unique.ExampleThe code for this will be − Live Democonst num = 1; const uniqueDigits = (num = 1) => {    const dp = [1, 10];    const sum = [1, 11];    for (let i = 2; i

Placing same string characters apart in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:25:32

94 Views

ProblemWe are required to write a JavaScript function that takes in a string of characters, str, as the first argument and a number, num, (num {    const map = {};    for(let i=0; i{       if(map[a] len){       return "";    };    const res = [];    let index = 0, max = str.length-1;    while(keys.length){       let currKey = keys.shift();       let count = map[currKey];       while(count){          res[index] = currKey;          index = index+2;          if(index>max)             index=1;             count--;       }    }    return res.join(""); }; console.log(placeApart(str));OutputAnd the output in the console will be −mlmklk

Applying f(x) to each array element in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:28:17

80 Views

ProblemSuppose, a mathematical function given by −f(x) = ax2 + bx + cWhere a, b and c are three constants.We are required to write a JavaScript function that takes in a sorted array of Integers, arr as the first argument and a, b and c as second, third and fourth argument. The function should apply the function f(x) to each element of the array arr.And the function should return a sorted version of the transformed array.For example, if the input to the function is −const arr = [-8, -3, -1, 5, 7, 9]; const a = 1; const b = ... Read More

Killing Enemy in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:29:53

208 Views

ProblemSuppose, we have a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero). We are required to write a function that returns the maximum enemies we can kill using only one bomb.The bomb kills all the enemies in the same row and column from the planted point until it hits the wall since the wall is too strong to be destroyed.We also have to keep in mind that we can only put the bomb in an empty cell. For example, if the input to the function is −const arr = [ ... Read More

Largest rectangle sum smaller than num in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:31:19

97 Views

ProblemWe are required to write a JavaScript function that takes in a 2-D array of Numbers as the first argument and a target sum number as the second argument.Our function should find out that rectangle from the 2-D array which has the greatest sum among all rectangles in the array but just less than or equal to the target sum specified by the second argument to the function.The function should then finally return that largest sum. For example, if the input to the function is −const arr = [    [1, 0, 1],    [0, -2, 3] ]; const num ... Read More

Finding a number of pairs from arrays with smallest sums in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:32:48

131 Views

ProblemWe are required to write a JavaScript function that takes in two sorted arrays of Integers as the first and the second argument respectively, arr1 and arr2.The third argument to the function will be a number, num, and num will always be lesser than the length of both the arrays. The task of our function is to pick (num) pairs of Integers.Each pair should have its first element from arr1 and second from arr2. The pairs should be picked such that the pairs have the smallest possible sum. Lastly our function should return an array of all these (num) pairs.For ... Read More

Any possible combination to add up to target in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:34:40

291 Views

ProblemWe are required to write a JavaScript function that takes in an array of unique integers, arr, as the first argument, and target sum as the second argument.Our function should count the number of all pairs (with repetition allowed) that can add up to the target sum and return that count.For example, if the input to the function is −const arr = [1, 2, 3]; const target = 4;Then the output should be −const output = 7;Output Explanation:Because, the possible combination ways are −(1, 1, 1, 1) (1, 1, 2) (1, 2, 1) (1, 3) (2, 1, 1) (2, 2) ... Read More

Advertisements