Longest Interval Containing One Number in C++

Arnab Chakraborty
Updated on 02-Sep-2020 11:27:25

204 Views

Suppose we have a list of distinct integers called nums. We have to find the size of the largest interval (inclusive) [start, end] such that it contains at most one number in nums.So, if the input is like nums = [10, 6, 20], then the output will be 99990, as the largest interval is [11, 100000], this contains 20 only.To solve this, we will follow these steps −ret := -infend := 100000prev := 1sort the array numsn := size of numsfor initialize i := 0, when i < size of nums, update (increase i by 1), do −if i + ... Read More

Is an Empty iframe src Valid in JavaScript

AmitDiwan
Updated on 02-Sep-2020 06:37:59

1K+ Views

For empty iframe src, use the element and set it like the following for an empty src −Example Live Demo Document To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.OutputThis will produce the following output −

Transform Object of Objects to Object of Array of Objects in JavaScript

AmitDiwan
Updated on 02-Sep-2020 06:19:44

810 Views

To transform object of objects to object of array of objects, use the concept of Object.fromEntries() along with map().Exampleconst studentDetails = {    'details1': {Name: "John", CountryName: "US"},    'details2': {Name: "David", CountryName: "AUS"},    'details3': {Name: "Bob", CountryName: "UK"}, }; console.log(    Object.fromEntries(Object.entries(studentDetails).map(([key,    value]) => [key, [value]])) );To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo45.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo45.js {    details1: [ { Name: 'John', CountryName: 'US' } ],    details2: [ { Name: 'David', CountryName: 'AUS' } ],    details3: ... Read More

Why HTML File Can't Find JavaScript Function from Sourced Module

AmitDiwan
Updated on 02-Sep-2020 06:14:29

2K+ Views

This may happen if you haven’t used “export” statement. Use “export” before the function which will be imported into the script file. The JavaScript file is as follows which has the file name demo.js.demo.jsconsole.log("function will import"); export function test(){    console.log("Imported!!!"); }Here is the “index.html” file that imports the above function −index.htmlExample Live Demo Document    import { test } from "./demo.js"    test(); To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS ... Read More

Refresh Page After Clearing All Form Fields in jQuery

AmitDiwan
Updated on 01-Sep-2020 12:30:34

2K+ Views

To refresh page, use the location.reload() in JavaScript. The sample JavaScript code is as follows −Example Live Demo Document UserName: Password: Refresh Page    $('#RefreshPage').click(function() {       location.reload();    }); To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.OutputThis will produce the following output −After filling the form, the snapshot is as follows −When you click the button “Refresh Page”, the page will refresh and the following output is visible −

Get Focus on Textbox After Pressing Mouse Cursor in JavaScript

AmitDiwan
Updated on 01-Sep-2020 12:27:37

452 Views

Let’s say the following is our input text −StudentName:To lose input hints on pressing mouse cursor, use onFocus and onBlur concept.Example Live Demo Document StudentName:    function guessFocus() {       if (this.value == this.defaultValue)          this.value = '';    }    function guessBlur(event) {       if (this.value == '')       this.value = this.defaultValue;    }    var event = document.getElementById('studentName');    event.onfocus = guessFocus;    event.onblur = guessBlur; To run the above program, save the file name “anyName.html(index.html)” and ... Read More

Inherit CSS Properties of Parent Element Using JavaScript

AmitDiwan
Updated on 01-Sep-2020 12:23:55

462 Views

You can use classList.add() along with append(). Use the document.createElement() to create a new div and the classList.add() would add the CSS class.(.class selector).Example Live Demo Document    .add-all-subject {       display: grid;       height: 100px;       width: 100px;       grid-template-columns: 2fr 2fr 2fr 2fr 2fr;       grid-template-rows: 2fr;       grid-column-gap: 5.9rem;       grid-row-gap: 2rem;       color: red;       border: 2px solid red;    } Add Subjects    const addSubjectArea = document.getElementById("add-subject"); ... Read More

Capitalize the First Letter of Each Word in a String Using JavaScript

AmitDiwan
Updated on 01-Sep-2020 12:18:46

3K+ Views

At first, you need to split() the string on the basis of space and extract the first character using charAt(). Use toUpperCase() for the extracted character.Examplefunction capitalizeTheFirstLetterOfEachWord(words) {    var separateWord = words.toLowerCase().split(' ');    for (var i = 0; i < separateWord.length; i++) {       separateWord[i] = separateWord[i].charAt(0).toUpperCase() +       separateWord[i].substring(1);    }    return separateWord.join(' '); } console.log(capitalizeTheFirstLetterOfEachWord("my name is john"));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo43.js.OutputThis will produce the following output with first letter capitalize −PS C:\Users\Amit\JavaScript-code> node demo43.js My Name ... Read More

Remove Same Values from Array in JavaScript

AmitDiwan
Updated on 01-Sep-2020 12:14:07

217 Views

Let’s say the following is our array with similar values −const listOfStudentName = ['John', 'Mike', 'John', 'Bob', 'Mike', 'Sam', 'Bob', 'John'];To remove similar values from array, use the concept of set(). Following is the code −Exampleconst listOfStudentName = ['John', 'Mike', 'John', 'Bob', 'Mike', 'Sam', 'Bob', 'John']; console.log("The value="+listOfStudentName); const doesNotContainSameElementTwice = [...new Set(listOfStudentName)]; console.log("The Array="); console.log(doesNotContainSameElementTwice)To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo42.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo42.js The value=John, Mike, John, Bob, Mike, Sam, Bob, John The Array= [ 'John', 'Mike', 'Bob', 'Sam' ]Read More

Check If Value is Empty in JavaScript

AmitDiwan
Updated on 01-Sep-2020 12:13:00

21K+ Views

Use the condition with “” and NULL to check if value is empty. Throw a message whenever ua ser does not fill the text box value.Example Live Demo Document USERNAME:    function checkingUserName() {       var username = document.forms["register"]["username"].value;       if (username == null || username == "") {          alert("Please enter> the username. Can’t be blank or empty !!!");          return false;       }    } To run the above program, save ... Read More

Advertisements