Newbie Coders Chat

Announcements Posted on
How helpful is our apprenticeship zone? Have your say with our short survey 02-12-2016
    • Thread Starter
    Offline

    3
    ReputationRep:
    I'm stuck and Codecademy's Q&A forum link seems to be broken. Does anybody here know what I did wrong?
    Offline

    2
    ReputationRep:
    (Original post by Craghyrax)
    I'm stuck and Codecademy's Q&A forum link seems to be broken. Does anybody here know what I did wrong?
    fix:
    1) The console.log() statement needs to be terminated by closing parentheses and a semicolon
    2) The "food" string cannot have a quote because it's not a literal string in this case, but a passed-in argument

    Code:
    console.log("I want to eat " + " " + food);
    technical explanation:
    otherwise the Javascript engine will interpret everything after that line as a statement, which }; is not, which is why the error reads: "invalid or unexpected token" (Javascript divides commands into separate tokens).
    • Thread Starter
    Offline

    3
    ReputationRep:
    (Original post by 571122)
    fix:
    1) The console.log() statement needs to be terminated by closing parentheses and a semicolon
    2) The "food" string cannot have a quote because it's not a literal string in this case, but a passed-in argument

    Code:
    console.log("I want to eat " + " " + food);
    technical explanation:
    otherwise the Javascript engine will interpret everything after that line as a statement, which }; is not, which is why the error reads: "invalid or unexpected token" (Javascript divides commands into separate tokens).
    PRSOM thanks very much!
    • Thread Starter
    Offline

    3
    ReputationRep:

    I'm having difficulty fully understanding functions in JS. I understand what they are doing, but I don't understand why the syntax is the way that it is. For example:
    Code:
    var orangeCost = function(cost) {
        console.log(cost * 5);
    };
    orangeCost("5");
    To my mind, the last line should be: cost("5"); because I thought I told it that the function 'cost' will change, but that the rest of the code in curly brackets should stay the same.... Instead it looks like I told it that the function 'cost' will change and then went ahead and called the variable in the last line, not the function :confused:
    Know what I mean?
    Offline

    2
    ReputationRep:
    (Original post by Craghyrax)
    I'm having difficulty fully understanding functions in JS. I understand what they are doing, but I don't understand why the syntax is the way that it is. For example:
    Code:
    var orangeCost = function(cost) {
        console.log(cost * 5);
    };
    orangeCost("5");
    To my mind, the last line should be: cost("5"; because I thought I told it that the function 'cost' will change, but that the rest of the code in curly brackets should stay the same.... Instead it looks like I told it that the function 'cost' will change and then went ahead and called the variable in the last line, not the function :confused:
    Know what I mean?
    Let me see if I properly understand what you are asking.

    cost is just the argument that belongs to the named function with name orangeCost. The variable orangeCost now points to an area in memory that represents a function.
    At the last line, you are calling the orangeCost("5"; function, passing in the literal string "5" as a parameter because that is a statement (a statement ends with a semicolon). A result of that statement is that the function gets looked up in memory and executed (argument + function body, what's inside the braces) with the parameters that you gave it.
    So, cost is a placeholder, that sits there, waiting for a value to be assigned to it. When you call orangeCost("5"; you pass in the value 5 and that will be passed onto cost, so that cost now equals 5.
    Finally, it will read: console.log(5*5); which will be 25 as a result.
    Using the function name makes the Javascript engine look up that function in memory (RAM). Using the parentheses is a way to pass in parameters, so that if they get used, their values will be redirected and assigned to the function's argument definition. Somewhere in memory there is a place for functions and somewhere in there is a place for the function's arguments too.

    I hope this was a decent explanation. If not, feel free to inquiry me further on it.

    P.S.: it's not good practice to pass in a string where a number is expected, so "5" should be 5.
    P.P.S: I'm not a Javascript expert.

    in-depth discussion:
    Spoiler:
    Show
    orangeCost is a variable which, when you assign a function to a variable, it becomes what is called a first-class function. In C, a first-class function is a pointer to a function. You can call a first-class function as if it was a function by using its named reference (the variable name orangeCost in this case).

    The keyword function returns the function so that it can be stored as a first-class function. Everything between the curly braces is the function body. cost is the only argument. The argument will be replaced by the argument provided to the function call.cost will be replaced with the value of the argument in the function call. orangeCost with a parentheses call is a function call.This syntax is like this is mostly because almost everything in most modern language is inherited from C, it hasn't changed much.

    Javascript and PHP and written to simulate the way C used to work. They're not exactly the same but they're similar.To understand why the syntax is like that, you have to go back why C syntax is like that, not Javascript, because Javascript is super modern.
    Offline

    3
    ReputationRep:
    Incase you wanted to know even more exciting stuff about JS functions - Javascript has a few different ways of declaring a function.

    Code:
    function orangeCost(arg){
    }
    
    orangeCost(5);
    Is the most common way. That is called a function declaration. You've declared a function named orangeCost and are calling it passing '5' as the parameter.

    Code:
    var orangeCost = function(cost) {
        console.log(cost * 5);
    };
    orangeCost("5")
    The way you did it is called a 'function expression' which lets you declare a function as part of an expression - It is often used when you want to pass functions around as arguments.
    You will often see them used like
    Code:
    http.get("SomeUrl",function() {
    //do some stuff on success
    },function(){
    //do some stuff on failior
    }
    );
    Notice those functions don't have names that is because function expressions can be anonymous!

    One thing to watch out for with function expressions is where you position them.

    Code:
    doStuff(); 
    var doStuff = function(){};
    That code is invalid because doStuff() doesn't exist at the point it is called.

    Code:
    doStuff();
    function doStuff(){
    }
    Is valid because 'function declarations' are 'hoisted ' to the top by javascript whilst function expressions are not.
    Offline

    3
    ReputationRep:
    (Original post by INTit)
    Incase you wanted to know even more exciting stuff about JS functions - Javascript has a few different ways of declaring a function.
    Code:
    function orangeCost(arg){
    }
    
    orangeCost(5);
    Is the most common way. That is called a function declaration. You've declared a function named orangeCost and are calling it passing '5' as the parameter.
    Code:
    var orangeCost = function(cost) {
        console.log(cost * 5);
    };
    orangeCost("5";)
    The way you did it is called a 'function expression' which lets you declare a function as part of an expression - It is often used when you want to pass functions around as arguments.
    You will often see code like
    Code:
    http.get("SomeUrl",function(){
    //do some stuff on success
    },function(){
    //do some stuff on failior
    };
    Notice those functions don't have names that is because function expressions can be anonymous!

    One thing to watch out for with function expressions is where you position them.
    Code:
    doStuff(); 
    var doStuff = function(){};
    That code is invalid because doStuff() doesnt exist at the point it is called.
    Code:
    doStuff();
    function doStuff(){
    }
    Is valid because 'function' declarations are 'hoisted ' to the top by javascript whilst function expressions are not.
    This stuff isn't original to JS btw. C++ (and pretty much any other modern language) can have anonymous functions and function variables but without the god awful JS syntax. JS is only good for one thing and that's for using jQuery on HTML, apart from that, I ****ing hate it.
    Offline

    3
    ReputationRep:
    (Original post by BobBobson)
    This stuff isn't original to JS btw. C++ (and pretty much any other modern language) can have anonymous functions and function variables but without the god awful JS syntax. JS is only good for one thing and that's for using jQuery on HTML, apart from that, I ****ing hate it.
    Haha don't get me started. Notice how crag was already accidently doing weird stuff like passing numeric values as strings, doing math with it and getting away with it due to JS's dynamic typing and automagic type conversions.
    • Thread Starter
    Offline

    3
    ReputationRep:
    (Original post by 571122)
    Let me see if I properly understand what you are asking.

    cost is just the argument that belongs to the named function with name orangeCost. The variable orangeCost now points to an area in memory that represents a function.
    At the last line, you are calling the orangeCost("5"; function, passing in the literal string "5" as a parameter because that is a statement (a statement ends with a semicolon). A result of that statement is that the function gets looked up in memory and executed (argument + function body, what's inside the braces) with the parameters that you gave it.
    So, cost is a placeholder, that sits there, waiting for a value to be assigned to it. When you call orangeCost("5"; you pass in the value 5 and that will be passed onto cost, so that cost now equals 5.
    Finally, it will read: console.log(5*5); which will be 25 as a result.
    Using the function name makes the Javascript engine look up that function in memory (RAM). Using the parentheses is a way to pass in parameters, so that if they get used, their values will be redirected and assigned to the function's argument definition. Somewhere in memory there is a place for functions and somewhere in there is a place for the function's arguments too.

    I hope this was a decent explanation. If not, feel free to inquiry me further on it.

    P.S.: it's not good practice to pass in a string where a number is expected, so "5" should be 5.
    P.P.S: I'm not a Javascript expert.

    in-depth discussion:
    Spoiler:
    Show
    orangeCost is a variable which, when you assign a function to a variable, it becomes what is called a first-class function. In C, a first-class function is a pointer to a function. You can call a first-class function as if it was a function by using its named reference (the variable name orangeCost in this case).

    The keyword function returns the function so that it can be stored as a first-class function. Everything between the curly braces is the function body. cost is the only argument. The argument will be replaced by the argument provided to the function call.cost will be replaced with the value of the argument in the function call. orangeCost with a parentheses call is a function call.This syntax is like this is mostly because almost everything in most modern language is inherited from C, it hasn't changed much.

    Javascript and PHP and written to simulate the way C used to work. They're not exactly the same but they're similar.To understand why the syntax is like that, you have to go back why C syntax is like that, not Javascript, because Javascript is super modern.
    :banghead: I wrote a long reply, and then when I hit 'submit' it vanished :motz:

    Ok so yeh I think you got what I meant. Part of the problem is that the damn tutorial hasn't bothered to clarify what 'statement', 'argument', etc. are. On my own, 'statement' and 'argument' sound like similar things, so I expect them to do the same things and get confused. It's the same with 'variable', and what you described as a placeholder. In my mind, if 'cost' is a placeholder, it's like a variable... it will change depending on the context while other things are fixed. So it makes me really confused as to which one I'm supposed to 'call'. Perhaps part of the confusion comes from having recently finished with CSS, where defining the value of ID or class is very direct. This seems indirect by contrast.

    So after reading your explanation, I am now trying to see the variable + the bit that tells it that it's a function + the content (argument?) of that function as acting all of a piece when I call the variable. Before I was expecting the function to be directly accessible without the variable bit :o: Your bit in the spoiler is helpful... thinking of it as a pointer, rather than as the function itself.

    Sorry for the slow reply, btw. I have a bit of time every day to glower at Codecademy and try and make stuff work, but this is on the side of a lot of other responsibilities.

    Also thanks @INTit
    Offline

    2
    ReputationRep:
    (Original post by Craghyrax)
    :banghead: I wrote a long reply, and then when I hit 'submit' it vanished :motz:

    Ok so yeh I think you got what I meant. Part of the problem is that the damn tutorial hasn't bothered to clarify what 'statement', 'argument', etc. are. On my own, 'statement' and 'argument' sound like similar things, so I expect them to do the same things and get confused. It's the same with 'variable', and what you described as a placeholder. In my mind, if 'cost' is a placeholder, it's like a variable... it will change depending on the context while other things are fixed. So it makes me really confused as to which one I'm supposed to 'call'. Perhaps part of the confusion comes from having recently finished with CSS, where defining the value of ID or class is very direct. This seems indirect by contrast.

    So after reading your explanation, I am now trying to see the variable + the bit that tells it that it's a function + the content (argument?) of that function as acting all of a piece when I call the variable. Before I was expecting the function to be directly accessible without the variable bit :o: Your bit in the spoiler is helpful... thinking of it as a pointer, rather than as the function itself.

    Sorry for the slow reply, btw. I have a bit of time every day to glower at Codecademy and try and make stuff work, but this is on the side of a lot of other responsibilities.

    Also thanks @INTit
    Hey there Craghyrax, I'm glad to see you are making progress in understanding.

    As for the whole statement, expression, variable terminology, this should help.

    The explanation for variable is quite simple: it is a piece of information whose value can vary (much like in math, x is a variable, as it represents an arbitrary value).
    It is a piece of information that refers to a specific spot in memory (RAM), in such a way that if I were to refer to that memory address, I would find its corresponding value for the variable (e.g.: x = 5). A little basket with an egg in it, if you will.
    • Thread Starter
    Offline

    3
    ReputationRep:
    (Original post by 571122)
    Hey there Craghyrax, I'm glad to see you are making progress in understanding.

    As for the whole statement, expression, variable terminology, this should help.

    The explanation for variable is quite simple: it is a piece of information whose value can vary (much like in math, x is a variable, as it represents an arbitrary value).
    It is a piece of information that refers to a specific spot in memory (RAM), in such a way that if I were to refer to that memory address, I would find its corresponding value for the variable (e.g.: x = 5). A little basket with an egg in it, if you will.
    Thanks Five. The link is useful :yy:
    • Thread Starter
    Offline

    3
    ReputationRep:
    Yeh, nevermind...


    :egg:

    :banghead:

    Oh dear God...
    I'd put "shuteye" rather than "shut eye" :lol:
    You either laugh or you cry...
    • Thread Starter
    Offline

    3
    ReputationRep:
    Ok yeh so this is a good example...
    I've switched from Codecademy to Free Code Camp for learning JavaScript.

    Three lessons in and Code Camp has informed me that assignations to variables run from right to left!!!!!
    If Codecademy had bothered to tell me that, I would probably not have had the confusion I did the other day! :hmpf:
    • Thread Starter
    Offline

    3
    ReputationRep:
    (Original post by BobBobson)
    JS is only good for one thing and that's for using jQuery on HTML, apart from that, I ****ing hate it.
    The bootcamp I'm interested in seem to want me to learn it without Bootstrap or jQuery. Surely understanding it thoroughly is good for being an advanced web developer?
    Offline

    2
    ReputationRep:
    (Original post by Craghyrax)
    Ok yeh so this is a good example...
    I've switched from Codecademy to Free Code Camp for learning JavaScript.

    Three lessons in and Code Camp has informed me that assignations to variables run from right to left!!!!!
    If Codecademy had bothered to tell me that, I would probably not have had the confusion I did the other day! :hmpf:
    This is why I recommend for beginners to read an entire in-depth book on the language, like I did.
    Offline

    2
    ReputationRep:
    (Original post by Craghyrax)
    The bootcamp I'm interested in seem to want me to learn it without Bootstrap or jQuery. Surely understanding it thoroughly is good for being an advanced web developer?
    Bootstrap is just a fancy CSS library to make up for CSS design/problems and you don't need it to understand Javascript.
    jQuery is another layer above Javascript to make everything easier and less raw.

    I recommend knowing Javascript well (or at least have a good foundation) before going into jQuery.

    As for your question: understanding the design principles of Bootstrap and how it helps CSS problems is helpful to become an advanced web developer, as well as knowing jQuery to a good degree, but I am still of the opinion that it's more important to know the lower level languages (like Javascript) first. It's not a one-off thing, you actually need to have strong fundamentals in the core of it all (in this case CSS for Bootstrap and Javascript for jQuery) in order to understand the bigger picture (Bootstrap and jQuery).

    I've worked with two guys who hadn't learned CSS or Javascript decently before and they were struggling like mad to get CSS and jQuery figured out. Every time, I had to go over and tell them how the specific problem they were having would originate back to the way JS and CSS work internally.

    And so, I emphasize again, the following skills will make you an advanced web developer:
    - have all the basics down (Javascript, HTML5, CSS3) as well as know the history and how the languages evolved, know how they play in today's IT market, etc.
    - strong fundamentals and a deep understanding of computer languages
    - strong analytical skills to read in between the lines, to see the links between different languages (this helps a lot for debugging. Especially cross-tier, cross-layer debugging and optimization is exceptionally hard)
    - dedication, motivation, hunger for knowledge
    - studying the languages up to a point that you get super nerdy (past the point people don't care, going into extreme detail and being pedantic, I actually do that, but perhaps that's just my style)
    • Thread Starter
    Offline

    3
    ReputationRep:
    (Original post by 571122)
    This is why I recommend for beginners to read an entire in-depth book on the language, like I did.
    Fair enough. That is often the best way to do everything in life. I always got a bit miffed with my undergrads when they complained that the course was hard and never bothered to do the reading that went with it :dry: Well I find the little practical tasks on these courses quite helpful, and all the encouraging badges and stuff. But probably when I finish them I'll also turn to some wider reading on the language. Friends have given me a few resources and lectures to watch :yy:
    (Original post by 571122)
    Bootstrap is just a fancy CSS library to make up for CSS design/problems and you don't need it to understand Javascript.
    jQuery is another layer above Javascript to make everything easier and less raw.

    I recommend knowing Javascript well (or at least have a good foundation) before going into jQuery.
    Codecademy, Free Code Camp and others all teach you bootstrap and JQuery before JavaScript :hmpf: I've skipped those bits because I was warned.
    Offline

    3
    ReputationRep:
    (Original post by 571122)
    Bootstrap is just a fancy CSS library to make up for CSS design/problems and you don't need it to understand Javascript.
    jQuery is another layer above Javascript to make everything easier and less raw.

    I recommend knowing Javascript well (or at least have a good foundation) before going into jQuery.

    As for your question: understanding the design principles of Bootstrap and how it helps CSS problems is helpful to become an advanced web developer, as well as knowing jQuery to a good degree, but I am still of the opinion that it's more important to know the lower level languages (like Javascript) first. It's not a one-off thing, you actually need to have strong fundamentals in the core of it all (in this case CSS for Bootstrap and Javascript for jQuery) in order to understand the bigger picture (Bootstrap and jQuery).

    I've worked with two guys who hadn't learned CSS or Javascript decently before and they were struggling like mad to get CSS and jQuery figured out. Every time, I had to go over and tell them how the specific problem they were having would originate back to the way JS and CSS work internally.

    And so, I emphasize again, the following skills will make you an advanced web developer:
    - have all the basics down (Javascript, HTML5, CSS3) as well as know the history and how the languages evolved, know how they play in today's IT market, etc.
    - strong fundamentals and a deep understanding of computer languages
    - strong analytical skills to read in between the lines, to see the links between different languages (this helps a lot for debugging. Especially cross-tier, cross-layer debugging and optimization is exceptionally hard)
    - dedication, motivation, hunger for knowledge
    - studying the languages up to a point that you get super nerdy (past the point people don't care, going into extreme detail and being pedantic, I actually do that, but perhaps that's just my style)
    Hi my good friend. Here are the latest programs i would greatly appreciate your help with:

    Name:  leapyearsv1.jpg
Views: 26
Size:  253.3 KB

    http://pastebin.com/9QWb1duP

    http://pastebin.com/Nm5wdZpF

    Thanks!
    Offline

    3
    ReputationRep:
    (Original post by john2054)
    Hi my good friend. Here are the latest programs i would greatly appreciate your help with:

    Name:  leapyearsv1.jpg
Views: 26
Size:  253.3 KB

    http://pastebin.com/9QWb1duP

    http://pastebin.com/Nm5wdZpF

    Thanks!
    First thing is that the question was asking you to create a static method in the LeapYear class that takes an int and returns a boolean. You're overcomplicating it by have constructors, instance variables etc. All you need is that one static method, and main which should call it a few times..
    Offline

    3
    ReputationRep:
    (Original post by BobBobson)
    First thing is that the question was asking you to create a static method in the LeapYear class that takes an int and returns a boolean. You're overcomplicating it by have constructors, instance variables etc. All you need is that one static method, and main which should call it a few times..
    http://pastebin.com/4UYWgDAZ

    Is this one any better??
 
 
 
Write a reply… Reply
Submit reply

Register

Thanks for posting! You just need to create an account in order to submit the post
  1. this can't be left blank
    that username has been taken, please choose another Forgotten your password?
  2. this can't be left blank
    this email is already registered. Forgotten your password?
  3. this can't be left blank

    6 characters or longer with both numbers and letters is safer

  4. this can't be left empty
    your full birthday is required
  1. Oops, you need to agree to our Ts&Cs to register
  2. Slide to join now Processing…

Updated: November 25, 2016
TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

Today on TSR
Poll
Would you rather have...?
Useful resources

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Quick reply
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.