10 problems with answer in JAVASCRIPT

Sahil Imrose Zahin
2 min readMay 8, 2021
  1. what a callback function is

A callback function is a function that is passed to another function as an argument and is executed after some operation has been completed.

function modifyArray(arr, callback) {
// do something to arr here
arr.push(100);
// then execute the callback function that was passed
callback();
}

var arr = [1, 2, 3, 4, 5];

modifyArray(arr, function() {
console.log("array has been modified", arr);
});

2. Given a string, reverse each word in the sentence

Problem:

For example: Javascript should be become tpircsavaJ

var string = "Welcome to this Javascript Guide!";

// Output becomes !ediuG tpircsavaJ siht ot emocleW
var reverseEntireSentence = reverseBySeparator(string, "");

// Output becomes emocleW ot siht tpircsavaJ !ediuG
var reverseEachWord = reverseBySeparator(reverseEntireSentence, " ");

function reverseBySeparator(string, separator) {
return string.split(separator).reverse().join(separator);
}

3. How would you check if a number is an integer in Javascprict?

function isInt(num) {
return num % 1 === 0;
}

console.log(isInt(4)); // true
console.log(isInt(12.2)); // false
console.log(isInt(0.3)); // false

4. Make this work

Problem:

duplicate([1, 2, 3, 4, 5]); // [1,2,3,4,5,1,2,3,4,5]function duplicate(arr) {
return arr.concat(arr);
}

duplicate([1, 2, 3, 4, 5]); // [1,2,3,4,5,1,2,3,4,5]

5. Write a function that would allow you to do this?

Problem:

var addSix = createBase(6);
addSix(10); // returns 16
addSix(21); // returns 27
function createBase(baseNumber) {
return function(N) {
// we are referencing baseNumber here even though it was declared
// outside of this function. Closures allow us to do this in JavaScript
return baseNumber + N;
}
}

var addSix = createBase(6);
addSix(10);
addSix(21);

6. FizzBuzz Challenge

Problem:

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

7. Write a function that would allow you to do this

Problem:

 multiply(5)(6);

8. What will the following code output?

Problem:

(function() {
var a = b = 5;
})();

console.log(b);
var a = b;
b = 5;

9. Provide some examples of non-bulean value coercion to a boolean one

  • "" (empty string)
  • 0, -0, NaN (invalid number)
  • null, undefined
  • false

Any value that’s not on this “falsy” list is “truthy.” Here are some examples of those:

  • "hello"
  • 42
  • true
  • [ ], [ 1, "2", 3 ] (arrays)
  • { }, { a: 42 } (objects)
  • function foo() { .. } (functions)

10. Write a function that would allow you to do this

Problem:

multiply(5)(6);function multiply(a) {
return function(b) {
return a * b;
}
}

multiply(5)(6);

--

--