Found 9312 Articles for Object Oriented Programming

Repeat String in JavaScript?

AmitDiwan
Updated on 27-Oct-2020 09:40:39

114 Views

To repeat string, you can use Array() along with join().ExampleFollowing is the code −            Document    String.prototype.counter = function (value) {       return new Array(value + 1).join(this);    }    console.log("The repeat string".counter(5)); 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.OutputThis will produce the following output: on console −

I'm trying to make an id searcher which does a thing when you input the right id. However, the JavaScript if statement always runs. How?

AmitDiwan
Updated on 27-Oct-2020 09:36:16

43 Views

If you will use equal operator(=) in if condition the if block will always be executed.You need to use == operator or === .ExampleFollowing is the code −var details = [    {       id: 10001,       name: "John"    },    {       id: 10002,       name: "Bob"    },    {       id: 10003,       name: "Carol"    },    {       id: 10004,       name: "David"    } ] var searchId = 10003; for (var index = 0; index < details.length; index++) {    if (details[index].id === searchId) {       console.log(details[index].id + " found");       break;    } }To run the above program, you need to use the below command −node fileName.js.Here, my file name is demo322.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo322.js 10003 found

Get the number of true/false values in an array using JavaScript?

AmitDiwan
Updated on 26-Oct-2020 11:25:52

1K+ Views

To get the number of true/ false values in an array, the code is as follows −var obj = [    {       isMarried: true    },    {       isMarried: false    },    {       isMarried: true    },    {       isMarried: true    },    {       isMarried: false    } ] function numberOfTrueValues(obj) {    var counter = 0;    for (var index = 0; index < obj.length; index++) {       if (obj[index].isMarried === true) {          counter++;     ... Read More

Understanding the find() method to search for a specific record in JavaScript?

AmitDiwan
Updated on 26-Oct-2020 11:15:22

357 Views

To fetch a specific record, use find() with some condition.ExampleFollowing is the code −var obj=[    {       studentId:101,       studentName:"John"    },    {       studentId:102,       studentName:"Bob"    },    {       studentId:103,       studentName:"David"    } ] const result = obj.find(    (o) =>    {       return o.studentId === 102    } ); console.log(result);To run the above program, you need to use the below command −node fileName.js.Here, my file name is demo315.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo315.js { studentId: 102, studentName: 'Bob' }

Array.prototype.fill() with object passes reference and not new instance in JavaScript?

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

132 Views

To fix this, you can use map() in JavaScript.The syntax is as follows −var anyVariableName= new Array(yourSize).fill().map(Object);ExampleFollowing is the code −var arrayOfObject = new Array(5).fill().map(Object); console.log(arrayOfObject);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo311.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo311.js [ {}, {}, {}, {}, {} ]

How to make a setInterval() stop after some time or after a number of actions in JavaScript?

AmitDiwan
Updated on 26-Oct-2020 11:06:41

1K+ Views

Use some conditions to stop after some time.The below code will stop in half a minute.ExampleFollowing is the code −            Document    var now = new Date().getTime();    var interval = setInterval(function () {       if (new Date().getTime() - now > 30000) {          clearInterval(interval);          return;       }       console.log("working");    }, 2000); 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 −

How to sort an array of integers correctly in JavaScript?

AmitDiwan
Updated on 26-Oct-2020 11:04:21

311 Views

To sort an array of integers, use sort() in JavaScript.ExampleFollowing is the code −var arrayOfIntegers = [67, 45, 98, 52]; arrayOfIntegers.sort(function (first, second) {    return first - second; }); console.log(arrayOfIntegers);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo310.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo310.js [ 45, 52, 67, 98 ]

WebDriver click() vs JavaScript click().

Debomita Bhattacharjee
Updated on 26-Oct-2020 06:53:23

519 Views

We can click a link with the webdriver click and Javascript click. For the Selenium webdriver click of a link we can use link text and partial link text locator. We can use the methods driver.findElement(By.linkText()) and driver.findElement(By.partialLinkText()) to click.The links in an html code are enclosed in an anchor tag. The link text enclosed within the anchor tag is passed as argument to the driver.findElement(By.linkText()) method. The partial matching link text enclosed within the anchor tag is passed as argument to the driver.findElement(By.partialLinkText()) method. Finally to click on the link the click method is used.Let us see the html ... Read More

Get price value from span tag and append it inside a div after multiplying with a number in JavaScript?

AmitDiwan
Updated on 24-Oct-2020 12:33:33

1K+ Views

Extract the value and convert it from String to integer to get price value from span tag.ExampleFollowing is the code −            Document           Value=                10                    $(document).ready(function () {       var v = parseInt($(".purchase.amount")       .text()       .trim()       .replace(', ', ''));       var totalValue = v * 10;       console.log(totalValue);   ... Read More

Removing listener from inside outer function in JavaScript?

AmitDiwan
Updated on 24-Oct-2020 12:28:41

60 Views

To remove listener from outer function, use removeEventListener().ExampleFollowing is the code − Document Press Me    var demoId = document.getElementById('demo');    demoId.addEventListener('click', function fun() {       outerFunction(this, fun);    }, false);    function outerFunction(self, funct) {       console.log('outer function is called....');       self.removeEventListener('click', funct, false);       console.log("Listener has been removed...")    } 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 ... Read More

Advertisements