0% found this document useful (0 votes)
48 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)
48 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
You are on page 1/ 5

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?`;

console.log(generateGreeting("Alice"));
console.log(generateGreeting("Bob"));
console.log(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 = document.getElementById('income')


var wealthEl = document.getElementById('wealth')
var taxEl = document.getElementById('tax');

function calculate() {
var incomeTax = 0.35 * incomeEl.value;
var wealthTax = 0.25 * wealthEl.value;
var tax = incomeTax + wealthTax;

// round with 2 decimal places


taxEl.value = Math.round(tax * 100) / 100;
}

incomeEl.addEventListener('input', calculate);
wealthEl.addEventListener('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);


console.log("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);
console.log(`The factorial of ${num} is ${result}`);
}
else {
console.log('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

}
}

} console.log(sum(1)(2)(3)) // 6
//SEPERATE SUM--

const sum1 = sum(1);

const sum2 = sum1(2);


const result = sum2(3);

console.log(result); // 6

You might also like