Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script>
- // 1. Write a function which will return these outputs.
- // f(1)(2)(3); // 9
- // f(2)(2)(1); // 4
- // f(2, 2, 1); // 4
- // f(); // 0
- function result(a, b, c) {
- if (a && b && c) {
- return (a + b) * c;
- } else if (a && b) {
- return function(val) {
- return (a + b) * val;
- }
- } else if (a) {
- return function(val1) {
- return function(val2) {
- return (a + val1) * val2;
- }
- }
- } else {
- return 0;
- }
- }
- console.log(result(1)(2)(3)); // 9
- console.log(result(2)(2)(1)); // 4
- console.log(result(2, 2, 1)); // 4
- console.log(result(2, 2)(2)); // 8
- console.log(result()); // 0
- // 2. let say an object is there. Convert into array which stores only the values of the object.
- // x = {
- // a: 1, // name: value pairs
- // b: 2
- //};
- const nameArr = [];
- const valArr = [];
- for(let i in x) {
- nameArr.push(i); // prints name
- valArr.push(x[i]); // prints value
- }
- console.log(nameArr); // ["a", "b"]
- console.log(valArr); // [1, 2]
- // 3 . Reverse a string.
- const reverseString = (str) => str.split('').reverse().join('');
- console.log(`Reverse of the string: ${reverseString('amrit')}`); // Reverse string: tirma
- // 4. Modify the code so that newObj.getA().getB(); should not return undefined
- const newObj = {
- a: 1,
- b: 2,
- getA() {
- console.log(this.a);
- return this; // add this line
- },
- getB() {
- console.log(this.b);
- }
- }
- newObj.getA().getB();
- // 5. Add all the elements of an array.
- let addArr = [1, 2, 5, 6, 8];
- let resultArr = addArr.reduce((acc, item) => acc + item, 0);
- console.log('Result array after reduce: ' + resultArr); // Result array after reduce: 22
- // 6. Show the output of the following.
- console.log([] + []); // empty string
- console.log([] + {}); // [object Object]
- console.log({} + {}); // [object Object][object Object]
- // 7. Write a program to find factorial.
- function factorial(number) {
- if (number <= 1) {
- return 1;
- }
- return number * factorial(number - 1);
- }
- document.write(factorial(6))
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement