Find the Sum of Radio Button Values using jQuery

AmitDiwan
Updated on 09-Nov-2020 11:15:35

505 Views

Let’s say we have marks records and we need to sum them. The records are displayed in Radio Button −Marks1 75 {          if (evnt.checked) {             total = total + parseInt(evnt.value);             return;          }       });       secondMark.forEach((evnt) => {          if (evnt.checked) {             total = total + parseInt(evnt.value);             return;          }       });       ... Read More

Merge Specific Elements Inside an Array Together in JavaScript

AmitDiwan
Updated on 09-Nov-2020 11:11:59

131 Views

Let’s say the following is our array −var values = [7,5,3,8,9,'/',9,5,8,2,'/',3,4,8];To merge specific elements, use map along with split().ExampleFollowing is the code −var values = [7,5,3,8,9,'/',9,5,8,2,'/',3,4,8]; var afterMerge = values.join('') .split(/(\d+)/). filter(Boolean). map(v => isNaN(v) ? v : +v); console.log(afterMerge);To run the above program, you need to use the following command −node fileName.js. Here, my file name is demo301.js.OutputThis will produce the following output on console −PS C:\Users\Amit\javascript-code> node demo301.js [ 75389, '/', 9582, '/', 348 ]

Get the Correct Century from 2-Digit Year Date Value in JavaScript

AmitDiwan
Updated on 09-Nov-2020 11:10:40

338 Views

For this, you can use ternary operator based on some condition.ExampleFollowing is the code −const yearRangeValue = 18; const getCorrectCentury = dateValues => {    var [date, month, year] = dateValues.split("-");    var originalYear = +year > yearRangeValue ? "20" + year : "18" + year;    return new Date(date + "-" + month + "-" + originalYear).toLocaleDateString('en-GB') }; console.log(getCorrectCentury('10-JAN-19'));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo300.js.OutputThis will produce the following output on console −PS C:\Users\Amit\javascript-code> node demo300.js 1/10/2019

Get Keyword Before a Function in a Class - JavaScript

AmitDiwan
Updated on 09-Nov-2020 11:08:56

465 Views

The get keyword can be used as a getter function like C#, Java and other technologies.We set a function with get like the following in a class −class Employee {    constructor(name) {    this.name = name;    }    get fullName() {       return this.name;    } }ExampleFollowing is the code displaying an example of get −class Employee {    constructor(name) {       this.name = name;    }    get fullName() {       return this.name;    } } var employeeObject = new Employee("David Miller"); console.log(employeeObject.fullName);To run the above program, you need to use ... Read More

Matrix Multiplication Result of Two 2D Arrays in JavaScript

AmitDiwan
Updated on 09-Nov-2020 11:06:59

371 Views

We are required to write a JavaScript function that takes in two 2-D arrays of numbers and returns their matrix multiplication result.Let’s say the following are our two matrices −// 5 x 4 let a = [    [1, 2, 3, 1],    [4, 5, 6, 1],    [7, 8, 9, 1],    [1, 1, 1, 1],    [5, 7, 2, 6] ]; // 4 x 6 let b = [    [1, 4, 7, 3, 4, 6],    [2, 5, 8, 7, 3, 2],    [3, 6, 9, 6, 7, 8],    [1, 1, 1, 2, 3, 6] ];ExampleLet’s ... Read More

Return a Split Array of String Based on Specified Separators in JavaScript

AmitDiwan
Updated on 09-Nov-2020 11:03:56

145 Views

We are required to write a JavaScript function that takes in a string and any number of characters specified as separators. Our function should return a splitted array of the string based on all the separators specified.For example −If the string is −const str = 'rttt.trt/trfd/trtr, tr';And the separators are −const sep = ['/', '.', ', '];Then the output should be −const output = [ 'rttt', 'trt', 'trfd', 'trtr' ];ExampleFollowing is the code −const str = 'rttt.trt/trfd/trtr, tr'; const splitMultiple = (str, ...separator) => {    const res = [];    let start = 0;    for(let i = 0; ... Read More

Improve File Reading Performance in Python with mmap Function

Kiran P
Updated on 09-Nov-2020 11:02:12

804 Views

Introduction..MMAP abbreviated as memory mapping when mapped to a file uses the operating systems virtual memory to access the data on the file system directly, instead of accessing the data with the normal I/O functions. There by improving the I/O performance as it does not require either making a separate system call for each access or copying data between buffers.To a matter of fact anything in memory for instance a SQLlite database when created in-memeory has better performance compared to on disk.Memory-mapped files can be treated as mutable strings or file-like objects, depending on what you want to do.MMAP supports ... Read More

Find Element Appearing Odd Times in JavaScript

AmitDiwan
Updated on 09-Nov-2020 11:01:20

371 Views

Given an array of integers, we are required to write a function that takes this array and finds the one element that appears an odd number of times. There will always be only one integer that appears an odd number of times.We will approach this problem by sorting the array. Once sorted, we can iterate over the array to pick the element that appears for odd number of times.ExampleFollowing is the code −const arr = [20, 1, -1, 2, -2, 3, 3, 5, 5, 1, 2, 4, 20, 4, -1, -2, 5]; const findOdd = arr => {    let ... Read More

Read Text Files Using Linecache in Python

Kiran P
Updated on 09-Nov-2020 10:59:40

591 Views

Solution..The linecache module implements cache which holds the contents of files, parsed into separate lines, in memory. linecache module returns line/s by indexing into a list, and saves time over repeatedly reading the file and parsing lines to find the one desired.lincecache module is very useful when looking for multiple lines from the same file.Prepare test data. You can get this text by just using Google and searching for sample text.Lorem ipsum dolor sit amet, causae apeirian ea his, duo cu congue prodesset. Ut epicuri invenire duo, novum ridens eu has, in natum meliore noluisse sea. Has ei stet explicari. ... Read More

Compare Files in Python

Kiran P
Updated on 09-Nov-2020 10:57:07

9K+ Views

Problem.You need to compare files in Python.Solution..The filecmp module in python can be used to compare files and directories. 1.cmp(file1, file2[, shallow])filecmp Compares the files file1 and file2 and returns True if identical, False if not. By default, files that have identical attributes as returned by os.stat() are considered to be equal. If shallow is not provided (or is True), files that have the same stat signature are considered equal.cmpfiles(dir1, dir2, common[, shallow])Compares the contents of the files contained in the list common in the two directories dir1 and dir2. cmpfiles returns a tuple containing three lists - match, mismatch, ... Read More

Advertisements