Creating a Function that Returns Data

Hello Kirupa,

I am referring your book on Absolute Beginner’s Guide to JavaScript
Chapter 3 - Functions
I’m doing exactly the same as per instructions given in the book but I am not getting any result when I go live on visual studio code. there is no dialog box.
Please help me.
Here is my code from VS

<!DOCTYPE html>
<html lang="en">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Get Distance</title>

    function getDistance(speed, time) {
      let distance = speed * time;
      return distance;
    let myDistance = getDistance(10, 5);
1 Like

Hi @Anand_Singh1 - in the way I described the code, I was unclear in my expectation. That’s a bug on my end.

The myDistance variable correctly stores the returned value from the getDistance function. To display the result, add an alert(myDistance) at the end:

let myDistance = getDistance(10, 5);
alert(myDistance); // <-- new line to add

Hopefully this helps! I’ll revise this section in a future edition :slight_smile:


Thank you so much Kirupa!! You’re the best!!

1 Like

This a really great post