find the power
of a number
[ for loop ]

Find the power of a number

Write a function that takes two numbers, the first number representing the base number and the second representing the exponent.

The power of a number is the result of raising the base to the power of the exponent.

base → number that is being multiplied.

exponent → number that indicates how many times the base is multiplied by itself.


Example ...

Find 2 to the power of 3

23 2 × 2 × 2 8

Find 3 to the power of 4

34 3 × 3 × 3 × 3 81

Numbers are used to represent both integer and floating-point values.

Numbers are most commonly expressed in literal forms like 255 or 3.14159 ↴

let num1 = 5; → number

let num2 = 2.5; → number

let num3 = num1 + num2;

console.log(num3); returns ↴

7.5 → number


Exponentiation

Exponentiation is the mathematical process of multiplying a number by itself, raised to the power of another number.

base exponent

base number to multiply.

exponent number of times to multiply the base by itself.


Find the power of a number using a for loop


for loop repeatedly executes a block of code until a specified condition evaluates to false.

The loop runs a block of code a set number of times, defined by an initialization, a condition, and an increment.

for (let x = 0; x < 4; x++) {

console.log(x);

}

Loop variable x is initialized to 0

Condition x < 4 is checked before each iteration.

The loop will continue to run as long as x is less than 4

The loop repeatedly executes a block of code 4 times, from 0 to 3

For each iteration of the loop, the current value of x is printed to the console.

After each iteration, x is incremented by 1 x++

When x reaches 4 the condition evaluates to false, terminating the loop.

0

1

2

3 → printed to console


Initialize a variable to hold the base value.

const num1 = 3; → user input

Initialize a variable to hold the exponent value.

const num2 = 4; → user input


Define a function findPower to find the power of a number.

function findPower(str) {}

The function takes two numbers as input num1, num2 representing the base and exponent and returns the result of raising the base to the power of the exponent.

Check if the exponent is negative.

if (exp < 0) {

Return error message for negative exponent and end execution of function.

return "Exponent cannot be negative"

Initialize result variable to 1

let num = 1 num

If exponent 0 or positive, loop to multiply the base by itself exp times.

for (let x = 0; x < exp; x++) {

Multiply the current result by the base.

num = num * base

Return the final result.

return num


Call the function with ↴

findPower(num1, num2;


Find 3 to the power of 4 34

base 3

exponent 4

Loop to multiply the base 3 by itself exponent 4 times.

for (let x = 0; x < exponent; x++) {

num = num * base; multiply the current result by the base

};

Iteration ↴

0 1 × 3 3

1 3 × 3 9

2 9 × 3 27

3 27 × 3 81

34 81


Find 3 to the power of 4 34

const num1 = 3;

const num2 = 4;

function findPower(base, exp) {

if (exp < 0) {

return "Exponent cannot be negative";

}

let num = 1;

for (let x = 0; x < exp; x++) {

num = num * base;

}

return num;

}

call function

findPower(num1, num2); returns ↴

81

Find the power of a number

Base
Exponent