Jump to content

Complete noob. Need help figuring out a javascript code.


adventurouscoderr

Recommended Posts

Hello everyone, I've been studying programming over the summer. I've been reading the the head first books javascript programming to learn javascript if anyone is familiar with the book. I need help figuring out how this code works.

The book shows an example of multple properties and a function together like this

 

<!doctype html><html lang="en"><head><title>Prequalifcation</title><meta charset="utf-8"><script>var chevy = { make: "Chevy", model: "Bel Air", year: 1957, color: "red", passengers: 2, convertible: false, mileage: 1021};var fiat = { make: "Fiat", model: "500", year: 1957, color: "Medium Blue", passengers: 2, convertible: false, mileage: 88000};var cadi = { make: "GM", model: "Cadillac", year: 1955, color: "tan", passengers: 5, convertible: false, mileage: 12892};var taxi = { make: "WebVille Motors", model: "Taxi", year: 1955, color: "yellow", passengers: 4, convertible: false, mileage: 281341};function prequal(car) { if (car.mileage > 10000) { return false; } else if (car.year > 1960) { return false; } return true;}var worthALook = prequal(taxi);if (worthALook) { console.log("You gotta check out this " + taxi.make + " " + taxi.model);} else { console.log("You should really pass on the " + taxi.make + " " + taxi.model);}//// add a function to make it easier to generate the console output!//function isWorthALook(didQualify, car) { if (didQualify) { console.log("You gotta check out this " + car.make + " " + car.model); } else { console.log("You should really pass on the " + car.make + " " + car.model); }}isWorthALook(prequal(taxi), taxi);isWorthALook(prequal(cadi), cadi);isWorthALook(prequal(chevy), chevy);isWorthALook(prequal(fiat), fiat);</script></head><body></body></html>

 

I know the code works, but how does it work? I understand the variable prequal is getting passed an agruement from the taxi Object. Then the prequal function is called which then the agruement is passed to the taxi parameter and does its magic. Then the worthALook is passed onto the if loop and the info is passed onto the console. Where I get confused at is the isWorthALook function and is isWorthALook calls below it. Does the prequal inside the parethesis of the isWorthALook have to do anything with the var prequal? How does this code work? Any help is appreciated.

Link to comment
Share on other sites

 

 

Does the prequal inside the parethesis of the isWorthALook have to do anything with the var prequal?

 

When you call the isWorthALook function with it's first argument passing the prequal function where as the the argument for the prequal function is an object of one of the car objects like taxi, didQualify becomes true or false depending on what's returned from the prequal function. If didQualify is true, you get the console.log of: console.log("You gotta check out this " + car.make + " " + car.model); If not true, you get console.log: console.log("You should really pass on the " + car.make + " " + car.model);

 

Hopefully this helps.

Link to comment
Share on other sites

<script>function getanumber(){return Math.floor(101 * Math.random() - 50);}function positive(i){if (i>=0){return {x:i,pos:true};}else{return {x:i,pos:false};}}function even(o){if (o.x%2==0){return{x:o.x,pos:o.pos,even:true};}else{return{x:o.x,pos:o.pos,even:false};}}function test(o){alert(o.x +' is '+ (o.pos ? 'positive':'negative') +' and '+ (o.even ? 'even':'odd'));}while(true){test(even(positive(getanumber())));}</script>
Link to comment
Share on other sites

Just some clarification on terminology:

I understand the variable prequal is getting passed an agruement from the taxi Object.

prequal is not a variable, it is a function. It gets the entire object passed to it (or at least that's what it expects, it doesn't actually do any error checking).

Then the prequal function is called which then the agruement is passed to the taxi parameter and does its magic.

The taxi variable is passed as a parameter to the prequal function.

Then the worthALook is passed onto the if loop

If is not a loop, it is a statement. worthALook is not passed to the if statement, things are only passed to functions, not statements. The worthALook variable is evaluated by the if statement to determine if it is true or false.

Does the prequal inside the parethesis of the isWorthALook have to do anything with the var prequal?

Again, prequal is not a variable, it is a function. An identifier can only refer to a single thing. If you have a variable called "test", and then you define a function called "test", the function overwrites the variable. The identifier "test" now refers to the function, the old variable with that name is no longer available.This line:isWorthALook(prequal(taxi), taxi);has 2 function calls, it calls isWorthALook and prequal. When that actually gets executed, execution goes from right to left. taxi is just a variable, it doesn't need to evaluate that, so it goes to the next argument to isWorthALook, which is prequal(taxi). So it passes the taxi object to the prequal function, which returns a boolean true/false value. That boolean value is what gets passed to isWorthALook. It doesn't pass the function, it passes the return value after the function is executed.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...