To extract hostname from URL string, use split() function. Following is the code −Examplefunction gettingTheHostNameFromURL(websiteURL) { var getTheHostName; if (websiteURL.indexOf("//") > -1) { getTheHostName = websiteURL.split('/')[2]; } else { getTheHostName = websiteURL.split('/')[0]; } getTheHostName = getTheHostName.split(':')[0]; getTheHostName = getTheHostName.split('?')[0]; return getTheHostName; } var websiteURL="https://www.tutorialspoint.com/java/index.htm";var websiteURL1="https://www.tutorix.com/about_us.htm"; var websiteURL2="https://www.tutorialspoint.com/execute_python_online.php"; console.log(gettingTheHostNameFromURL(websiteURL)) console.log(gettingTheHostNameFromURL(websiteURL1)) console.log(gettingTheHostNameFromURL(websiteURL2))To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo161.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo161.js www.tutorialspoint.com www.tutorix.com www.tutorialspoint.comRead More
For this, use . Since you want to type in it, do not forget to set as −contenteditable="true"Example Live Demo Document span{ border: solid 2px skyblue; } div{ max-width: 500px; } John Smith 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 −
To find the index of a two-dimensional array of objects, use two for loops, one for row and another for column. Following is the code −Examplefunction matrixIndexed(details, name) { var r; var c; for (r = 0; r < details.length; ++r) { const nsDetails = details[r]; for (c = 0; c < nsDetails.length; ++c) { const tempObject = nsDetails[c]; if (tempObject.studentName === name) { return { r, c}; } } ... Read More
To get sequence number in loops, use the forEach() loop. Following is the code −Examplelet studentDetails = [ { id: 101, details: [{name: 'John'}, {name: 'David'},{name: 'Bob'}]}, {id:102, details: [{name:'Carol'},{name:'David'}, {name:'Mike'}] } ]; var counter = 1; studentDetails.forEach(function(k){ k.details.forEach(function(f) { console.log(counter++); } ); });To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo159.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo159.js 1 2 3 4 5 6
Let’s say the following is our array of objects with duplicates −var studentDetails=[ {studentId:101}, {studentId:104}, {studentId:106}, {studentId:104}, {studentId:110}, {studentId:106}, ]Use the concept of set to remove duplicates as in the below code −Examplevar studentDetails=[ {studentId:101}, {studentId:104}, {studentId:106}, {studentId:104}, {studentId:110}, {studentId:106}, ] const distinctValues = new Set const withoutDuplicate = [] for (const tempObj of studentDetails) { if (!distinctValues.has(tempObj.studentId)) { distinctValues.add(tempObj.studentId) withoutDuplicate.push(tempObj) } } console.log(withoutDuplicate);To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name ... Read More
For this, use Object.assign(). Following is the code −Exampleconst details = {john:{'studentName':'John'}, david:{'studentName':'David'}, mike:{'studen tName':'Mike'}, bob:{'studentName':'Bob'}, carol:{'studentName':'Carol'}}, join_values = ['David', 'Carol'], output = Object.assign( {}, ...Object .keys(details) .map(key => ({[key]: { ...details[key], lastName: join_values.includes(details[key].studentName) ? 'Miller' : 'Smith' }}))) console.log(output)To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo157.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo157.js { john: ... Read More
We will be creating two buttons, one for Increment and another Decrement −On clicking Increment (+), user will be able to increment the number in input type numberOn clicking Decrement (-), user will be able to decrement the number in input type numberExample Live Demo Document + - function increment() { document.getElementById('demoInput').stepUp(); } function decrement() { document.getElementById('demoInput').stepDown(); } To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option ... Read More
Here, we have set “Aabout_us” and “Hhome_page” with incorrectly spellings as anchor text.You can use substring(1) along with innerHTML to remove the first character and display them correctly as “about_us” and “home_page” respectively.Example Live Demo Document Aabout_us Hhome_page [...document.querySelectorAll('.linkDemo div a')].forEach(obj=> obj.innerHTML=obj.innerHTML.substring(1)) 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 with the correct forms ... Read More
Let’s say the following are our arrays −var firstArray=[100, 200, 400]; var secondArray=[400, 100, 200];You can sort both the arrays using the sort() method and use for loop to compare each value as in the below code −Examplevar firstArray=[100, 200, 400]; var secondArray=[400, 100, 200]; function areBothArraysEqual(firstArray, secondArray) { if (!Array.isArray(firstArray) || ! Array.isArray(secondArray) || firstArray.length !== secondArray.length) return false; var tempFirstArray = firstArray.concat().sort(); var tempSecondArray = secondArray.concat().sort(); for (var i = 0; i < tempFirstArray.length; i++) { if (tempFirstArray[i] !== tempSecondArray[i]) return false; } ... Read More
To access nested JSON property based on another property’s value, the code is as follows −Examplevar actualJSONData = JSON.parse(studentDetails()), studentMarks = getMarksUsingSubjectName(actualJSONData, "JavaScript"); console.log("The student marks="+studentMarks); function getMarksUsingSubjectName(actualJSONData, givenSubjectName){ for(var tempObj of actualJSONData){ if(tempObj.subjectName = givenSubjectName){ return tempObj.marks; } } } function studentDetails(){ return JSON.stringify( [ { firstName : "John", subjectName: "JavaScript", marks : 97 }, { firstName : "David", subjectName: "Java", marks : 98 } ] ); }To run the above ... Read More