Found 6710 Articles for Javascript

Display whether the office is closed or open right now on the basis of current time with JavaScript ternary operator

AmitDiwan
Updated on 09-Sep-2020 13:04:42

356 Views

Let’s say, we are matching the current date and time with the business hours. We need to display whether the office is closed or open right now on the basis of current time.Get the hours from the current date and can use the ternary operator for close and open. Following is the code −Example Live Demo Document    const gettingHours = new Date().getHours()    const actualHours = (gettingHours >= 10 && gettingHours < 18) ? 'Open' : 'Closed';    document.querySelector('.closeOrOpened').innerHTML = actualHours; To run the above program, save ... Read More

Counting elements of an array using a recursive function in JS?

AmitDiwan
Updated on 09-Sep-2020 13:02:23

611 Views

The recursive function calls itself with some base condition. Let’s say the following is our array with marks −var listOfMarks=[56, 78, 90, 94, 91, 82, 77];Following is the code to get the count of array elements −Examplefunction countNumberOfElementsUsingRecursive(listOfMarks) {    if (listOfMarks.length == 0) {       return 0;    }    return 1 +    countNumberOfElementsUsingRecursive(listOfMarks.slice(1)); } var listOfMarks=[56, 78, 90, 94, 91, 82, 77]; console.log("The array="); console.log(listOfMarks); var numberOfElements=countNumberOfElementsUsingRecursive(listOfMarks); console.log("The Number of elements = "+numberOfElements);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo110.js.OutputThis will produce the following ... Read More

Get Random value from a range of numbers in JavaScript?

AmitDiwan
Updated on 09-Sep-2020 13:00:42

236 Views

Let’s say, first, we will set the start and end range and call the function:console.log(getRandomValueBetweenTwoValues(400, 480))We have passed start value 400 and end value 480. Let’s get the random value with Math.random() in JavaScript −Examplefunction getRandomValueBetweenTwoValues(startRange, endRange) {    return Math.floor(Math.random() * (endRange - startRange + 1) + startRange); } console.log(getRandomValueBetweenTwoValues(400, 480))To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo109.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo109.js 401

Check if a string has white space in JavaScript?

AmitDiwan
Updated on 09-Sep-2020 12:59:13

4K+ Views

To check whitespace in a string, use the concept of indexOf(‘ ’). Following is the code −Examplefunction stringHasTheWhiteSpaceOrNot(value){    return value.indexOf(' ') >= 0; } var whiteSpace=stringHasTheWhiteSpaceOrNot("MyNameis John");    if(whiteSpace==true){       console.log("The string has whitespace");    } else {       console.log("The string does not have whitespace"); }To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo108.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo108.js The string has whitespace

How to put variable in regular expression match with JavaScript?

AmitDiwan
Updated on 09-Sep-2020 12:57:59

1K+ Views

You can use match() with passing the variable name. The syntax is as follows −console.log(yourVariableName.match(yourMatchingVariableName));Examplevar senetence = 'My Name is John'; console.log("The actual value="); console.log(senetence); var matchWord = 'John'; console.log("The matching value="); console.log(matchWord); var matchRegularExpression = new RegExp(matchWord, 'g' ); console.log(senetence.match(matchWord));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo107.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo107.js The actual value= My Name is John The matching value= JohnRead More

How to create a custom function similar to find() method in JavaScript?

AmitDiwan
Updated on 09-Sep-2020 12:56:13

423 Views

Let’s say we have the following records of studentId and studentName and want to check a specific student name −const studentDetails= [    {       studentId:101,       studentName:"John"    },    {       studentId:102,       studentName:"David"    },    {       studentId:103,       studentName:"Carol"    } ]Create a custom function to find by name. Following is the code −Exampleconst studentDetails= [    {       studentId:101,       studentName:"John"    },    {       studentId:102,       studentName:"David"    },    {       studentId:103,       studentName:"Carol"    } ] function findByName(name){    var flag=true;    for(var i=0;i node demo106.js The name found=David

Difference between Hibernate and Eclipse link

Himanshu shriv
Updated on 09-Sep-2020 12:06:39

2K+ Views

Hibernate and Eclipse link both are object relational mapping tool. They both are the implementation of JPA.Hibernate is very popular implementation of JPA built by Red hat. It also has some extra features that JPA does not provide.Eclipse is an open source implementation of JPA built by Eclipse foundation. It 'is one of the first project which became part of EE4J. It is available in two forms −Eclipse link jar file format − It is complete package. It has everything which need to run any Eclipse link functionality.OSGI bundles for each of the eclipse link component.Sr. No.KeyHibernateEclipse link1BasicIt is a ... Read More

Demonstrate nested loops with return statements in JavaScript?

AmitDiwan
Updated on 07-Sep-2020 08:58:48

626 Views

Here’s an example with two loops, outer and inner −Examplelet demoForLoop = ()=>{    for(var outer=1;outer

How do you make a button that adds text in HTML 'input'?

AmitDiwan
Updated on 13-Mar-2025 12:49:45

3K+ Views

Let’s say the following is our HTML button −Click the button to add the input into the belowText BoxUse document.getElementById() to add a text in on button click. Following is the code −Example Live Demo Document Click the button to add the input into the below Text Box  document.getElementById("clickButton").addEventListener("click", () =>{ document.getElementById("readTheInput").value += "JavaScript";  }); 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 ... Read More

Converting any string into camel case with JavaScript removing whitespace as well

AmitDiwan
Updated on 07-Sep-2020 08:52:41

1K+ Views

In order to convert string into camel case, you need to lowercase the first letter of the word and the first letter of the remaining words must be in capital.Following is the code to convert any string into camel case −Examplefunction convertStringToCamelCase(sentence) {    return sentence.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g,    function(camelCaseMatch, i) {       if (+camelCaseMatch === 0)          return "";       return i === 0 ? camelCaseMatch.toLowerCase() :       camelCaseMatch.toUpperCase();    }); } console.log(convertStringToCamelCase("Add two variables"));To run the above program, you need to use the following command −node fileName.js.Here, my file name is ... Read More

Advertisements