• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

Somnid

Member
Damn, sounds near impossible to get in. Do you remember what type of problem it was you struggled on?

It was a non-coding algorithm problem, basically they asked how to transform a file a certain way even though it couldn't fit into memory. There was a bit of trick to it I didn't catch on to and basically had to be guided to it (the problem would take roughly 5 minutes if you immediately caught on while I spent 20-30 minutes postulating different ways to do it). It's not the sort of thing I thought to study beforehand as I was more concerned about trees, graphs, sorting and all that type of stuff.
 
Yeah, traditional coding interview advice is that it's fine not to know the answer right away and it's often even better if you don't, since you can guide the interviewer through your thought process. That isn't the case with at least Facebook. They expect an answer within a few minutes.
 

Somnid

Member
Yeah, traditional coding interview advice is that it's fine not to know the answer right away and it's often even better if you don't, since you can guide the interviewer through your thought process. That isn't the case with at least Facebook. They expect an answer within a few minutes.

I think it's just a product of the number of applicants. It might not click with you and you spend 30 minutes and 2 passes whereas someone else nails it in 10 minutes on the first try, no matter how well you explain yourself why wouldn't they pick the one who got it in 10? It's impossible to quantify the rest of those hidden variables you might have. There's definitely a luck factor that goes into it, who you're against and what problems you get.
 

JaMarco

Member
It was a non-coding algorithm problem, basically they asked how to transform a file a certain way even though it couldn't fit into memory. There was a bit of trick to it I didn't catch on to and basically had to be guided to it (the problem would take roughly 5 minutes if you immediately caught on while I spent 20-30 minutes postulating different ways to do it). It's not the sort of thing I thought to study beforehand as I was more concerned about trees, graphs, sorting and all that type of stuff.
Did the trick have something to do with buffering or precomputation?
 

Kansoku

Member
If Rust compiles to LLVM...can I use it on platforms that don't support it like Switch and Xbone? I know PS4 works.

If they add a target for it, sure. From my quick searches right now, it seems Xbone uses the same CPU architecture as PS4. Since Sony added a PS4 target to LLVM, it might work* on Xbone, or require some easy adjustments.
Can't see Switch happening tho.

On that same note, I recently learned that LLVM is going to get a wasm target, so you could compile code for the web with Rust :D





* I have no idea what I'm talking about take this with a huge grain of salt.
 

Makai

Member
If they add a target for it, sure. From my quick searches right now, it seems Xbone uses the same CPU architecture as PS4. Since Sony added a PS4 target to LLVM, it might work* on Xbone, or require some easy adjustments.
Can't see Switch happening tho.

On that same note, I recently learned that LLVM is going to get a wasm target, so you could compile code for the web with Rust :D





* I have no idea what I'm talking about take this with a huge grain of salt.
wasm is already available.
 
If Rust compiles to LLVM...can I use it on platforms that don't support it like Switch and Xbone? I know PS4 works.

In theory, as long as a LLVM backend exists for the platform, you should be able to compile Rust to it. Xbox One and PS4 are x86-64 and Switch is ARM. With ARM it's a little trickier because there's different flavors and everything, but theoretically it should be no problem. The Rust standard library depends on libc being present I believe, but other than that, I see no major hurdles for Rust itself.* Whether you can effectively develop games with it on the platform, given that the library situation w.r.t. games is not that great in Rust is a different question.

* I am not a game developer and thus have no experience developing for consoles.
 
Thanks for the responses.

Did you end up getting an offer if you don't mind me asking?

Sadly no. I felt I did pretty well for most of it but one session I definitely struggled and I think that sealed it.

Sorry to hear. I honestly don't understand the interview process, everyone I know who has applied has gotten rejected, and many of them are very good. My best advice is to try again in 6 months. It's definitely possible to get accepted on your 2nd or even 3rd interview. FWIW it's actually ok to struggle on one session, but you need to absolutely kill it on another one to make up for it.

It was a non-coding algorithm problem, basically they asked how to transform a file a certain way even though it couldn't fit into memory. There was a bit of trick to it I didn't catch on to and basically had to be guided to it (the problem would take roughly 5 minutes if you immediately caught on while I spent 20-30 minutes postulating different ways to do it). It's not the sort of thing I thought to study beforehand as I was more concerned about trees, graphs, sorting and all that type of stuff.

I guess it depends on the nature of the file format in question, but as a purely general solution, couldn't you create an abstraction around mmap so that you could pass an arbitrary offset into the file, and it would mmap in slices of the file as needed, un mapping earlier portions if your offset is outside the view you currently have mapped? This way you only ever have in memory a fixed amount of data, but you can access arbitrary offsets on demand.
 

Somnid

Member
Sorry to hear. I honestly don't understand the interview process, everyone I know who has applied has gotten rejected, and many of them are very good. My best advice is to try again in 6 months. It's definitely possible to get accepted on your 2nd or even 3rd interview.

Yeah, I might try again someday depending on what job I get next, I think it's doable with a bit of luck. Although IIRC Google has a 3 on-site interview policy and they recommend a 1 year cooldown between interviews.

I guess it depends on the nature of the file format in question, but as a purely general solution, couldn't you create an abstraction around mmap so that you could pass an arbitrary offset into the file, and it would mmap in slices of the file as needed, un mapping earlier portions if you cross a boundary? This way you only ever have in memory a fixed amount of data, but you can access arbitrary offsets on demand.

Maybe. Since I was applying as mostly front-end web specific file IO operations are a bit outside of my purview and I think they were trying to avoid that since asking specific file IO implementation questions in an interview can get hairy quickly unless you're specializing there.
 

zeemumu

Member
For onsite, you basically show up to the lobby and wait for your initial interviewer. They stick you in a conference room and you're basically given 1 to 2 typical interview questions per session (1 interviewer per hour for 5 sessions plus lunch).Typically these are algorithm related and if you have a lot of experience at least one will be on architecture and design. You'll probably get at least one question dealing with combinatorics too (mine was pretty well disguised in simple math so it wasn't big deal). The first 5 minutes of each session are the interviewer introducing themselves and the project they work on and you'll usually have about 5 minutes at the end to ask questions. I got no soft questions but that might depend on the exact position and experience level. For each you have the option of using a text editor on a chromebook or a whiteboard (or both). For lunch you are assigned to another Googler they'll take you to the cafeteria and you'll eat, talk about Google and they'll probably tour you around the campus.

Overall it is extremely exhausting but goes by quick. Make sure to grab a water because you'll be talking a lot.


This feels intimidating as hell.
 

Slavik81

Member
I guess it depends on the nature of the file format in question, but as a purely general solution, couldn't you create an abstraction around mmap so that you could pass an arbitrary offset into the file, and it would mmap in slices of the file as needed, un mapping earlier portions if your offset is outside the view you currently have mapped? This way you only ever have in memory a fixed amount of data, but you can access arbitrary offsets on demand.
That would definitely work, but with the right flags, I think you may be able to just mmap the whole file and leave to the OS the hard work of paging chunks in and out.
 
That would definitely work, but with the right flags, I think you may be able to just mmap the whole file and leave to the OS the hard work of paging chunks in and out.

I don't think so, because mmap -- even if it doesn't actually page all the memory in -- still has to reserve a single contiguous block of address space. Otherwise there's no guarantee that if you access, say, offset 2^20+1 (assuming it decides to page in 2^20 bytes), that it will be able to use the memory at that address to put in the next set of bytes.

If the problem says there's not enough memory to fit the entire file, we have to assume that sizeof(File) > sizeof(Process address space)
 
Had a technical test for a trainee web developer graduate program and I struggled with that shit even though you didn't need to have a lot of coding experience. One of the problems was Javascript based and you had to form a specific word from jumbled up letters. Only could put 3 letters in the proper place before I had to move on to another task because I was running out of time for the second one. It was a pretty complex problem where you were given a scenario where you were a warehouse manager and you had to write down a way to clear the stock as efficiently as possible.

I had to be guided through the discussion afterwards about my approach to solving the 2 problems. I feel my actual answers and logic were fine, it's just that I didn't finish the problems, I didn't articulate my approach confidently at all and I came across as extremely nervous. I of course didn't get the job, which is disheartening. I must learn more and improve, honestly. That wasn't good enough. I have issues focusing a lot of the time. Would you recommend trying to get the Microsoft software development cert or enroll onto some kind of bootcamp to give me some structure? I come from a non IT STEM background so that may be helpful.
 

g23

European pre-madonna
guys i know this is amateur javascript stuff but can someone please help me solve this challenge:


// You are given one numeric variable:
var n = 25;

// Its value may change when you submit.
// DO NOT EDIT CODE ABOVE THIS LINE.
// =================================

// Your Challenge:
// 1. Declare a new variable named "sum" and initialize it to 0.
// Be sure to use the "var" keyword.
// 2. Use a for loop to add every positive integer less than n to sum.
// 3. Come up with a solution that works for all values of n.

// Your code:



var sum = 0; var n = 25;

for (n = 25; sum <= n; sum++)
{
alert( n );
console.log(n);
n + sum;
}

PLEASE TELL ME WHAT I'M DOING WRONG! are my conditions wrong? My operators? I have a gut feeling it's my actual block of code that's off but can't figure out what exactly to write in order to come up with a solution. Thanks
 

dabig2

Member
guys i know this is amateur javascript stuff but can someone please help me solve this challenge:


// You are given one numeric variable:
var n = 25;

// Its value may change when you submit.
// DO NOT EDIT CODE ABOVE THIS LINE.
// =================================

// Your Challenge:
// 1. Declare a new variable named "sum" and initialize it to 0.
// Be sure to use the "var" keyword.
// 2. Use a for loop to add every positive integer less than n to sum.
// 3. Come up with a solution that works for all values of n.

// Your code:



PLEASE TELL ME WHAT I'M DOING WRONG! are my conditions wrong? My operators? I have a gut feeling it's my actual block of code that's off but can't figure out what exactly to write in order to come up with a solution. Thanks

I don't know if I'm misreading the instruction, but you're supposed to be programming a simple summation algorithm, correct? For all integers between 0 and 24? Like 24 + 23 + ... + 1 + 0 = 300 (I think lol).

So given that, your entire loop counter is wrong. Think of what sum (and n) should be after the first 3 iterations for example:

iteration 1: 24 + (sum = 0) => sum = 24
iteration 2: 23 + (sum=24) => sum = 47
iteration 3: 22 + (sum=47) => sum = 69
and so on.

OR if you're an increment kind of guy

iteration 1: 0 + (sum =0) => sum = 0
iteration 2: 1 + (sum=0) => sum =1
iteration 3: 2 + (sum = 1) => sum=3
iteration 4: 3 + (sum=3) => sum= 6
and so on

Now given this you should be able to fix your loop counter. Get sum out of there for example and only manipulate n. Also, when adding n to sum, make sure you're storing that in sum itself: sum = n + sum
 

Jokab

Member
Came across a late high schooler's problem that I can't solve:

Given: For some x, all y: P(x,y) AND NOT P(y,x)
Give an example of x and P for which this holds.

Can't come up with anything. Seems very difficult as if P(x,y) holds then you can just pick y to be the x for which it works, and then P(y,x) will also hold. Right?
 

Somnid

Member
Came across a late high schooler's problem that I can't solve:

Given: For some x, all y: P(x,y) AND NOT P(y,x)
Give an example of x and P for which this holds.

Can't come up with anything. Seems very difficult as if P(x,y) holds then you can just pick y to be the x for which it works, and then P(y,x) will also hold. Right?

I don't interpret it like that. It's not enough that any y satisfies, all y must satisfy but just a single x needs to. So a trivial function:

Code:
function P(x,y){
   return x == 10
}

x = 10, y = anything will produce true.
if we fixed the second parameter at 10, then this would produce false for every y except 10, which is not all y. Or at least that's my thought.
 

Jokab

Member
I don't interpret it like that. It's not enough that any y satisfies, all y must satisfy but just a single x needs to. So a trivial function:

Code:
function P(x,y){
   return x == 10
}

x = 10, y = anything will produce true.
if we fixed the second parameter at 10, then this would produce false for every y except 10, which is not all y. Or at least that's my thought.

But this doesn't work since the second function also must be false for all y. However, it will be true for the specific case when y = 10, so P(y,x) will not be false for all y.

Edit: I think this is impossible. Consider x=y, then P(x,x) AND NOT P(x,x). That can't possible be true, can it? If so that means there is one y for which it doesn't hold, and therefore the expression is not true for all y. Maybe the question is faulty and should be "for all x and some y"?
 

Somnid

Member
But this doesn't work since the second function also must be false for all y. However, it will be true for the specific case when y = 10, so P(y,x) will not be false for all y.

Edit: I think this is impossible. Consider x=y, then P(x,x) AND NOT P(x,x). That can't possible be true, can it? If so that means there is one y for which it doesn't hold, and therefore the expression is not true for all y. Maybe the question is faulty and should be "for all x and some y"?

I see. Do we know what the domain of x and y are?
 

Jokab

Member
Can you use language tricks or must the inputs be real numbers?

In JS, NaN != NaN.

(x,y) => x === x;
Perhaps I should have defined it, but this is for a maths course with current topic predicate algebra. So I imagine programming languages can't be used (perhaps this is the wrong thread really, but programming and maths sort of intersect, especially in logic).
 

JesseZao

Member
Perhaps I should have defined it, but this is for a maths course with current topic predicate algebra. So I imagine programming languages can't be used (perhaps this is the wrong thread really, but programming and maths sort of intersect, especially in logic).

Seems like something is missing then; because, for it to work as described, you have to have the case where the same inputs return different outputs.


Maybe...

(x, y) => x subset y

x: {}
y: { z : z € R } (€ for element of)
 
Unless the problem is poorly worded I don't think it's possible.

For example, if you actually write this with mathematical notation you get this:

&#8707;x &#8704;y P(x,y) &#8743; ~P(y,x)

This is how the problem should be written if you interpret it strictly. This has been shown to be impossible earlier. For example, let y = x, then P(x,x) &#8743; ~P(x,x). The set of all P that satisfy this is precisely &#8709;

But if you reverse the order of x and y you get this:

&#8704;y &#8707;x P(x,y) &#8743; ~P(y,x)

These are two entirely different statements. In the first one, you fix x and then test every y. x and y are completely independent.

In the second one, for each y the value of x can be different.

Let P(x,y) = y > x

For each y, choose x = y - 1.

P(x,y) = P(y-1, y) = y > (y-1) = true
P(y,x) = P(y, y-1) = y-1 > y = false
 

Jokab

Member
Unless the problem is poorly worded I don't think it's possible.

For example, if you actually write this with mathematical notation you get this:

&#8707;x &#8704;y P(x,y) &#8743; ~P(y,x)

This is how the problem should be written if you interpret it strictly. This has been shown to be impossible earlier. For example, let y = x, then P(x,x) &#8743; ~P(x,x). The set of all P that satisfy this is precisely &#8709;

But if you reverse the order of x and y you get this:

&#8704;y &#8707;x P(x,y) &#8743; ~P(y,x)

These are two entirely different statements. In the first one, you fix x and then test every y. x and y are completely independent.

In the second one, for each y the value of x can be different.

Let P(x,y) = y > x

For each y, choose x = y - 1.

P(x,y) = P(y-1, y) = y > (y-1) = true
P(y,x) = P(y, y-1) = y-1 > y = false

Yeah this is pretty much what I came up with, but it's nice to see someone shares my ideas. The questions just has to be wrong. Thanks.
 

Koren

Member
I understand it the same way as you... and I think it's wrong since P(x,x) can't obviously be True AND False.

Another possibility to correct it could be "&#8707;x | &#8704;y!=x ..."
 
I am currently working through the JavaScript section on Codecademy, and I am wondering on the best way of writing a particular piece of code.

Basically I have written a rock, paper, scissors game, which works, but I also want to write an IF statement to validate the users choice. So I wrote this:

var userChoice = prompt("Do you choose rock, paper or scissors?");

if(userChoice != "rock" || "paper" || "scissors") {
alert("Please select either 'rock', 'paper' or 'scissors'");
userChoice = prompt("Do you choose rock, paper or scissors?");
}

It seems to work if I enter either rock or paper, but when I enter scissors, it doesnt like it for some reason. Also, I am wondering if there is a more elegant way of reprompting the user for their answer without duplicating the code.
 

Somnid

Member
I am currently working through the JavaScript section on Codecademy, and I am wondering on the best way of writing a particular piece of code.

Basically I have written a rock, paper, scissors game, which works, but I also want to write an IF statement to validate the users choice. So I wrote this:

var userChoice = prompt("Do you choose rock, paper or scissors?");

if(userChoice != "rock" || "paper" || "scissors") {
alert("Please select either 'rock', 'paper' or 'scissors'");
userChoice = prompt("Do you choose rock, paper or scissors?");
}

It seems to work if I enter either rock or paper, but when I enter scissors, it doesnt like it for some reason. Also, I am wondering if there is a more elegant way of reprompting the user for their answer without duplicating the code.

I think you want to put that in a while loop so you continually prompt until they pick something correct. Also, the check should be userChoice != "rock" || userChoice != "paper" || userChoice != "scissors", without respecifying the variable you are actually saying "is userChoice rock?", then "is paper truthy?" (always), then "is scissors truthy?" (always)
 
I think you want to put that in a while loop so you continually prompt until they pick something correct. Also, the check should be userChoice != "rock" || userChoice != "paper" || userChoice != "scissors", without respecifying the variable you are actually saying "is userChoice rock?", then "is paper truthy?" (always), then "is scissors truthy?" (always)

Thanks :)
 
Hmm, not sure what I have done now. I changed it to this:

var userChoice = prompt("Do you choose rock, paper or scissors?");

while(userChoice != "rock" || userChoice != "paper" || userChoice != "scissors") {
alert("Please select either 'rock', 'paper' or 'scissors'");
userChoice = prompt("Do you choose rock, paper or scissors?");
}

but now it doesn't accept any answers and chrome is endlessly looping through the alert and prompt.
 
Hmm, not sure what I have done now. I changed it to this:

var userChoice = prompt("Do you choose rock, paper or scissors?");

while(userChoice != "rock" || userChoice != "paper" || userChoice != "scissors") {
alert("Please select either 'rock', 'paper' or 'scissors'");
userChoice = prompt("Do you choose rock, paper or scissors?");
}

but now it doesn't accept any answers and chrome is endlessly looping through the alert and prompt.

If choice is "rock", does your condition evaluate to true or false?
 
If choice is "rock", does your condition evaluate to true or false?

"rock" also doesn't work. I am still relatively new to JS and programming in general, so I am not quite sure why this isn't working. Is it because with any choice, it also does not equal the other options i.e. whilst it may equal "rock", it does not equal "paper" or "scissors"?
 

Somnid

Member
"rock" also doesn't work. I am still relatively new to JS and programming in general, so I am not quite sure why this isn't working. Is it because with any choice, it also does not equal the other options i.e. whilst it may equal "rock", it does not equal "paper" or "scissors"?

Look at what happens for each comparison:

if rock:

false || true || true -> true -> loop continues

if paper:

true || false || true -> true -> loop continues

if scissors:

true || true || false -> true -> loop continues
 
Look at what happens for each comparison:

if rock:

false || true || true -> true -> loop continues

if paper:

true || false || true -> true -> loop continues

if scissors:

true || true || false -> true -> loop continues

Changed it to this which now seems to work properly:

var userChoice = prompt("Do you choose rock, paper or scissors?");

while(userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors") {
alert("Please select either 'rock', 'paper' or 'scissors'");
userChoice = prompt("Do you choose rock, paper or scissors?");
}

Thanks again :)
 
I'm learning Java, and currently working on an assignment. We're making deck, card, player, and game classes in order to practice lists, sets, maps etc.

Each deck is given a list of cards from the professor's test harness. I'm having trouble with a drawAll method:
Removes all Cards from this Deck and returns them as a new Deck. If this deck is empty, then this method returns an empty new Deck.

So the professor gave me some hints.

1. Construct a new empty deck
2. Pass elements from the list the professor created into the new deck
3. Call clear() to remove cards from the first deck, not removeAll()

As far as two goes, he first said to use a loop to get and remove each item, then said sublists would be easier (I don't think we've ever gone over sublists, so it's fun research time).

For now just trying to cobble together something that works, I have no idea what I'm doing.
public class DeckImp extends java.util.ArrayList<Card> implements edu.gsu.csc1302.coll1.Deck {

///constructors and other methods

public Deck drawAll() {
Deck newdeck = new DeckImp(); //construct new deck
if (isEmpty() == false){ //check that deck is not empty
newdeck.addAll(list); //add all elements of list(what to call?) to newdeck
list.clear(); //clear old list
{
return newdeck; //return new deck, which either has the old deck's cards or is empty is isEmpty returned true
}
}
}

In the interest of not cluttering up the thread too much, here's a pastebin with the test that is failing.
http://pastebin.com/yeAnaBaW
 

Jokab

Member
The code looks fine to me unless I'm missing something. What do you mean by "failing" in the pastebin? Does the program crash? Also what is "list" that you're using in the method?
 
The code looks fine to me unless I'm missing something. What do you mean by "failing" in the pastebin? Does the program crash? Also what is "list" that you're using in the method?

I'm using a "test harness" with assertions enabled to get the tests in a class the professor provided to run using the classes and methods/implementations I designed according to the assignment. When I run the code it tells me which tests didn't finish running and at what point it stopped.

I think the "list" is the last piece I need, the "list" is in the test class. The cardList is used to create deck1, I think it's deck1 I need. I'm just not sure how to access it from my method.
 

Tristam

Member
guys i know this is amateur javascript stuff but can someone please help me solve this challenge:


// You are given one numeric variable:
var n = 25;

// Its value may change when you submit.
// DO NOT EDIT CODE ABOVE THIS LINE.
// =================================

// Your Challenge:
// 1. Declare a new variable named "sum" and initialize it to 0.
// Be sure to use the "var" keyword.
// 2. Use a for loop to add every positive integer less than n to sum.
// 3. Come up with a solution that works for all values of n.

// Your code:



PLEASE TELL ME WHAT I'M DOING WRONG! are my conditions wrong? My operators? I have a gut feeling it's my actual block of code that's off but can't figure out what exactly to write in order to come up with a solution. Thanks

As another poster said, avoid altering the sum variable in the loop's control statements. Also, you are initializing n at 25, so you'll want to decrement n (e.g., n--) as part of the final statement of the loop control statement group. Also, you'll really want to initialize at n - 1, since the instructions read "every positive integer less than n."

Code:
       var sum = 0;
	
	for (n = n - 1; n > 0; n--) {
	    sum += n;
	}
	
	console.log(sum);
 
Is there a way to store a message that the users entered in an array? I've been looking for answers on google, and I found fgets() or something like this "scanf("%[^\n]", message);" but it's not working out for me. For C programming.
 

ionitron

Member
I am currently working through the JavaScript section on Codecademy, and I am wondering on the best way of writing a particular piece of code.

Basically I have written a rock, paper, scissors game, which works, but I also want to write an IF statement to validate the users choice. So I wrote this:

var userChoice = prompt("Do you choose rock, paper or scissors?");

if(userChoice != "rock" || "paper" || "scissors") {
alert("Please select either 'rock', 'paper' or 'scissors'");
userChoice = prompt("Do you choose rock, paper or scissors?");
}

It seems to work if I enter either rock or paper, but when I enter scissors, it doesnt like it for some reason. Also, I am wondering if there is a more elegant way of reprompting the user for their answer without duplicating the code.

Haha I'm here too!


Yeah, so I'm trying to learn Javascript. It's fun and all, but there are some things that I'm just like, wtf how am I supposed to know? I'm trying to learn the basics on codeacademy and then if things go well then I'm giving my hand on Eloquent Javascript.

But, one thing that is tripping me up is that they are throwing out terms willy nilly like I'm supposed to know what the exact difference between minor things are or why something has a period or a semicolon and why something else doesn't?

Like, what makes console.log("insertblahere") have a period between the console and log, but prompt("insertblahere") not have a period? Like, what exactly is "console" and what is "log," what is "prompt." I feel like I am missing an overarching hierarchy.

And then there's the semicolon. How come if/else statements have the ";" inside the curly braces, but return statements have the semicolon outside the curly braces?

I hope I'm making sense and someone can help me out. D:
 
console is an object. It has lots of methods besides logging, but they all have one thing in common. They mess with the console. log on the other hand is a specific operation (function) to perform on that object. You might want to log to things other than consoles. Maybe you want to log to a file. In that case you'd probably have a file object, and write file.log().

Different objects have entirely different operations. For example on a file you can seek. So maybe file supports the seek() operation while console doesn't.

Some methods are just global system methods that are not associated with any object. prompt() is one such function.
 

D4Danger

Unconfirmed Member
Haha I'm here too!


Yeah, so I'm trying to learn Javascript. It's fun and all, but there are some things that I'm just like, wtf how am I supposed to know? I'm trying to learn the basics on codeacademy and then if things go well then I'm giving my hand on Eloquent Javascript.

But, one thing that is tripping me up is that they are throwing out terms willy nilly like I'm supposed to know what the exact difference between minor things are or why something has a period or a semicolon and why something else doesn't?

Like, what makes console.log("insertblahere") have a period between the console and log, but prompt("insertblahere") not have a period? Like, what exactly is "console" and what is "log," what is "prompt." I feel like I am missing an overarching hierarchy.

And then there's the semicolon. How come if/else statements have the ";" inside the curly braces, but return statements have the semicolon outside the curly braces?

I hope I'm making sense and someone can help me out. D:

the easiest way to think about it is an object is like a folder and files. That's the hierarchal structure.

console is an object so it doesn't do anything it's just a container/context for functions and data and is a way of structuring code.

log in this case is a function in the console object. To access it you have to say where it is and you drill down into objects using the period. so log which lives inside console would be called using console.log()

if you just tried to call log() it wouldn't know it belonged to console it would actually look to see if there was a function called log and if not work it's way up the scope of the program until it hit the global object (in the browser this is called "window")

so in this case the reason you can use prompt() but not log() is because prompt is actually a function of the browser API and is inside the global "window" object

console is actually part of the browser API too and lives in global "window" object. If you wanted to call log you could say window.console.log() and it would work. The global "window" object is assumed if you leave it off though which is why prompt() works because it goes up until it finds it on the global object.
 

Somnid

Member
Haha I'm here too!


Yeah, so I'm trying to learn Javascript. It's fun and all, but there are some things that I'm just like, wtf how am I supposed to know? I'm trying to learn the basics on codeacademy and then if things go well then I'm giving my hand on Eloquent Javascript.

But, one thing that is tripping me up is that they are throwing out terms willy nilly like I'm supposed to know what the exact difference between minor things are or why something has a period or a semicolon and why something else doesn't?

Like, what makes console.log("insertblahere") have a period between the console and log, but prompt("insertblahere") not have a period? Like, what exactly is "console" and what is "log," what is "prompt." I feel like I am missing an overarching hierarchy.

And then there's the semicolon. How come if/else statements have the ";" inside the curly braces, but return statements have the semicolon outside the curly braces?

I hope I'm making sense and someone can help me out. D:

Semicolon separates distinct statements. When you have something in curly braces (block statement) you don't need to add the semicolon because it's understood. It's still confusing even if you know javascript well though because it's kinda vestigial from languages like C, Javascript doesn't actually need semicolons and will implicitly add them and there are a handful of cases where this causes behavior you might not intend which is why most styles suggest you are explicit.

Code:
var x  = 2; //typical statement

var y = 3 //javascript will implicitly put a semicolon here
var z = 4

while(true){
 //do something
} //nothing needed here because this is a block

function(x, y){
 //do something
} //nothing needed here because this is a block

var func = function(x, y) {
 //do something
}; //semicolon here because the function was part of an expression

return 
{
   value : 2
}; //this is where things get weird

//javascript is doing this:

return;
{
  value: 2
}

//but what you probably want is this
return {
  value :2
};

period denote properties on an object. So for example "console" is an object in the global space you can use it like:

Code:
console.log("hello");
console.warn("oops");

Basically those function belong to "console". Implicitly everything is attached to a global object, in browsers this is called "window". So really when you call console.log you're doing:

Code:
window.console.log("hello")

"prompt" does not belong to console (it doesn't write to the console). It belongs to "window" so these are the same:

Code:
window.prompt("hello");
prompt("hello")

Again the latter example works because everything is implicitly on window (except this is a simplifying lie and you'll learn about the "this" context parameter which muddies things and is probably one of the more confusing aspects of javascript)

Hopefully that helps.
 

ionitron

Member
Thank you so much guys. It helps a lot! I feel like some of this stuff is a little vague/abstract still but hopefully with practice I understand it more.

It's kind of annoying but a few of the introductory materials I looked at don't tell you really what these things are, just to do it. And I'm like, am I really getting anything out of that?
 
Top Bottom