Found 6686 Articles for Javascript

Hexadecimal color to RGB color JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:08:01

137 Views

We are required to write a JavaScript function that takes in a hexadecimal color and returns its RGB representation.The function should return an object containing the respective values of red green and blue color −For example:hexToRgb('#0080C0') should return 0, 128, 192The code for this will be −const hex = '#0080C0'; const hexToRGB = hex => {    let r = 0, g = 0, b = 0;    // handling 3 digit hex    if(hex.length == 4){       r = "0x" + hex[1] + hex[1];       g = "0x" + hex[2] + hex[2];       ... Read More

RGB color to hexadecimal color JavaScript

AmitDiwan
Updated on 09-Oct-2020 11:05:17

236 Views

We are required to write a JavaScript function that takes in a RGB color and returns its hexadecimal representation.The function should take in an object containing three numbers representing the respective values of red green and blue color.For example:rgbToHex(0, 128, 192) should return '#0080C0'The code for this will be −const rgbColor = {    red: 0,    green: 51,    blue: 155 } function rgbToHex({    red: r,    green: g,    blue: b }) {    const prefix = '#';    const hex = prefix + ((1

How to replace before first forward slash - JavaScript?

AmitDiwan
Updated on 03-Oct-2020 15:38:48

671 Views

Let’s say the following is our string with forward slash −var queryStringValue = "welcome/name/john/age/32"To replace before first forward slash, use replace() along with regular expressions.ExampleFollowing is the code −var regularExpression = /^[^/]+/ var queryStringValue = "welcome/name/john/age/32" var replacedValue = queryStringValue.replace(regularExpression, 'index'); console.log("Original value="+queryStringValue); console.log("After replacing the value="+replacedValue);To run the above program, you need to use the following command −node fileName.js. Here, my file name is demo245.js.OutputThis will produce the following output on console −PS C:\Users\Amit\javascript-code> node demo245.js Original value=welcome/name/john/age/32 After replacing the value=index/name/john/age/32Read More

Call a function with onclick() – JavaScript?

AmitDiwan
Updated on 03-Oct-2020 15:37:10

3K+ Views

Let’s say the following is our button −Press MeWe need to call a function on the click of above button.ExampleFollowing is the code − Live Demo            Document        Press Me    function displayingMessageOnButtonClick() {       const showMessage = document.getElementById('showTheTextMessage');       showMessage.innerHTML = 'Welcome to Javascript Program...';       showMessage.style.display = 'block';    } To run the above program, save the file name anyName.html(index.html). Right click on the file and select the option “Open with live server” in VS Code editor −OutputThe output is as follows −Now, press the button. Following is the output −

How to get only first word of object's value – JavaScript?

AmitDiwan
Updated on 03-Oct-2020 15:34:13

357 Views

Let’s say the following is our object −const employeeDetails = [    {       employeeName: "John Smith",       employeeTechnology: "JavaScript HTML"    },    {       employeeName: "David Miller",       employeeTechnology: "Java Angular"    } ]You can use split() on the basis of space.ExampleFollowing is the code −const employeeDetails = [    {       employeeName: "John Smith",       employeeTechnology: "JavaScript HTML"    },    {       employeeName: "David Miller",       employeeTechnology: "Java Angular"    } ] const objectValues = employeeDetails.map(emp => {    var ... Read More

How to redundantly remove duplicate elements within an array – JavaScript?

AmitDiwan
Updated on 03-Oct-2020 15:31:39

197 Views

Let’s say the following are our array elements −10,20,10,50,60,10,20,40,50To remove duplicate elements, use … new Set().ExampleFollowing is the code −var arrayWithNoDuplicateNumbers = [...new Set([10,20,10,50,60,10,20,40,50])]; console.log("No Duplicate values=") console.log(arrayWithNoDuplicateNumbers);To run the above program, use the following command −node fileName.js. Here, my file name is demo243.js.OutputThe output is as follows −PS C:\Users\Amit\javascript-code> node demo243.js No Duplicate values= [ 10, 20, 50, 60, 40 ]

Compare the Triplets - JavaScript?

AmitDiwan
Updated on 03-Oct-2020 15:30:31

131 Views

For this, you need to use if condition to compare triplets.Let’s say we are passing the following values −35, 36, 37, 33, 48, 50ExampleFollowing is the code −function tripletsSolution(first, second, third, fourth, fifth, sixth) {    var storedResult = []    if (first > fourth || second > fifth || third > sixth) {       storedResult = storedResult + 1;    }    if (first < fourth || second < fifth || third < sixth) {       storedResult = storedResult + 1;    }    return storedResult.split(''); } console.log(tripletsSolution(35, 36, 37, 33, 48, 50));To run the above program, use the following command −node fileName.js.Here, my file name is demo242.js.OutputThe output is as follows −PS C:\Users\Amit\javascript-code> node demo242.js [ '1', '1' ]

JavaScript - Create an alert on clicking an HTML button

AmitDiwan
Updated on 03-Oct-2020 15:27:51

3K+ Views

To fire an alert on click of a button, use addEventListener(). Let’s say the following is our button on an HTML web page −Please Press MeExampleFollowing is the code − Live Demo            Document    Please Press Me    var pressedButton = document.getElementsByTagName("button")[0];    pressedButton.addEventListener("click", function (event) {       alert("You have pressed the button..........")    }) To run the above program, save the file name anyName.html(index.html). Right click on the file and select the option “Open with live server” in VS Code editor −OutputThe output ... Read More

Splitting a hyphen delimited string with negative numbers or range of numbers - JavaScript?

AmitDiwan
Updated on 03-Oct-2020 15:25:31

351 Views

Let’s say we have the following hyphen delimited string with negative or range of numbers −var firstValue = "John-Smith-90-100-US"; var secondValue = "David-Miller--120-AUS";To split, use regular expressions. Following is the code −ExampleFollowing is the code −var firstValue = "John-Smith-90-100-US"; var secondValue = "David-Miller--120-AUS"; var regularExpression = /-(?=[A-Za-z-]|\d+-\d)/; var result1 = firstValue.split(regularExpression); var result2 = secondValue.split(regularExpression); console.log(result1); console.log(result2);To run the above program, use the following command −node fileName.js. Here, my file name is demo241.js.OutputThe output is as follows −PS C:\Users\Amit\javascript-code> node demo241.js [ 'John', 'Smith', '90-100', 'US' ] [ 'David', 'Miller', '-120', 'AUS' ]Read More

How to make filter and word replacement method - JavaScript?

AmitDiwan
Updated on 03-Oct-2020 15:24:22

205 Views

There is no in-built function to replace all occurrences of word. You need to create your own function.Let’s say the following is our string −var sentence = "Yes,  My Name is John Smith. I live in US. Yes,  My Favourite Subject is JavaScript";ExampleFollowing is the code −var sentence = "Yes, My Name is John Smith. I live in US. Yes, My Favourite Subject is JavaScript"; console.log(sentence); function replaceYesWithHi(word,  sentence,  replaceValue) {    return sentence.split(word).join(replaceValue); } var replacementofYesWithHi = replaceYesWithHi("Yes", sentence, "Hi"); console.log(replacementofYesWithHi);To run the above program, use the following command −node fileName.js.Here, my file name is demo240.js.OutputThe output is as follows −PS C:\Users\Amit\javascript-code> node demo240.js Yes, My Name is John Smith. I live in US. Yes, My Favourite ... Read More

Advertisements