These are chat archives for FreeCodeCamp/Help

12th
Feb 2015
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 11:07 UTC
I need help!
var languages = {
    english: "Hello!",
    french: "Bonjour!",
    notALanguage: 4,
    spanish: "Hola!"
};

// print hello in the 3 different languages
for(var property in languages){
    if(typeof property === "string"){
        console.log(property)
    }
}
MychaelZ
@MychaelZ
Feb 12 2015 11:08 UTC
var languages = {
    english: "Hello!",
    french: "Bonjour!",
    notALanguage: 4,
    spanish: "Hola!"
};

// print hello in the 3 different languages
for(var property in languages){
    if(typeof property === "string"){
        console.log(languages[property]);
    }
}
brah
Cassandra Cruz
@lambdatastic
Feb 12 2015 11:09 UTC
@MychaelZ Still not right
MychaelZ
@MychaelZ
Feb 12 2015 11:10 UTC
lol
var languages = {
    english: "Hello!",
    french: "Bonjour!",
    notALanguage: 4,
    spanish: "Hola!"
};

// print hello in the 3 different languages
for(var property in languages){
    if(typeof languages[property] === "string"){
        console.log(languages[property]);
    }
}
Cassandra Cruz
@lambdatastic
Feb 12 2015 11:10 UTC
Better :P
@AmmarAliShah Get the difference?
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 11:11 UTC
@alicekamada not much
@MychaelZ thanks
MychaelZ
@MychaelZ
Feb 12 2015 11:13 UTC
brah the property is just the name ('english', 'french', 'notALanguage', 'spanish') you need to reference the object that it is on to get the value like languages.english or languages['english']
Cassandra Cruz
@lambdatastic
Feb 12 2015 11:13 UTC
So in your snippet, you made two mistakes. First, you were checking the keys themself to see if they were strings. That doesn’t skip the one you wanted to pass over, notALanguage. The second was that you were printing the keys instead of their assigned values.
MychaelZ
@MychaelZ
Feb 12 2015 11:14 UTC
property === 'english';
languages[property] === languages['english'] === 'Hello!';
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 11:16 UTC
@alicekamada @MychaelZ oh! Thanks guys
Cassandra Cruz
@lambdatastic
Feb 12 2015 11:18 UTC
np
Bettina Schaden
@bSchaden
Feb 12 2015 14:04 UTC
ahh, sorry, I'm learning the ins and outs of this chat room with the markdown
thanks for spotting the mistake
Bettina Schaden
@bSchaden
Feb 12 2015 15:02 UTC
Hi, silly n00b question. Is DOM pronounced D-O-M or "dom?"
Cristián Berríos
@crisberrios
Feb 12 2015 15:03 UTC
I just say Dom
as in "Dohm" not "dome"
Bettina Schaden
@bSchaden
Feb 12 2015 15:05 UTC
@crisberrios thanks
Cristián Berríos
@crisberrios
Feb 12 2015 15:05 UTC
that's just me anyways, but don't care too much
Maxim Orlov
@Maximization
Feb 12 2015 15:05 UTC
the guys from code school pronounce it like this so I guess it's right
Cristián Berríos
@crisberrios
Feb 12 2015 15:05 UTC
there's a war between "GIF" and "JIF" for .gif files
allare
@allare
Feb 12 2015 15:11 UTC
Hello All.
I am in Nodejs right now. I wonder if Nodejs can replace PHP for good.
Cristián Berríos
@crisberrios
Feb 12 2015 15:12 UTC
For many tasks it can
but you can say that about Ruby and Python
and still PHP is used a lot
so who nows
knows
allare
@allare
Feb 12 2015 15:16 UTC
I have read that PHP has lots of problems. Many developers suggest to skip PHP in Quora too.
Cristián Berríos
@crisberrios
Feb 12 2015 15:19 UTC
Well, If we are here, we're sticking with node right?
allare
@allare
Feb 12 2015 15:21 UTC
Yeah, that's why I was wondering. I am more into front-end, but wonder if nodejs is enough if I decide to go for simple back-end job in the future.
Maxim Orlov
@Maximization
Feb 12 2015 15:22 UTC
@allare the mean stack was chosen by FCC argumenting job prospects
so to answer your question, yes
allare
@allare
Feb 12 2015 15:23 UTC
Great @Maximization
Maxim Orlov
@Maximization
Feb 12 2015 15:23 UTC
now on a more specific level, depending on where you live and how flexible you are, what career goals you have, a different back-end language would be more suitable
nevertheless, pick one and stick to it and don't hop on/off
allare
@allare
Feb 12 2015 15:25 UTC
Yeah, this is the first time I stick to something. Thanks to FreeCodeCamp. Otherwise whenever I saw someone mentioning new technology to take over the world, I wanted to learn that language.
Cristián Berríos
@crisberrios
Feb 12 2015 15:25 UTC
@allare Also, node.js has some great tools to streamline frontend development, like testing and building tools, and package management.
allare
@allare
Feb 12 2015 15:32 UTC
Yeah, I noticed that. But many things are completely alien to me. I hope to get your guidance to get familiar to many terms. @crisberrios
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 15:36 UTC
I'm stuck again
var cashRegister = {
    total: 0,
//insert the add method here    
    add: function(itemCost){
        this.total += itemCost;
    },

    scan: function (item) {
        switch (item) { 
        case "eggs": 
            this.add(0.98); 
            break;

        case "milk": 
            this.add(1.23); 
            break;

        //Add other 2 items here
        case "magazine":
            this.add(4.99);
            break;
        case  "chocolate":
            this.add(0.45);
            break;

        }
        return true;
    }
};

//Scan 2 eggs and 3 magazines
cashRegister.scan(eggs);
cashRegister.scan(eggs);
cashRegister.scan(magazine);
cashRegister.scan(magazine);
cashRegister.scan(magazine);

//Show the total bill
console.log('Your bill is '+cashRegister.total);
What's wrong?
Kamal Sharif
@ksharifbd
Feb 12 2015 15:38 UTC
Are you missing 'default'?
Cristián Berríos
@crisberrios
Feb 12 2015 15:39 UTC
sure @allare, I'll do what I can :P
allare
@allare
Feb 12 2015 15:39 UTC
We don't need default here as per Codecademy
Cristián Berríos
@crisberrios
Feb 12 2015 15:41 UTC
cashRegister.scan(magazine);
missing " "
allare
@allare
Feb 12 2015 15:43 UTC
That's it. All the items' names must be within ""
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 16:21 UTC
that was it
thanks guys
Darryl Dixon - Piece Digital
@piecedigital
Feb 12 2015 16:25 UTC
I need assistance.
I need to add prime numbers but I don't know how to thoroughly check
function sumPrimes(num) {
  var numSum = 0;
  for(i=0; i < num; i++){
    if( i % i !== 0  && i % 1 !== 0 )
      {numSum+=0;}
    else
      {numSum+=i;}
  }
  return numSum;
}

sumPrimes(3);
Maxim Orlov
@Maximization
Feb 12 2015 16:32 UTC
i % i is always going to be 0
you will need to have an array to store your primes as you go
Darryl Dixon - Piece Digital
@piecedigital
Feb 12 2015 16:34 UTC
How would that fix anything if all I have to do is add the primes?
Nathan
@terakilobyte
Feb 12 2015 16:37 UTC
you need to properly check to see if the number is prime
having an array of primes you keep pushing to allows you to expand the tests
i % 1 will also always be 0 unless the number isn’t a whole number
Darryl Dixon - Piece Digital
@piecedigital
Feb 12 2015 16:43 UTC
function sumPrimes(num) {
  var numSum = 0, primeCheck = [];

  for(i=0; i < num; i++){
    if( i % i !== 0  && i % 1 !== 0 )
      {numSum+=0    else
      {primeCheck.push(i);}
  }
  return primeCheck;
}

sumPrimes(10);
I get 0-9.
Nathan
@terakilobyte
Feb 12 2015 16:44 UTC
you arent’ checking for primeness
what is 7 % 7?
Darryl Dixon - Piece Digital
@piecedigital
Feb 12 2015 16:44 UTC
I don't know how to make a tighter check... They're only supposed to be divisible by 1 and itself.
Nathan
@terakilobyte
Feb 12 2015 16:44 UTC
what is 7 % 1?
don’t even try summing anything up until you can generate a list of prime numbers
Cristián Berríos
@crisberrios
Feb 12 2015 16:45 UTC
@piecedigital that's because any integer is divisible by 1 and itself
Darryl Dixon - Piece Digital
@piecedigital
Feb 12 2015 16:46 UTC
So.. this makes no sense.
Cristián Berríos
@crisberrios
Feb 12 2015 16:46 UTC
nope
Darryl Dixon - Piece Digital
@piecedigital
Feb 12 2015 16:47 UTC
Everything is a prime if this is what a prime is.
Cristián Berríos
@crisberrios
Feb 12 2015 16:47 UTC
there are many ways to check for primes
Darryl Dixon - Piece Digital
@piecedigital
Feb 12 2015 16:47 UTC
and all I have to go off of is that they're divisible by 1 and itself.
Cristián Berríos
@crisberrios
Feb 12 2015 16:47 UTC
the bruteforce one is divide the input by all numbers between 1 and number/2
and if input%number ==0 for any division then it's not a prime
but then you can start making some optimizations
like... if the number is even, don't check it at all since all even numbers > 2 aren't primes
and then, you can only check if they are divisible by prime factors
because, following logic
why check if it's divisible by 9, 18, 27... if you can only check if it's divisible by 3
and so on...
Nathan
@terakilobyte
Feb 12 2015 16:52 UTC
any number is divisible by 1 and itself
all whole numbers are
only primes are uniquely divisible by 1 and themselves, they have no other divisors
Darryl Dixon - Piece Digital
@piecedigital
Feb 12 2015 16:53 UTC
"A prime number is defined as having only two divisors, 1 and itself."
So that means this description isn't thorough?
Nathan
@terakilobyte
Feb 12 2015 16:53 UTC
it’s thorough
Cristián Berríos
@crisberrios
Feb 12 2015 16:54 UTC
also, that's why 1 isn't a prime
Nathan
@terakilobyte
Feb 12 2015 16:54 UTC
that is the mathematical definition ofa prime
Cristián Berríos
@crisberrios
Feb 12 2015 16:54 UTC
(I thought it was)
because it has only 1 divisor
not 2
Nathan
@terakilobyte
Feb 12 2015 16:54 UTC
yeah if you read the more info I put that info in there for math geeks :)
@piecedigital how many divisors does 4 have?
Maxim Orlov
@Maximization
Feb 12 2015 16:55 UTC
@piecedigital a prime number has ONLY two divisors, 1 and itself, a non-prime number has MANY divisors, including 1 and itself
checking divisibility by 1 and itself will give you all numbers, you need to check that those are the only divisors
Nathan
@terakilobyte
Feb 12 2015 16:56 UTC
what whole numbers can I divide 4 by, that wil result in another whole number?
Maxim Orlov
@Maximization
Feb 12 2015 16:56 UTC
all apples are fruits but not all fruits are apples
Darryl Dixon - Piece Digital
@piecedigital
Feb 12 2015 16:57 UTC
1 & 2?
Suzanne Atkinson
@AdventureBear
Feb 12 2015 16:57 UTC
If i%x =0 , x = 2 through i-1, then it is not a prime. You need to check the middle
This may be the only useful advice I ever give, lol.
Nathan
@terakilobyte
Feb 12 2015 16:58 UTC
4 is divisible by 1, 2, and 4
right?
Darryl Dixon - Piece Digital
@piecedigital
Feb 12 2015 16:58 UTC
yeah.
Nathan
@terakilobyte
Feb 12 2015 16:58 UTC
so it’s not only divisible by 1 and itself
an interesting thing about numbers, is they can all be broken down into prime factors
which is useful because when we’re checking if a number is prime, we only need to iterate over known prime numbers
I could immediately tell 4 isn’t prime if I did if (4 % 2 === 0)
bam, it is, and it fails
but 5, oh beatiful 5
if (5 % 2 === 0) // false
if (5 % 3 === 0) // false
5 is prime
it must be
Cristián Berríos
@crisberrios
Feb 12 2015 17:01 UTC
more so, we only need to iterate over known prime numbers up to the square root of the input number (well, the closest number, since if it has a square root then it's not prime)
Nathan
@terakilobyte
Feb 12 2015 17:02 UTC
right
though you can be forgiven if you just check all your primes for now
though it can be illustrated with 11 (which is prime) and 12 (which clearly is not, the only even prime number is 2)
sqrt(11) is like 3.yada
sqrt(12) is 3.alittlebiggeryada
11 % 3 === 0 // false done, prime
12 % 3 === 0 // true done, not prime
though 12 would have failed checking it against 2
let sal explain it
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 18:06 UTC
stuck again
allare
@allare
Feb 12 2015 18:06 UTC
I need a help. If I have a link to my style.css placed just after the bootstrap.css, doesn't any css code I write in style file override the css code in the bootstrap file?
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 18:06 UTC
var cashRegister = {
    total:0,
    //Dont forget to add your property
    add: function(itemCost) {
        this.total +=  itemCost;
        var lastTransactionAmount = itemCost;
    },
    scan: function(item,quantity) {
        switch (item) {
        case "eggs": this.add(0.98 * quantity); break;
        case "milk": this.add(1.23 * quantity); break;
        case "magazine": this.add(4.99 * quantity); break;
        case "chocolate": this.add(0.45 * quantity); break;
        }
        return true;
    },
    //Add the voidLastTransaction Method here
    voidLastTransaction: function(){
        this.total - this.lastTransactionAmount;
};

cashRegister.scan('eggs',1);
cashRegister.scan('milk',1);
cashRegister.scan('magazine',1);
cashRegister.scan('chocolate',4);

//Void the last transaction and then add 3 instead
voidLastTransaction();
cashRegister.scan('chocolate',3);
//Show the total bill
console.log('Your bill is '+cashRegister.total);
Can anyone find the problem?
@allare yes
Cristián Berríos
@crisberrios
Feb 12 2015 18:08 UTC
@allare CSS is cascading style sheet, so everything you write after the previous css is overridden
of course, taking into account selector specificity
voidLastTransaction: function(){
this.total - this.lastTransactionAmount;
allare
@allare
Feb 12 2015 18:10 UTC
@AmmarAliShah I think you need to put object name cashRegister before voidLastTranscation(). Like,
Cristián Berríos
@crisberrios
Feb 12 2015 18:10 UTC
you aren't assigning the value
allare
@allare
Feb 12 2015 18:10 UTC
cashRegister.voidLastTransaction();
Cristián Berríos
@crisberrios
Feb 12 2015 18:11 UTC
also that
allare
@allare
Feb 12 2015 18:13 UTC
@crisberrios I am trying to change the css code for h1 element in my style.css but it shows no effect. I delete the bootstrap file link to see if it makes any difference, but it doesn't work either.
Perhaps the problem with my file management.
Cristián Berríos
@crisberrios
Feb 12 2015 18:14 UTC
can you link some code? like the header of the site and the code inside your custom css
also where h1 is located
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 18:15 UTC
now?
var cashRegister = {
    total:0,
    //Dont forget to add your property
    add: function(itemCost) {
        this.total +=  itemCost;
        var lastTransactionAmount = itemCost;
    },
    scan: function(item,quantity) {
        switch (item) {
        case "eggs": this.add(0.98 * quantity); break;
        case "milk": this.add(1.23 * quantity); break;
        case "magazine": this.add(4.99 * quantity); break;
        case "chocolate": this.add(0.45 * quantity); break;
        }
        return true;
    },
    //Add the voidLastTransaction Method here
    voidLastTransaction: function(){
        this.total - this.lastTransactionAmount;
    },
};

cashRegister.scan('eggs',1);
cashRegister.scan('milk',1);
cashRegister.scan('magazine',1);
cashRegister.scan('chocolate',4);

//Void the last transaction and then add 3 instead
cashRegister.voidLastTransaction();
cashRegister.scan('chocolate',3);
//Show the total bill
console.log('Your bill is '+cashRegister.total);
allare
@allare
Feb 12 2015 18:16 UTC
If my h1 element is lying inside a multiple divs, can I target h1 just by specifying h1 element, or do I need to specify all the divs? I just have one h1 element right now.
Nathan
@terakilobyte
Feb 12 2015 18:16 UTC
what’s the point of voidlasttransaction?
and what does it actually do?
vs what you may think it currently does
@allare if is is the only h1 element you could grab it by $(‘h1’)
if it’s not, well, that’s what ids are for
unless you want to crawl over your dom
Nathan
@terakilobyte
Feb 12 2015 18:18 UTC
@AmmarAliShah I know the exercise
I’m asking you
not them
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 18:18 UTC
@terakilobyte oh!
allare
@allare
Feb 12 2015 18:18 UTC
I am not sure if it's strict rule, but I think you shouldn't put comma at the end of voidLastTransaction function inside cashRegister object. @AmmarAliShah
Cristián Berríos
@crisberrios
Feb 12 2015 18:18 UTC
@allare I think it's an specificity problem
ComunidadTIC user
@usrcomunidadtic
Feb 12 2015 18:18 UTC
Hi All
Cristián Berríos
@crisberrios
Feb 12 2015 18:18 UTC
are you using bootstrap classes?
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 18:19 UTC
@terakilobyte it's suppose to remove the last transiction
Nathan
@terakilobyte
Feb 12 2015 18:19 UTC
ok
Cristián Berríos
@crisberrios
Feb 12 2015 18:19 UTC
let's say bootstrap defines #header h1 { ....}
Nathan
@terakilobyte
Feb 12 2015 18:19 UTC
and what does it do now?
Cristián Berríos
@crisberrios
Feb 12 2015 18:19 UTC
then you define h1 {....}
bootstrap is being more specific
(Just guessing without seeing the actual code)
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 18:20 UTC
@terakilobyte it subtracts last transition amount from total
which = to the itemCost
allare
@allare
Feb 12 2015 18:20 UTC
I will go back and start again. Luckily I haven't written too many codes, so it's easy
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 18:20 UTC
so it subtracts the itemCost from the total
Nathan
@terakilobyte
Feb 12 2015 18:21 UTC
does it really?
I mean, it looks like it does
but what’s the resulting value?
voidLastTransaction: function(){
        this.total - this.lastTransactionAmount; // if this.total = 10 and this.lastTransaction =5...
    }, // the last property of an object literal shouldn’t have a comma after it
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 18:23 UTC
@terakilobyte comma removed
and we get 5
5 is our new total
Nathan
@terakilobyte
Feb 12 2015 18:24 UTC
so if total was 10, and lastTransactionAmount is 5, sure, the result of that expression is 5
but our new total is not a new total at all
because you aren’t retaining the result of that expression
ComunidadTIC user
@usrcomunidadtic
Feb 12 2015 18:24 UTC
code this.total = this.total - this.lastTransactionAmount;
allare
@allare
Feb 12 2015 18:25 UTC
Yeah, @usrcomunidadtic is right
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 18:25 UTC
@terakilobyte but @usrcomunidadtic doesn't work either
Cristián Berríos
@crisberrios
Feb 12 2015 18:26 UTC
@usrcomunidadtic @terakilobyte it's trying to explain so @AmmarAliShah comes up with the solution :P
Nathan
@terakilobyte
Feb 12 2015 18:26 UTC
a = 1;
b = 2;
b - a; //the value of this expression is 1
b; // the value of b is still 2
b -= a; // hey javascript subtract the value of a from b and assign the result of that
// expression to b for me if you please
ComunidadTIC user
@usrcomunidadtic
Feb 12 2015 18:26 UTC
ups! sorry
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 18:27 UTC
@terakilobyte oh so i was not using the assignment operator
ComunidadTIC user
@usrcomunidadtic
Feb 12 2015 18:27 UTC
He has to understand that " -= " is a short path for x = x - y
Nathan
@terakilobyte
Feb 12 2015 18:27 UTC
right
javascript won’t change values unless you tell it to
ComunidadTIC user
@usrcomunidadtic
Feb 12 2015 18:27 UTC
The function, return the result of the last operation?
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 18:28 UTC
@terakilobyte but now my result is Nan
Nathan
@terakilobyte
Feb 12 2015 18:28 UTC
and that’s ok
let’s see your new, more correct code
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 18:29 UTC
var cashRegister = {
    total:0,
    //Dont forget to add your property
    add: function(itemCost) {
        this.total +=  itemCost;
        var lastTransactionAmount = itemCost;
    },
    scan: function(item,quantity) {
        switch (item) {
        case "eggs": this.add(0.98 * quantity); break;
        case "milk": this.add(1.23 * quantity); break;
        case "magazine": this.add(4.99 * quantity); break;
        case "chocolate": this.add(0.45 * quantity); break;
        }
        return true;
    },
    //Add the voidLastTransaction Method here
    voidLastTransaction: function(){
        this.total -= this.lastTransactionAmount;
    }
};

cashRegister.scan('eggs',1);
cashRegister.scan('milk',1);
cashRegister.scan('magazine',1);
cashRegister.scan('chocolate',4);

//Void the last transaction and then add 3 instead
cashRegister.voidLastTransaction();

//Show the total bill
console.log('Your bill is '+cashRegister.total);
Cristián Berríos
@crisberrios
Feb 12 2015 18:31 UTC
@AmmarAliShah Now, you have to check where the "this.variable" are pointing
allare
@allare
Feb 12 2015 18:31 UTC
There shouldn't be var declaration in lastTransaction amount in object cashRegister. Instead we put 'this'
Putting var will make it private.
Cristián Berríos
@crisberrios
Feb 12 2015 18:32 UTC
well, not private, but within the scope of add :P
allare
@allare
Feb 12 2015 18:33 UTC
Yeah, my technical explanation is weak. I hope it is correct. :)
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 18:33 UTC
@allare that was it
thank you all
@terakilobyte you're a good teacher
Nathan
@terakilobyte
Feb 12 2015 18:34 UTC
@AmmarAliShah thanks
allare
@allare
Feb 12 2015 18:34 UTC
:)
Nathan
@terakilobyte
Feb 12 2015 18:34 UTC
let’s see the entire, working code now
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 18:35 UTC
var cashRegister = {
    total:0,
    //Dont forget to add your property
    add: function(itemCost) {
        this.total +=  itemCost;
        this.lastTransactionAmount = itemCost;
    },
    scan: function(item,quantity) {
        switch (item) {
        case "eggs": this.add(0.98 * quantity); break;
        case "milk": this.add(1.23 * quantity); break;
        case "magazine": this.add(4.99 * quantity); break;
        case "chocolate": this.add(0.45 * quantity); break;
        }
        return true;
    },
    //Add the voidLastTransaction Method here
    voidLastTransaction: function(){
        this.total -= this.lastTransactionAmount;
    }
};

cashRegister.scan('eggs',1);
cashRegister.scan('milk',1);
cashRegister.scan('magazine',1);
cashRegister.scan('chocolate',4);

//Void the last transaction and then add 3 instead
cashRegister.voidLastTransaction();
cashRegister.scan('chocolate',3);
//Show the total bill
console.log('Your bill is '+cashRegister.total);
Nathan
@terakilobyte
Feb 12 2015 18:36 UTC
@allare you should never declare variables without var
allare
@allare
Feb 12 2015 18:38 UTC
Haha, I simply meant to say " REPLACE 'var' with 'this', but decided to be little explanatory like you @terakilobyte
Cristián Berríos
@crisberrios
Feb 12 2015 18:38 UTC
@AmmarAliShah the code is "still" wrong
Nathan
@terakilobyte
Feb 12 2015 18:38 UTC
@allare if you find yourself declaring global variables from within functions, go find where you need that variable or property in the lowest scope possible and explicity define it there
Cristián Berríos
@crisberrios
Feb 12 2015 18:39 UTC
@AmmarAliShah even you implicitly create lastTransactionAmount when using add, if you call voidLastTransaction before using add it will cause an error since the property is undefined.
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 18:39 UTC
This message was deleted
Cristián Berríos
@crisberrios
Feb 12 2015 18:39 UTC
it should be declared the same way as total
Nathan
@terakilobyte
Feb 12 2015 18:39 UTC
^
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 18:40 UTC
@crisberrios oh i see
allare
@allare
Feb 12 2015 18:40 UTC
@terakilobyte :) Thank you.
Nathan
@terakilobyte
Feb 12 2015 18:40 UTC
@allare np. It may seem silly in the context of this simple cash register object
you may say “but I’ll never call lastTransactionAmount before I call add!"
and you may never do so, but what about someone else?
AmmarAliShah @AmmarAliShah is trying to get the last bit done
Nathan
@terakilobyte
Feb 12 2015 18:43 UTC
I forget where I read it, but in my readings I remember a warning that said to always write your code as if someone else was going to use it and couldn’t see how it actually works inside
allare
@allare
Feb 12 2015 18:45 UTC
@terakilobyte how long have you been involved in programming?
Nathan
@terakilobyte
Feb 12 2015 18:45 UTC
takign it seriously? about 3 months
reading differnet programming books but never doing anything useful? about 5-6 years or so
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 18:46 UTC
I'm going to the last bit in the morning
I can't get my brain to think right now
Nathan
@terakilobyte
Feb 12 2015 18:46 UTC
before I got into web development I had written an ant colony system in java and an iphone app that can solve any solvable sudoku puzzle, but those were years ago
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 18:46 UTC
although its just 2 lines of code
allare
@allare
Feb 12 2015 18:46 UTC
Wow, but seeing your blog articles, I felt like you have many years of experiences.
Darryl Dixon - Piece Digital
@piecedigital
Feb 12 2015 18:47 UTC
He's just a really intelligent guy.
Nathan
@terakilobyte
Feb 12 2015 18:47 UTC
@AmmarAliShah what 2 lines of code?
allare
@allare
Feb 12 2015 18:48 UTC
@piecedigital I bet he is.
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 18:48 UTC
I did it
I wrote the final piece of code
Cristián Berríos
@crisberrios
Feb 12 2015 18:49 UTC
:thumbsup:
congrats!
Nathan
@terakilobyte
Feb 12 2015 18:49 UTC
awesome
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 18:49 UTC
I'm very tired now
AmmarAliShah @AmmarAliShah is going to sleep
Nathan
@terakilobyte
Feb 12 2015 18:50 UTC
g'night
allare
@allare
Feb 12 2015 18:50 UTC
Goodnight @AmmarAliShah
Nathan
@terakilobyte
Feb 12 2015 18:50 UTC
make it a nap!
team meeting later
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 18:51 UTC
@terakilobyte I sleep for like 6 hours
allare
@allare
Feb 12 2015 18:51 UTC
Actually, I have to say goodnight too. Thank you @crisberrios and @terakilobyte
Ammar Ali Shah
@AmmarAliShah
Feb 12 2015 18:51 UTC
I sleep around 1 and wake up by 6:30
but today I'm going to go to sleep right now(11:53 PM)
Bin Ury
@teddy-error
Feb 12 2015 20:21 UTC
I really need help understanding node scoping
Bin Ury
@teddy-error
Feb 12 2015 20:28 UTC
Say I declare a function I want to export
module.exports = function(data, callback) {
  // i declare a list
  var mylist = []
  // i iterate over the data
  data.forEach(function(x) {mylist.push(x)});
return callback(null, mylist);
}
i dont understand but it seems like when I call mylist from inside, no changes are made to the actual mylist declared on line 3
Cristián Berríos
@crisberrios
Feb 12 2015 20:30 UTC
missing; 1st thing
;
Bin Ury
@teddy-error
Feb 12 2015 20:30 UTC
i mean that’s psuedo code
var fs = require('fs');
module.exports = function(dir, filter, callback) {
    var filtered_list = [];
    fs.readdir(dir, function(err, list) {
        if (err) return callback(err);
        list.forEach(function(x) {
            if (x.indexOf(filter) !== -1 && x.length > filter.length) filtered_list.push(x);
        });
    });
    console.log(filtered_list);
    callback(null, filtered_list);
};
Cristián Berríos
@crisberrios
Feb 12 2015 20:34 UTC
shouldn't it be dir.forEach?
since the 2nd parameter of readdir should be the callback right?
nah
Bin Ury
@teddy-error
Feb 12 2015 20:35 UTC
well, you call readdir by passing it a file directory path and it returns the list into your callback
Cristián Berríos
@crisberrios
Feb 12 2015 20:35 UTC
it's list
I read it and it's ok
Bin Ury
@teddy-error
Feb 12 2015 20:35 UTC
i am like 70% sure it’s a scope issue
Cristián Berríos
@crisberrios
Feb 12 2015 20:35 UTC
what is happening
is an async issue
you are logging filtered_list before you actually have the list
Bin Ury
@teddy-error
Feb 12 2015 20:36 UTC
var mymodule = require('./dirmodule.js');
var filedir = process.argv[2];
var filter = process.argv[3];
mymodule(filedir, filter, function(err, arr) {
    if(err)
        console.log(err);

    arr.forEach(function(x) {
        console.log(x);
    });
});
this is the Main
Cristián Berríos
@crisberrios
Feb 12 2015 20:37 UTC
put console.log inside the callback function of fs.readdir
and pass callback as an argument
and call it from inside too
the birth of a callback hell :P
Bin Ury
@teddy-error
Feb 12 2015 20:42 UTC
hmmmmmmmmmm…….
Bin Ury
@teddy-error
Feb 12 2015 20:51 UTC
var fs = require('fs');
module.exports = function(dir, filter, callback) {
    fs.readdir(dir, function(err, list) {
        filtered_list = [];
        if (err) return callback(err);
        list.forEach(function(x) {
            if (x.indexOf(filter) !== -1 && x.length > filter.length) filtered_list.push(x);
        });
        return callback(null, filtered_list);
    });  
};
Cristián Berríos
@crisberrios
Feb 12 2015 20:51 UTC
don't return the callback
call it as a function
Bin Ury
@teddy-error
Feb 12 2015 20:52 UTC
in either case, that fixed the issue
Cristián Berríos
@crisberrios
Feb 12 2015 20:52 UTC
callback(null, filtered_list);
unless you want to return the value of calling the callback function
but convention is just calling the function
Bin Ury
@teddy-error
Feb 12 2015 20:53 UTC
you’re right. it returns void either way.
do you see how i had to move everything into the readdir callback?
Cristián Berríos
@crisberrios
Feb 12 2015 20:54 UTC
yes
that's a good excuse to use async.waterfall
Bin Ury
@teddy-error
Feb 12 2015 20:54 UTC
i dont know what that is yet :ok_woman:
Cristián Berríos
@crisberrios
Feb 12 2015 20:54 UTC
a library for managing the flow of async functions
you can do series, parallel, waterfall (when one function returns you pass the data to the next one) etc...
Bin Ury
@teddy-error
Feb 12 2015 20:56 UTC
i wil look at that.
I still dont understand why declaring my list outside of readdir somehow makes it inaccessible from within a different callback
i thought javascript would remember the context in which the function was declared
Cristián Berríos
@crisberrios
Feb 12 2015 20:58 UTC
hmm but did you have to move filtered list inside?
or just the callback
what did you move first?
Bin Ury
@teddy-error
Feb 12 2015 20:59 UTC
oh I guess just the callback needed to be inside readdir
this is so weird to me
module.exports = function(dir, filter, callback) {

    fs.readdir(dir, function(err, list) {
        filtered_list = [];
        if (err) return callback(err);
        list.forEach(function(x) {
            if (x.indexOf(filter) !== -1 && x.length > filter.length) filtered_list.push(x);
        });   
    });
    callback(null, filtered_list);    
};
when done this way we get filtered_list is undefined
so its going past readdir without going into the callback
Suzanne Atkinson
@AdventureBear
Feb 12 2015 21:02 UTC
Hey guys here is something basic I am confused about. When writing a .js file, if I create a named function, does it only run when called? or does it run when the flow of code reaches it? I feel like I see both versions. In my prior coding experience functions only ran when called, they were outside the loop of the main program. However with JS sometimes I feel like I am seeing functions (maybe they are anonymous) that run when encountered in the program flow from top to bottom of the code. I am currently reading eloquent JS chapter on functions but just thougth I'd ask.
Cristián Berríos
@crisberrios
Feb 12 2015 21:04 UTC
only when called
but in some examples they are called right after defined
adding () at the end
Suzanne Atkinson
@AdventureBear
Feb 12 2015 21:04 UTC
OH! well that explains some things then.
Bin Ury
@teddy-error
Feb 12 2015 21:15 UTC
thanks for your help @crisberrios