in Education by
I wrote javascript code to solve the following problem >>> Write a function exponentiate that accepts two arguments: base (number) power (number) Exponentiate should return the result of raising the base by the power. Assume the power argument will always be an integer greater than or equal to zero. Don't forget that any number raised to the 0th power is equal to 1! function exponentiate (base, power) { if (power === 0){ return result = 1 } // while loop let count = 0 let result = 1; while (count < power){ result *= base count += 1 } return result } exponentiate(3, 0) But I get the following error: ReferenceError: result is not defined What is wrong with my code? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You're accessing result before it is defined. so you should move them at the top of function Here you should return value directly instead of assignment if (power === 0){ return result = 1 } function exponentiate (base, power) { let count = 0 let result = 1; if (power === 0){ return 1 } // while loop while (count < power){ result *= base count += 1 } return result } console.log(exponentiate(3, 0)) Run code snippetExpand snippet Simplest is using ** exponent operator const exp = (b,e)=>{ return b**e } console.log(exp(3,0)) console.log(exp(3,1)) console.log(exp(2,10)) Run code snippetExpand snippet

Related questions

0 votes
0 votes
0 votes
    If I use a while loop for my below code, it is not giving the desired output, but when i use for loop i am anle ... x += 1 print(Comm) Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    Rewrite the following loop using for loop: int i=0; while (i...
asked Dec 22, 2021 in Education by JackTerrance
0 votes
    How can I fix this issues while compiling release build in Android Studio 3.0 RC2 Error:Error: commons- ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I have a program that loops through the lines of a book to match some tags I've created indicating ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    When we choose for loop and how it is deffernt from while loop Select the correct answer from above options...
asked Dec 25, 2021 in Education by JackTerrance
0 votes
    WHILE…LOOP is used when we know the number of iterations of a loop. True False Select the correct answer from above options...
asked Nov 27, 2021 in Education by JackTerrance
0 votes
    _____________ is a pictorial representation of the sequence of steps to solve a particular problem is called: Algorithm ... the above Select the correct answer from above options...
asked Dec 25, 2021 in Education by JackTerrance
0 votes
0 votes
    The probabilities of A, B, C solving a problem are 1/3, 1/4 and 1/6, respectively. If all the three try ... one of them will solve it. Select the correct answer from above options...
asked Nov 16, 2021 in Education by JackTerrance
0 votes
0 votes
    A can solve 90% of the problems given in a book and B can solve 70%. What is the probability that at least ... random from the book? Select the correct answer from above options...
asked Nov 13, 2021 in Education by JackTerrance
...