Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doing Math in Multiple Javascript Functions

Tags:

javascript

I'm new to the Javascript world and trying to figure out this assignment my teacher assigned. Here is his description of what is expected:

  1. Build a function that will start the program. Please call it start()

  2. From the start() function, call a function called getValue()

  3. The getValue() function will get a number from the user that will be squared.

  4. Also from the start function, call a function called makeSquare()

  5. The makeSquare() function will square the number that was received by the user in the getValue() function.

  6. Make sure that you display the results of squaring the number inside of the makeSquare() function.

Here is what I have so far:

function start() {

    getValue();
    getSquare();
}

function getValue() {

    var a = prompt("Number please")
}

function getSquare() {

    var b = Math.pow(a)
    document.write(b)
}

start()

This assignment doesn't have to be working with any HTML tags. I've only got the prompt box to work, nothing else does though. Am I using variables in a way that can't be used?

like image 606
user1725798 Avatar asked May 18 '26 22:05

user1725798


1 Answers

You were close. But it seems that you don't understand scoping and how exactly to use the pow function.

Math.pow:

Math.pow takes two parameters, the base and the exponent. In your example, you only provide the base. That will cause problems as the function will return the value undefined and set it to b. This is how it should have looked (if you wanted to square it):

Math.pow(a, 2);

Scoping:

Every function has it's own scope. You can access other variables and functions created outside the function from within the function. But you cannot access functions and variables created inside another function. Take the following example:

var c = 5;

function foo() { // we have our own scope
   var a = c; // Okay
}

var b = a; // NOT okay. a is gone after the function exits.

We could say that a function is private. The only exception is that we can return a value from a function. We return values using the return keyword. The expression next to it is the return-value of the function:

function foo() {
    return 5;
}

var a = foo(); // a === 5

foo() not only calls the function, but returns its return-value. A function with no return-value specified has a return value of undefined. Anyway, in your example you do this:

function getValue() {
    var a = prompt("Number please")
}

and access it like this:

// ...
var b = Math.pow(a)

Do you see the error now? a is defined in the function, so it can't be accessed outside of it.

This should be the revised code (Note: always use semicolons. I included them in for you where necessary):

function start() {
    getSquare();
}

function getValue() {

    var a = prompt("Number please");
    return a;
}

function getSquare() {

    var b = Math.pow(getValue(), 2); // getValue() -> a -> prompt(...)
    document.write(b);
}

start();
like image 177
David G Avatar answered May 21 '26 11:05

David G



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!