Intro to objects II - 26. Passing Arguments


#1

The code bellow works well...
But when I change:
===> var myBalance = john.askTeller(1234);

to
===> var myBalance = john.askTeller(prompt("Type the correct pass to see the Bank Balance", "type here"));

I put prompt for asking data from user, but it doesn't run, could you guys help me?

===>Full code bellow:

function Person(first,last,age) {
this.firstname = first;
this.lastname = last;
this.age = age;
var bankBalance = 7500;

this.askTeller = function(pass) {
if (pass == 1234) return bankBalance;
else return "Wrong password.";
};
}

var john = new Person('John','Smith',30);
/* the variable myBalance should access askTeller()
with a password as an argument */
var myBalance = john.askTeller(1234);

/*
var myBalance = john.askTeller(prompt("Type the correct pass to see the Bank Balance", "type here"));
*/


#2

I've tried this:
var myBalance = john.askTeller(parseFloat(prompt("Type the correc pass to see the Bank Balance","type here")));

And this:
var myBalance = john.askTeller(Number(prompt("Type the correc pass to see the Bank Balance","type here")));


#3

@marcelvip,

function Person(first,last,age) {
    this.firstname = first;
    this.lastname = last;
    this.age = age;
    var bankBalance = 7500;

    this.askTeller = function(pass) {
         if (pass == 1234) return bankBalance;
         else return "Wrong password.";
    };
}

var john = new Person('John','Smith',30);
/* the variable myBalance should access askTeller()
 with a password as an argument */
/*
var myBalance = john.askTeller(1234);

*/ 
var myBalance = john.askTeller(prompt(
        "Type the correct pass to see the Bank Balance", "type here"));
console.log( myBalance );

Concerning the usage of the IF ELSE without a code-block encapsulation
is dangerous......
Please read Javascript code-convention
http://javascript.crockford.com/code.html


#4

Thank you you LeonHard.

The modification you show me almost run completely, but the console didn't accept.

However the screen output show us the correct answer
Output:
7500

message:
"Ops, try again:.
Make sure to set myBalance equal to calling john's askTeller method with 1234 as the argument"


#5

@marcelvip,
Modified the Original-Answer-Post...so sorry!!


#6

I must do something like this?

var x = prompt("Enter a Value","0");
var y = prompt("Enter a Value", "0");
var num1 = parseInt(x);
var num2 = parseInt(y);

http://stackoverflow.com/questions/17907455/how-to-get-numeric-value-from-a-prompt-box


#7

@marcelvip,

In Javascript you have a

  • strict comparison using === with which a "1" === 1 will render to false
  • type–converting comparison using == with which a "1" == 1 will render to true

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators


#8

thank you so much! you helped me a lot


#9

I have the same code than you at the beginning, marcelvip, and I didn´t understand what do I have to do, because I don´t speak english very well (I speak spanish), can you tell me how does your code look now? Thanks!