These are chat archives for FreeCodeCamp/Help

6th
Feb 2015
Suzanne Atkinson
@AdventureBear
Feb 06 2015 00:04
can anyone help me with a node.js question from nodeschool.io 'learnyounode' challenge 4?
nm
Jordan Wood
@jordanw
Feb 06 2015 00:07
@AdventureBear You got it?
Suzanne Atkinson
@AdventureBear
Feb 06 2015 00:07
Yeah I got the right answer. My question had to do with getting the file contents returned so I could manipulate it outside of the function call .
the correct anser didn't require it, but it seems like something you'd want to be able to do
here's my code that worked
var fs = require('fs');

var buf = fs.readFile(process.argv[2], 'utf8', function(err,data) {
    if (err) throw err;
    //  console.log(data);
    var newLines = data.toString().split(/\n/).length-1;
    console.log(newLines);
//   return data;

} );
here's what I wanted to do
var fs = require('fs');

var buf = fs.readFile(process.argv[2], 'utf8', function(err,data) {
    if (err) throw err;
    return data;
} );
 console.log(buf);
var newLines = data.toString().split(/\n/).length-1;
console.log(newLines);
@JordanW if you have any ideas that's great, but I'll just keep moving on for now so I can kepe learnign!
oops, that code above should include the return data; within the read File call
Jordan Wood
@jordanw
Feb 06 2015 00:11
@AdventureBear By moving it outside it won't be asynchronous
Suzanne Atkinson
@AdventureBear
Feb 06 2015 00:12
Hmm...never thought of that...so it has to stay inside it's own function call?
Jordan Wood
@jordanw
Feb 06 2015 00:13
Yeah. It goes off and does whatever it needs to inside that function, without blocking the main loop.
Suzanne Atkinson
@AdventureBear
Feb 06 2015 00:13
interesting! that makes a lot of sense.
what if the rest of the program needs some information from inside of it? eventually...?
Jordan Wood
@jordanw
Feb 06 2015 00:13
That's why you get nesting. You have to wait for one function to complete before the surrounding function can finish.
Suzanne Atkinson
@AdventureBear
Feb 06 2015 00:14
like a bunch of asynchronous calls to read some files, then from the results of all of those you could do something?
just thinking out loud here, not something i need to do right now
and thanks
Jordan Wood
@jordanw
Feb 06 2015 00:17
@AdventureBear You're welcome. Good luck.
Suzanne Atkinson
@AdventureBear
Feb 06 2015 00:34
thanks. got the filtered filenames one. This is a nice change of pace from the codeschool stuff
@brndnb I'm on challenge 5 of the initial learnyounode course. I kind of like the slight change of pace...less handholding. For the first time with js I'm reading documentation and (kind of) know what to do when the instructions say for example, "You might want to read the node.js documentation on path.extname" And I actually can read it and figure out how to implement it!
Nathan
@terakilobyte
Feb 06 2015 00:40
@philsco you get sorted out?
and everyone else doing ok?
Suzanne Atkinson
@AdventureBear
Feb 06 2015 00:46
yes I think so! I'm on ch 5
I'm digging it. I like reading from the file system, makes me feel powerful
Suzanne Atkinson
@AdventureBear
Feb 06 2015 00:52
@terakilobyte have you done bug clinic? https://github.com/othiym23/bug-clinic
Nathan
@terakilobyte
Feb 06 2015 00:54
some of it, yep
Alex Dixon
@alex-dixon
Feb 06 2015 10:10
Anyone available to look at like 10 lines of code
Ben Clist
@BenClist
Feb 06 2015 10:15
go for it @alex-dixon
Alex Dixon
@alex-dixon
Feb 06 2015 10:15
@BenClist Lol thanks man…I just changed one thing and everything works haha
  var arr = [];
  for (var i = 0; i < str.length; i++) {
    if (/^\d*[13579]$/.test(str[i]) && /^\d*[13579]$/.test(str[i+1]) ) {
      arr.push(str[i]);
      arr.push("-");
    } else {
      arr.push(str[i]);
    }
  }
  // code goes here  
  return arr.join(""); 

}
i had the && as ===
once I changed that :+1:
Ben Clist
@BenClist
Feb 06 2015 10:17
haha, no worries :)
Hannan Ali
@abdulhannanali
Feb 06 2015 15:06
HELLO Campers!
I am back
Ammar Ali Shah
@AmmarAliShah
Feb 06 2015 15:06
@abdulhannanali Hello!
Cristián Berríos
@crisberrios
Feb 06 2015 15:37
hello!
Evan Davis
@davisec52
Feb 06 2015 16:12
Hi. The code sample below tests whether a number is a power of 2. However, my attempts to add a "false" statement causes it to indicate false for all numbers. I'm probably just not seeing the obvious, but I'd appreciate any suggestions.

function func(num) {
    if(num === 1) {
        return true;
    }
    //else{return false;}

    return func(num/2);
}
func(16);
Cristián Berríos
@crisberrios
Feb 06 2015 16:13
let's see
I think you need another if statement
else if(num<0) {
return false; }
also... hmm
as it's a recursive function I think it should be something like return func(num/2) || false;
Evan Davis
@davisec52
Feb 06 2015 16:18
@crisberrios Hey, Cris! Tried that, but gives false.
let me try that
@crisberrios Thank you. That was it.
Cristián Berríos
@crisberrios
Feb 06 2015 16:20
@davisec52 it was num<1
sorry for the typo
Evan Davis
@davisec52
Feb 06 2015 16:21
Ok, but it works with else if(num > 0) {
return func(num/2)||false;
Cristián Berríos
@crisberrios
Feb 06 2015 16:21
function func(num) {
    if(num === 1) {
        return true;
    }
    else if (num < 1)
      return false;
    //else{return false;}

    return func(num/2);
}
func(16);
Evan Davis
@davisec52
Feb 06 2015 16:22
Thank you. That's what I was shooting for.
Nathan
@terakilobyte
Feb 06 2015 16:25
return Math.log(num) / Math.log(2) % 1 === 0;
will avoid recursion
Evan Davis
@davisec52
Feb 06 2015 16:27
@terakilobyte Thank you. I'll make myself familiar with Math.log().
I found the Math.pow method but wanted to fix the problem above without it.
Cristián Berríos
@crisberrios
Feb 06 2015 16:31
2^x = num
x(log(2)) = log(num)
x = log(num) / log (2)
and that has to be an integer
just in case
Nathan
@terakilobyte
Feb 06 2015 16:33
@davisec52 understood
as did I when I solved the problem
did a recursive solution as well
then afterwards remembered I could take a logarithm. But js as of yet doesn't have built in log bases aside from e
Evan Davis
@davisec52
Feb 06 2015 16:36
@terakilobyte My next stop will be the log method. My math skills are a train wreck. This will no doubt help me put the pieces back together.
Nathan
@terakilobyte
Feb 06 2015 16:38
khan academy my friend
he's an excellent teacher
Evan Davis
@davisec52
Feb 06 2015 16:39
@crisberrios Thank you for the formula.
@terakilobyte Ok--this would be your recommend over other online codeschool options such as FrontEnd Masters or Code School?
I've heard of Khan academy but no more than that.
Nathan
@terakilobyte
Feb 06 2015 16:41
hmm
if you would say your Maths are horrible
then yes, I'd recommend khan academy over the others at first
but within a day or two, at least up to the end of the algebra stuff, you'll be good
math really does make programming easier
Jimmy Bryant II
@JimBry
Feb 06 2015 16:42
@davisec52 I second that khan academy is great for developing math skills for free.
Nathan
@terakilobyte
Feb 06 2015 16:42
you can get by without it but you'll enjoy programming, and math, a lot more if you brush up
Evan Davis
@davisec52
Feb 06 2015 16:43
@terakilobyte Yes, I can see that math really helps. But I feel I am capable of math rehabilitation.
@JimBry @terakilobyte Ok, then. I'll check out Khan academy.
Jimmy Bryant II
@JimBry
Feb 06 2015 16:44
I'm back in college and taking all math courses just to get it out of the way :smile:
Nathan
@terakilobyte
Feb 06 2015 16:44
and then you can be mr. smarmy pants like me!
http://cdn.meme.am/instances/500x/58890982.jpg
Evan Davis
@davisec52
Feb 06 2015 16:45
hah!
Ammar Ali Shah
@AmmarAliShah
Feb 06 2015 16:46
whats up?
Jimmy Bryant II
@JimBry
Feb 06 2015 17:40
I'm having trouble getting "FizzBuzz" to populate when the number is divisible by both 3 and 5.
for(var number = 0; number <= 100; number++) {
  if(number % 3 == 0)
    console.log("Fizz");
  else if(number % 5 ==0)
    console.log("Buzz");
  else if(number % 3 == 0 || number % 5 == 0)
    console.log("FizzBuzz");
  else
    console.log(number);
}
Cristián Berríos
@crisberrios
Feb 06 2015 17:41
start backwards
1st check for fizzbuzz then cascade to the other cases
Jimmy Bryant II
@JimBry
Feb 06 2015 17:41
Okay I'll try that!
Cristián Berríos
@crisberrios
Feb 06 2015 17:42
also it should be && instead of ||
Jimmy Bryant II
@JimBry
Feb 06 2015 17:42
It says to use the OR logical operator
Cristián Berríos
@crisberrios
Feb 06 2015 17:42
and also you can check for number % 15 == 0
it doesn't make sense using the OR operator there
Jimmy Bryant II
@JimBry
Feb 06 2015 17:44
This just changed all the divisible numbers to "Fizz Buzz"
for(var number = 0; number <= 100; number++) {
  if(number % 3 == 0 || number % 5 == 0)
    console.log("FizzBuzz");
  else if(number % 3 == 0)
    console.log("Fizz");
  else if(number % 5 ==0)
    console.log("Buzz");
  else
    console.log(number);
}
Cristián Berríos
@crisberrios
Feb 06 2015 17:44
because of the or operator
Jimmy Bryant II
@JimBry
Feb 06 2015 17:45
You're right, I wonder why they said to use the || operator then?
This was the answer using the || operator
for (var n = 1; n <= 100; n++) {
  var output = "";
  if (n % 3 == 0)
    output += "Fizz";
  if (n % 5 == 0)
    output += "Buzz";
  console.log(output || n);
}
Cristián Berríos
@crisberrios
Feb 06 2015 17:50
not a particularly good use of || IMHO
Nathan
@terakilobyte
Feb 06 2015 17:52
perfectly valid
and a common pattern from what I've seen
I use the crap of it
Jimmy Bryant II
@JimBry
Feb 06 2015 17:53
I'll take that into consideration, I'm still learning JS so I don't really have an opinion on best practices yet I'm still in the whatever gets the job done stage :smile:
Nathan
@terakilobyte
Feb 06 2015 17:53
it's saying console, I'd really like you to log one of these two things
first, if output isn't false, log output
if output is falsy, go ahead and log n
remember || won't evaluate the right side if the left side is truthy
Jimmy Bryant II
@JimBry
Feb 06 2015 17:55
Makes sense to me haha I had to refresh on logical operators while doing this exercise.
Cristián Berríos
@crisberrios
Feb 06 2015 17:55
@terakilobyte I'm not against the return pattern but summing chunks of strings, since strings have to be recreated each time as they are immutable.
Nathan
@terakilobyte
Feb 06 2015 17:55
"Here's your receipt and survey entry code. Fill out the short survey for your chance to win more JS tidbits!"
oh it's not optimized, sure
it looks neat but I'd be explicit and do checks to see if n % 5 && n % 3, then n % 3, then n % 5
because early returns will save time
but then again, computers are beastly nowadays and we're talking bytes here
Cristián Berríos
@crisberrios
Feb 06 2015 17:58
I don't know how powerful, my laptop craps out with some pens xD
Nathan
@terakilobyte
Feb 06 2015 17:58
well that's from the css engines and all that other poop :P
we do pay a price for all of that
Cristián Berríos
@crisberrios
Feb 06 2015 17:58
and I won't get started on my subpar smartphone
Nathan
@terakilobyte
Feb 06 2015 18:01
I'm betting your smartphone has enough power to guide a spaceship to the moon
Cristián Berríos
@crisberrios
Feb 06 2015 18:02
sure, one IC from my webcam could run all the Apollo missions at the same time while still having a Skype videoconference with my granma
Derrick
@whyderrick
Feb 06 2015 19:56

hey all,

question on the jquery exercises, number 9. I got it to work but I'm wondering if it's the most elegant way to do it.
$("select").val("target1").attr("selected");

I'm also wondering why the hint specifically encourages you to use $("#location1 select") instead of just $("select") Any insight on why it's necessary or beneficial?

(I know it's "target2", but I like to poke around from time to time)
Cristián Berríos
@crisberrios
Feb 06 2015 19:59
select will select all select even outside #location1
even if you don't have any other select always try to be more specific
so you do less DOM traversal
and slow down the page
Derrick
@whyderrick
Feb 06 2015 20:11
Thanks.
I figured you always want the simplest, lowest weight selector that gets the job done right. How does the less specific selector create more traversal in this case?
Cristián Berríos
@crisberrios
Feb 06 2015 20:32
id selectors are faster than class selectors, but most important is that should reflect what you want to select
like... do you want to select the class inside #location or just any class named "select" ?
Derrick
@whyderrick
Feb 06 2015 20:49

I think I'm trying to apply things I read about CSS selectors when I was just learning CSS to jQuery. I always had it that the higher the specificity value, the slower it would be. Basically I had in my head that element selectors are always fastest, then class, then id, then inline (*shudder).

If they're equivalent in performance (which the docs I'm seeing seem to suggest), then I get it. I just thought they weren't.

Your second question is a readability consideration that has never mattered before since no one else has had to read and work with my CSS.

Derrick
@whyderrick
Feb 06 2015 21:00
Yup. jQuery documentation vs slightly dated CSS advice: