Logical Approach to Common Problems — Part 2

📄 Table of contents

➤ Misc

➤ Strings

◉ 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 in advance…rest operator.

const sum = (...args)=>{
return args.reduce((a, b) => a+b);
// args.length will return '4' here;
}
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(...arr);

Q. increment the counter on each page load

◉ Chaining in JS (as in jQuery)

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
});

◉ 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

◉ Count the occurrence of each character in a string

var str = "count number of each char in this string";var obj = {};// Misc vars
var ch, index, len, count;
for (index = 0, len = str.length; index < len; ++index) {
ch = str.charAt(index);
count = obj[ch];
console.log("count =>"+ count); // "count =>undefined"
obj[ch] = count ? count + 1 : 1;
}
for (ch in obj) {
console.log(ch + " ==>: " + obj[ch]);
}

◉ reverse a “string” using inbuilt methods

function reverseString(str) {
return str.split("").reverse().join("");
}
reverseString("hello");

◉ reverse an INTEGER using inbuilt methods

let myNumber = 23;
let myChar = myNumber.toString().split('').reverse().join('');
console.log(myChar) // returns 32;
console.log(typeof(myChar)) // returns string
const myNewNumber = parseInt(myChar);console.log(myNewNumber); // returns 32
console.log(typeof(myNewNumber)) // returns number

◉ 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

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();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

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