10 Fundamental Problem Solving Elements.

Md.Saiful Islam
5 min readMay 16, 2020

--

Credit:Jacob Ward

Today We will see 10 frequently used problem solving paradigm.Let’s get started —

1) Given a string, reverse each word in the sentence.

Sample input:

Welcome to this Javascript Guide!

Sample Output:

emocleW ot siht tpircsavaJ !ediuG

Solution:

var string = “Welcome to this Javascript Guide!”;var reverseEntireSentence = reverseBySeparator(string, “”);var reverseEachWord = reverseBySeparator(reverseEntireSentence, “ “);function reverseBySeparator(string, separator) {return string.split(separator).reverse().join(separator);}

When we split without any space it separates character by character and reverse function also reverse character by character and join function join separated character into a string.

But when we split with a space it separates word by word and reverse function also reverse word by word and join function join separated word into a string.

2) Write a “mul” function which will properly when invoked as below syntax.

console.log(mul(2)(3)(4)); // output : 24console.log(mul(4)(3)(4)); // output : 48

We can solve this problem by using closure. Closure is a function that takes another function as a parameter or return another function.

Solution:

function mul (x) {return function (y) {return function (z) {return x * y * z;};};}

Here mul function accept the first argument and return anonymous function which take the second parameter and return anonymous function which take the third parameter and return multiplication of arguments which is being passed in successive.

3) How to empty an array in JavaScript?

We can do it many way —

Method-1:

arrayList = [];

Above code will set the variable arrayList to a new empty array.

Method 2

arrayList.length = 0;

Above code will clear the existing array by setting its length to 0.

Method 3

arrayList.splice(0, arrayList.length);

Splice function updates not only the length of the array but also reference of the array too.

For instance,

var arrayList = ['a', 'b', 'c', 'd', 'e', 'f']; // Created arrayvar anotherArrayList = arrayList;  // Referenced arrayList by another variablearrayList.splice(0, arrayList.length); // Empty the array by setting length to 0console.log(anotherArrayList);

Method 4

while(arrayList.length) {arrayList.pop();}

Above implementation can also empty the array

4) How to check if an object is an array or not?

In modern browser, we can use isArray property of Array object.

Array.isArray(arrayList);

If we want we can use JQuery like as —

if($.isArray(arrayList)) {console.log('Array');} else {console.log('Not an array');}

5) How would you use a closure to create a private counter?

Answer:
You can create a function within an outer function (a closure) that allows you to update a private variable but the variable wouldn’t be accessible from outside the function without the use of a helper function.

function counter() {var _counter = 0;add: function(increment) { _counter += increment; },retrieve: function() { return ‘The counter is currently at: ‘ + _counter; }}}var c = counter();c.add(5);c.add(9);c.retrieve();

Here return an object with several functions that allow us to modify the private _counter variable. It shows error if we try to access the private variable like below _counter. That’s Why we use our counter function.

6) How would you check if a number is an integer?

Answer:
A very simply way to check if a number is a decimal or integer is to see if there is a remainder left when you divide by 1.

function isInt(num) {return num % 1 === 0;}console.log(isInt(4)); // trueconsole.log(isInt(12.2)); // falseconsole.log(isInt(0.3)); // false

We can also do it like as below –

function isInt(num) {return num == parseInt(num);}console.log(isInt(4)); // trueconsole.log(isInt(12.2)); // falseconsole.log(isInt(0.3)); // false

7) FizzBuzz Challenge

Create a for loop that iterates up to 100 while outputting "fizz" at multiples of 3, "buzz" at multiples of 5 and "fizzbuzz" at multiples of 3 and 5.

Answer:

for (let i = 1; i <= 100; i++) {let f = i % 3 == 0,b = i % 5 == 0;console.log(f ? (b ? 'FizzBuzz' : 'Fizz') : b ? 'Buzz' : i);}

Here first we check whether i is divided by 3 or 5, if f is true then check, is b also true or not, if b also that means i is divided by 3 as well as 5 and return ‘FizzBuzz’ but if b is false that means also f true and return ‘Fizz’ but if only b is true then return ‘Buzz’ otherwise return i only.

8) Given two strings, return true if they are anagrams of one another.

Answer:

Anagram is a word, phrase, or name formed by rearranging the letters of another.

Such as spar, formed from rasp.

var firstWord = "Mary";var secondWord = "Army";isAnagram(firstWord, secondWord); // truefunction isAnagram(first, second) {// For case insensitivityvar a = first.toLowerCase();var b = second.toLowerCase();a = a.split("").sort().join("");b = b.split("").sort().join("");return a === b;}

Here first split the strings then sort the strings, and join the resulting array to a string. Finally compare the results.

9) What will be the output of the following code?

var y = 1;if (function f() {}) {y += typeof f;}console.log(y);

Answer:
Above code would give output 1undefined. If condition statement evaluate using eval so eval(function f() {}) which return function f() {} which is true so inside if statement code execute. typeof f return undefined because if statement code execute at run time, so statement inside if condition evaluated at run time.

Rather we can do as —

var k = 1;if (1) {function foo() {};k += typeof foo;}console.log(k); // output 1function

10) When would you use the “bind” function?

Answer:
A good use of the bind function is when you have a particular function that you want to call with a specific this value. You can then use bind to pass a specific object to a function that uses a this reference.

function fullName() {return "Hello, this is " + this.first + " " + this.last;}console.log(fullName()); // => Hello this is undefined undefined// create a person object and pass its values to the fullName functionvar person = {first: "Foo", last: "Bar"};console.log(fullName.bind(person)()); // => Hello this is Foo Bar

If we understand above this 10 problem. We can easily solve many problem using these concept. Hopefully we understood everything.

No more today. Thanks you😊.

--

--

No responses yet