0% found this document useful (0 votes)
84 views5 pages

Promise Based Function

Uploaded by

shubhangiv05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views5 pages

Promise Based Function

Uploaded by

shubhangiv05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Assignment-19

1. Create an arrow function called square that takes a number as an


argument and returns its square. Use the arrow function to
calculate the square of a given number and display the result.
ANS.

function sq(n){
var res=n;
for(i=1;i<n;i++)
res+=n;
return res;
}

2. Create a JavaScript function called generateGreeting that takes a


name as an argument and returns a personalized greeting message.
Use this function to greet three different people.
ANS.

function generateGreeting(name) {
return `Hello, ${name}! How are you today?`;

[Link](generateGreeting("Alice"));
[Link](generateGreeting("Bob"));
[Link](generateGreeting("Charlie"));

3. Create an IIFE (Immediately Invoked Function Expression) that


calculates the square of a number and immediately displays the
result.
ANS. console. var result = num * num;
console. log("The square of", num, "is", result);

4. Write a JavaScript function called calculate Tax that takes an


income as an argument and returns the amount of tax to be paid.
Use a closure to handle different tax rates based on income ranges.
Test the function with various incomes.
ANS.

var incomeEl = [Link]('income')


var wealthEl = [Link]('wealth')
var taxEl = [Link]('tax');

function calculate() {
var incomeTax = 0.35 * [Link];
var wealthTax = 0.25 * [Link];
var tax = incomeTax + wealthTax;

// round with 2 decimal places


[Link] = [Link](tax * 100) / 100;
}

[Link]('input', calculate);
[Link]('input', calculate);

5. Write a JavaScript function called factorial that calculates the


factorial of a non-negative integer using recursion. Test the function
with different inputs.
ANS.
function factorial(n) {
if (n === 0 || n === 1) {

return 1;
} else {

return n * factorial(n - 1);


}

}
let num1 = 6;

let result = factorial(num1);


[Link]("The factorial of given number is :" + result);

ANOTHER EXAMPLE---

// program to find the factorial of a number


function factorial(x) {

// if number is 0
if (x == 0) {
return 1;
}

// if number is positive
else {
return x * factorial(x - 1);
}
}

// take input from the user


const num = prompt('Enter a positive number: ');
// calling factorial() if num is positive
if (num >= 0) {
const result = factorial(num);
[Link](`The factorial of ${num} is ${result}`);
}
else {
[Link]('Enter a positive number.');
}

6. Write a JavaScript function called curry that takes a function as an


argument and returns a curried version of that function. The curried
function should accept arguments one at a time and return a new
function until all arguments are provided. Then, it should execute
the original function with all arguments. Test the curry function with
a function that adds two numbers.
ANS.

function sum(a) {
return (b) => {

return (c) => {


return a + b + c

}
}

} [Link](sum(1)(2)(3)) // 6
//SEPERATE SUM--

const sum1 = sum(1);

const sum2 = sum1(2);


const result = sum2(3);

[Link](result); // 6

You might also like