Found 9315 Articles for Object Oriented Programming

Checking a Checkbox with JavaScript

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

427 Views

Let’s say the following are ourrinput type checkbox −John David We want to check any of the checkbox. Use the checked property to check the checkbox.ExampleFollowing is the code − Live Demo            Document    John        David        document.querySelector('#checkedValue2').checked = true 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 −

How to replace some preceding characters with a constant 4 asterisks and display the last 3 as well - JavaScript?

AmitDiwan
Updated on 03-Oct-2020 15:11:14

812 Views

Let’s say the following are our values −'6778922' '76633 56 1443' '8888 4532 3232 9999'We want the preceding characters to be replaced with 4 asterisks and the display rest of the last 3 characters. The output should be −**** 922 **** 443 **** 999For such conditions, use the replace() and set regex in it.ExampleFollowing is the code −const hideDataWithDot = value => value.replace(/.+(.{3})$/, "**** $1"); console.log(hideDataWithDot('6778922')) console.log(hideDataWithDot('76633 56 1443')) console.log(hideDataWithDot('8888 4532 3232 9999')) To run the above program, use the following command −node fileName.js. Here, my file name is demo236.js.OutputThe output is as follows −PS C:\Users\Amit\javascript-code> node demo236.js **** ... Read More

Split the sentences by comma and remove surrounding spaces - JavaScript?

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

7K+ Views

Let’s say the following is our string with comma and whitespace −var sentences = "  John ,    David ,         Bob ,      Mike,            Carol        ";To split the sentences by comma, use split(). For removing surrounding spaces, use trim().ExampleFollowing is the code −var sentences = "  John , David , Bob , Mike, Carol "; console.log("The value=" + sentences); var result = sentences.split(", ").map(function (value) {    return value.trim(); }); console.log("After modifying the value=") console.log(result);To run the above program, use the following command −node fileName.js.Here, my file ... Read More

How do I make an array with unique elements (remove duplicates) - JavaScript?

AmitDiwan
Updated on 03-Oct-2020 15:07:15

136 Views

Let’s say the following is our array with duplicate elements −var duplicateNumbers = [10, 20, 100, 40, 20, 10, 100, 1000];We want the output to be −[10, 20, 100, 40, 1000];To display only the unique elements, use the concept of filter.ExampleFollowing is the code −var duplicateNumbers = [10, 20, 100, 40, 20, 10, 100, 1000]; console.log("With Duplicates Values="); console.log(duplicateNumbers); var noDuplicateNumbersArray = duplicateNumbers.filter(function (value, index, array) {     return array.indexOf(value) === index; } ); console.log("Without Duplicates Values=") console.log(noDuplicateNumbersArray);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo234.js.OutputThe output is as ... Read More

Comparing adjacent element and swap - JavaScript?

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

291 Views

This is the concept of Bubble Sort. It compares to adjacent element if it is lesser it will swap the value.ExampleFollowing is the code −var numbers = [10, 100, 30, 40, 90, 4, 91, 56, 78]; function bubbleSorting(numbers) {    for (var outer = 0; outer < numbers.length; outer++) {       for (var inner = 0; inner < numbers.length; inner++) {          if (numbers[outer] < numbers[inner]) {             var temp = numbers[outer];             numbers[outer] = numbers[inner];             numbers[inner] = temp; ... Read More

JavaScript: take every nth Element of Array and display a fixed number of values?

AmitDiwan
Updated on 03-Oct-2020 14:54:55

475 Views

For this, you can use for loop along with if condition. Let’s say the following is our array −var numbers = [1, 2, 34, 56, 78, 90, 100, 110, 40, 70, 67, 77, 34, 68, 89, 91, 94];We have set a counter to set a value. This value is set to display a fixed number of values −var counter = 6;The above shows the result will be 6 values.ExampleFollowing is the code −var numbers = [1, 2, 34, 56, 78, 90, 100, 110, 40, 70, 67, 77, 34, 68, 89, 91, 94]; var counter = 6; var newNumbers = []; var start = 0; for (var index = 0; index < numbers.length; index++) {    if (index % 2 != 0) {       start++;       if (start node demo231.js [ 2, 56, 90, 110, 70, 77 ]

Number prime test in JavaScript by creating a custom function?

AmitDiwan
Updated on 03-Oct-2020 14:52:27

107 Views

Create a custom function to test a number is prime or not in JavaScript.ExampleFollowing is the code −function checkNumberIsPrime(number) {    var flag = false;    for (start = 2; start < number / 2; start++) {       if (number % start === 0) {          flag = false;             break;       }       else {          flag = true;       }    }    return flag; } var number = 11; if (checkNumberIsPrime(number) == true) {    console.log("The number is prime"); } else {    console.log("The number is not prime"); }To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo230.js.OutputThe output is as follows −PS C:\Users\Amit\JavaScript-code> node demo230.js The number is prime

Display the result difference while using ceil() in JavaScript?

AmitDiwan
Updated on 03-Oct-2020 14:49:47

101 Views

The Math.ceil() is part of the Math object in JavaScript. Suppose, your value is 4.5 or 4.3, it will give the result 5.Let’s say the following are our values −var result1 = 98; var result2 = 5;Following is the code to display the result difference while using ceil() or not −var result1 = 98; var result2 = 5; console.log("The actual result is=" + result1 / result2); var output = Math.ceil(result1 / result2); console.log("The ceil result is=" + output);To run the above program, you need to use the following command −node fileName.js. Here, my file name is demo229.js.OutputThe output is as ... Read More

Remove/ filter duplicate records from array - JavaScript?

AmitDiwan
Updated on 03-Oct-2020 14:48:10

200 Views

Let’s say the following are our records with some duplicate values −var objectOfNationality =    [       { nationality: "Indian" },       { nationality: "American" },       { nationality: "Emirati" },       { nationality: "Indian" },       { nationality: "American" }    ];ExampleTo remove duplicate records, use the concept of Set().Following is the code −var objectOfNationality =    [       { nationality: "Indian" },       { nationality: "American" },       { nationality: "Emirati" },       { nationality: "Indian" },       ... Read More

How do I turn a string in dot notation into a nested object with a value – JavaScript?

AmitDiwan
Updated on 03-Oct-2020 14:45:22

4K+ Views

Let’s say the following is our string in dot notation −const keys = "details1.details2.details3.details4.details5"And the following is our array −const firsName = "David";To turn into a nested object, use the concept of split(‘.’) along with map().ExampleFollowing is the code −const keys = "details1.details2.details3.details4.details5" const firsName = "David"; var tempObject = {}; var container = tempObject; keys.split('.').map((k, i, values) => {    container = (container[k] = (i == values.length - 1 ? firsName : {})) }); console.log(JSON.stringify(tempObject, null, ' '));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo227.js.OutputThe output is as ... Read More

Advertisements