w3resource

CoffeeScript function: Compute the value of bn where n is the exponent and b is the bases

CoffeeScript Function : Exercise-19 with Solution

Write a CoffeeScript function to compute the value of bn where n is the exponent and b is the bases. Accept b and n from the user and display the result.

HTML Code :


<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <script src="//jashkenas.github.io/coffee-script/extras/coffee-script.js"></script>
  <title>Compute the value of bn where n is the exponent and b is the bases</title>
</head>
<body>

</body>
</html>

CoffeeScript Code :


ub = Number(prompt('Input a base number.', '0'))
un = Number(prompt('Input an exponent.', '0'))

exp = (b, n) ->
  ans = 1
  i = 1
  while i <= n
    ans = b * ans
    i++
  ans

console.log exp(ub, un)

Sample Output:

If base=2, exponent=4 then
16

Live Demo :

See the Pen coffeescript-exercise-19 by w3resource (@w3resource) on CodePen.


Improve this sample solution and post your code through Disqus.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/coffeescript-exercises/coffeescript-exercise-19.php