Logical Approach to Common Problems — Part 2

📄 Table of contents

➤ Misc

Write a JavaScript program to find the most frequent item of an array. Search should be case insensitive.input: [1, 'T', 'a', 'T', 2, 3, 'T', 3, 't', 2, 't', 9, 3];
output: t = 5
Attach a function to array in js?
let tempArray = [6, 2, 3, 4, 5];
let averageValues = tempArray.average();
console.log(averageValues); // should return average of tempArray

◉ sum of no.s using Recursion

var array_sum = function(arr) {
if (arr.length === 1) {
return arr[0];
}
return arr.pop() + array_sum(arr);

};
console.log(array_sum([1,2,3,4,5,6]));# 21
// functional approach
const log = message => console.log(message);
const increaseByOne = num => num + 1;const num = 1
const newNum = increaseByOne(num) // => 2
console.log(num) // => 1
const sum = (a,b) => a + b
const average = (fn, ...nums) => nums.reduce(fn) / nums.length;
const numsAvg = average(sum, 1, 2, 3) // => 2

◉ sum of (numbers) when you don’t know how many numbers are there…rest operator.

const sum = (...args)=>{
return args.reduce((a, b) => a+b);
}
console.log(sum(2,3,4,6));
# 15

(…) Rest parameter syntax

function numbers(x,y, ...z){
console.log(x,y,z);
}
numbers(1,2,3,4,5,6);
# Output:
1
2
[3,4,5,6]

(…) Spread parameter syntax

function getSum(x, y, z){
console.log(x+y+z);
}
let arr = [10,20,30];
getSum(...sumArray);

Q. increment the counter on each page load

◉ Chaining in JS (as in jQuery)

◉ Get all possible substrings of a given string

all possible substrings of a given string

◉ Find a value in an array of objects in JS

sum(10)(2)(3)(4)()

Sum using Recursion

Don’t Use If-Else and Switch in JavaScript, Use Object Literals

◉ “this” keyword in js

4. Binding this

◉ “this”

var foo = {
name: "anil",
age: 30
};
function getDetails() {
return (this.name + " is + this.age + " yrs old.");
}
foo.logMyDetails = getDetails;
console.log(foo.logMyDetails());
//anil is 30 yrs old.
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
function Employee(name) {
this.name = name;
this.getName = function () {
return this.name;
}
}
var e1 = new Employee(“anil");
e1.getName();
#anil
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
function Employee(name) {
this.name = name;
}
Employee.getName = function () {
return this.name;
}
var e2 = new Employee("kumar");
e2.getName(); #TypeError
var e3 = e2.getName;
console.log(e3()); #TypeError
var e4 = e2.getName.bind(e2);
console.log(e3()); #TypeError ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
var x = "outermost";
var foo = {
x: "inner",
baz: {
x: "innermost",
bar: function () {
return this.x;
}
}
}
var go = foo.baz.bar; # "this" refers to global object
console.log(go());
console.log(foo.baz.bar())# outermost
# innermost

◉ What happens to ‘this’ in a function nested inside a method of an object?

◉ `this` inside Classes

Getting key with the highest value from object

var obj = {a: 1, b: 2, undefined: 1};Object.keys(obj).reduce(function(a, b){ return obj[a] > obj[b] ? a : b });

Q. find the most frequent item in an array

Write a JavaScript program to find the most frequent item of an array. Search should be case insensitive.input: [1, 'T', 'a', 'T', 2, 3, 'T', 3, 't', 2, 't', 9, 3];
output: t = 5

Add a method to Array object in Javascript?

var arr = [2,3,4,5];arr.getSum = function() { 
var sum= 0;
for(var i=0; i< arr.length; i++){
sum = sum + arr[i];
};
console.log(sum);
}
arr.getSum();

Chaining ( as in jQuery)

Experience with Front-end Technologies and MERN / MEAN Stack. Working on all Major UI Frameworks like React, Angular.