These are chat archives for FreeCodeCamp/Help

24th
Feb 2015
Oliver
@Dadsaster
Feb 24 2015 00:09
Can someone explain the logic of this numeric sort function?
//function for numberical sorting
function sortNumber(a,b) {
    return a - b;
}
for sorting an array of numbers
Danny Fritz
@dannyfritz
Feb 24 2015 00:36
is b is larger than a it will return negative
if a is larger than b it will return positive
Matthew Harames
@Harames
Feb 24 2015 01:19
@crisberrios You still here because my code isn't working for the Title Case a Sentence
No OK then I have to leave
Cristián Berríos
@crisberrios
Feb 24 2015 01:34
I'm back
you should post the code in question :D
Matthew Harames
@Harames
Feb 24 2015 01:40
@crisberrios I actually figured it out
I would show you my end result, but bonfire doesn't save your code
@crisberrios Here was my original code though
function titleCase(str) {
  str = str.toLowerCase();
  var test2 = [];
  var test = str.split(" ");
  for(var i = 0; i < test.length; i++) {
    if(test[i].length === 1) {
      test2[i] = test[i].toUpperCase();
    } else {
      var split = test[i].split("");
      var upper = split[1].toUpperCase();
      var together = split.join("");
      test2[i] = together;
    }
  }
  test2 = test2.join(" ");
  return test2;
}

titleCase("I'm a little tea pot");
I believe I changed it to
Cristián Berríos
@crisberrios
Feb 24 2015 01:42
it wasn't far
Matthew Harames
@Harames
Feb 24 2015 01:42
function titleCase(str) {
  str = str.toLowerCase();
  var test2 = [];
  var test = str.split(" ");
  for(var i = 0; i < test.length; i++) {
    if(test[i].length === 1) {
      test2[i] = test[i].toUpperCase();
    } else {
      var split = test[i].split("");
      split[0] = split[0].toUpperCase();
      var together = split.join("");
      test2[i] = together;
    }
  }
  test2 = test2.join(" ");
  return test2;
}

titleCase("I'm a little tea pot");
Cristián Berríos
@crisberrios
Feb 24 2015 01:42
another option without splitting is using regex
if you want to try going down that road
Matthew Harames
@Harames
Feb 24 2015 01:42
@crisberrios I dislike regex
How do I access arrays in arrays?
Matthew Harames
@Harames
Feb 24 2015 01:53
Never mind I am leaving
Cristián Berríos
@crisberrios
Feb 24 2015 01:55
@PKMNinja arr[i][j]
Nathan
@terakilobyte
Feb 24 2015 07:27
@PKMNinja bonfire saves your code, the end user just can’t see it yet
it will be a part of your profile, every solution you came up with
You can also complete a bonfire multiple times and each solution is saved, in fact
Vince
@Vince33
Feb 24 2015 07:43
I want to make sure I understand a concept of lastIndexOf and IndexOf when passed to filter()
I think I got it just want to be certain
Cristián Berríos
@crisberrios
Feb 24 2015 07:45
what's confusing you?
Vince
@Vince33
Feb 24 2015 07:49
Well if I pass to args to filter, elem and pos, I check that both lastIndexOf and IndexOf are equal to position it is checking that there isn't elem existing at two positions right
positions being indexes
I can get the function to return the value I like but I want to understand it on a deeper level
Cristián Berríos
@crisberrios
Feb 24 2015 07:52
can you give a code example? indexOf and lastIndexOf can point to the same index inside an array or string
solved it earlier today in a pair session and another camper was looking for help later on and it took me a little bit to reconstruct it when I went to resolve it later
Cristián Berríos
@crisberrios
Feb 24 2015 07:56
ok I think I get it
var uniqueArr = newArr.filter(function (elem, pos) {
    return newArr.lastIndexOf(elem)==pos && newArr.indexOf(elem)==pos;
1st you concatenate 2 arrays
so arr1 = [a,b,c,d] and arr2 = [d,e,f,g]
Vince
@Vince33
Feb 24 2015 07:57
yes
Cristián Berríos
@crisberrios
Feb 24 2015 07:57
(note the repeated d)
if an item isn't repeated
then indexOf(a) has to be equal to lastIndexOf(a)
otherwise, the item is in 2 positions within the array, and that would mean that it's in both arrays
Vince
@Vince33
Feb 24 2015 07:58
right
so pos is index as well
Cristián Berríos
@crisberrios
Feb 24 2015 07:59
yes
if you use the filter method
Vince
@Vince33
Feb 24 2015 07:59
yes
Cristián Berríos
@crisberrios
Feb 24 2015 07:59
it will pass every item to the function indicating the element and the position within the array
Vince
@Vince33
Feb 24 2015 08:00
right
It just seems to easy
ok I think I am thinking about it the right way then
thanks it was nagging at me
Cristián Berríos
@crisberrios
Feb 24 2015 08:04
function diff(arr1,arr2) {
    var answer = arr1.filter(function(item) {
        return arr2.indexOf(item) >= 0 ;
    });
    return answer;
}
well, this returns items found in both
Vince
@Vince33
Feb 24 2015 08:06
ok phew
Cristián Berríos
@crisberrios
Feb 24 2015 08:06
but it's kind of similar
Vince
@Vince33
Feb 24 2015 08:07
I saw quite a few uses of indexOf on stack overflow
sometimes too much info is bad though
trying to assimilate it all at once
I really like the filter method
Cristián Berríos
@crisberrios
Feb 24 2015 08:09
and then, after graduating from FCC, you'll start to love some libraries https://lodash.com/docs#difference
Nathan
@terakilobyte
Feb 24 2015 08:09
cough ramda
Vince
@Vince33
Feb 24 2015 08:10
yeah I took a gander it seems like there is some nice shorthands for this
I though about trying to modify the prototype but might be a bit excessive for bonfire
so with ramda data first right
no!
data last
always
Vince
@Vince33
Feb 24 2015 08:13
yeah that s what I meant same difference
hehe
wow yeah these lib could make things alot easier
Cristián Berríos
@crisberrios
Feb 24 2015 08:14
and you can compose them
Vince
@Vince33
Feb 24 2015 08:14
but I guess this is good for the old interview and actual js knowledge
Cristián Berríos
@crisberrios
Feb 24 2015 08:14
so... for the problem using LODASH it would be like...
Vince
@Vince33
Feb 24 2015 08:14
compose them like make your own
?
Cristián Berríos
@crisberrios
Feb 24 2015 08:15
like passing the result of the function to another function
.funcA(_.funcB(a,b),.funcD(f);
you can mix intersection + difference + flatten
Vince
@Vince33
Feb 24 2015 08:16
like a chain
Cristián Berríos
@crisberrios
Feb 24 2015 08:16
hmm not quite
in this case, you use functions as parameters
and build higher order functions
chaining is serializing functions
of course, you can chain and serialize
I mean
compose and hcain
chain
Vince
@Vince33
Feb 24 2015 08:18
yeah kind of get I have to stop my mind from thinking about recurssive calls
I see its different
this is getting fun !
I sort of appreciate alot of php's functions now
Is there any good reading material on higher order functions
sorry my brain is going in alot of diff directions once
Vince
@Vince33
Feb 24 2015 08:27
@crisberrios Hey man thanks again! it is like three in the morning I have to get some zzzz
Cristián Berríos
@crisberrios
Feb 24 2015 08:27
here too
so I think I'm also off for tonight
Vince
@Vince33
Feb 24 2015 08:27
nice another insomniac
night sir and thanks!
Jordan Wood
@jordanw
Feb 24 2015 08:28
Dreaming in JS
Vince
@Vince33
Feb 24 2015 08:28
@jordanw ha
Jordan Wood
@jordanw
Feb 24 2015 08:28
lol. g'night.
Vince
@Vince33
Feb 24 2015 08:28
g night
Evan Davis
@davisec52
Feb 24 2015 16:16
I'm working on the "Where Art Though" Bonfire challenge and having some difficulty. The code sample below
appears to return the correct result for the first input but returns a blank array for second (commented out) input sample. I'd appreciate a little direction on this on. A bit lost on it.

function where(collection, source) {
  var arr = [];
  var obj = {};
  for(var i in source) {
      var x = source[i];
      for(var j = 0; j < collection.length; j++) {
          var y = collection[j];
          for(var p in y) {
              for(var q in x) {
                  if(y.hasOwnProperty(p) === x.hasOwnProperty(q)) {
                      obj[p] = y[p];
                      arr = [obj];

                  }
              }
          }
      }
  }


  //return obj;
  return arr;
}
where([{ first: 'Romeo', last: 'Montague' }, { first: 'Mercutio', last: null }, { first: 'Tybalt', last: 'Capulet' }], { last: 'Capulet' });

//where([{ 'a': 1 }, { 'a': 1 }, { 'a': 1, 'b': 2 }], { 'a': 1 });
Cristián Berríos
@crisberrios
Feb 24 2015 16:20
hey
Evan Davis
@davisec52
Feb 24 2015 16:20
Hi
Cristián Berríos
@crisberrios
Feb 24 2015 16:20
for(var i in source) { var x = source[i];
remember that variables don't have block scope
you must declare them outside the loop
well, I stopped checking at the 3rd loop or so
Evan Davis
@davisec52
Feb 24 2015 16:22
so I should take all of the variables in the loops and declare them at the top?
Cristián Berríos
@crisberrios
Feb 24 2015 16:22
it's really a bad practice to say for(a in b) { for (c in b) { for (d in c) { ...... }}}}
also, since you aren't being expressive on what each variable means
at least do for (person in array) { for (property in person) {}
Evan Davis
@davisec52
Feb 24 2015 16:23
That makes intuitive sense, but I'm just "feeling around" trying to get an idea of how to solve this.
Cristián Berríos
@crisberrios
Feb 24 2015 16:24
to see a general idea on what you're trying to do
Evan Davis
@davisec52
Feb 24 2015 16:24
Here is my understanding of what I need to do: compare properties of an object with the properties of objects within in an array.
Cristián Berríos
@crisberrios
Feb 24 2015 16:25
yeah, that's right
Evan Davis
@davisec52
Feb 24 2015 16:25
So all of those loops were me trying to get at the properties and values to compare them
Cristián Berríos
@crisberrios
Feb 24 2015 16:26
well, without checking further
Evan Davis
@davisec52
Feb 24 2015 16:26
Sort of like trying to eat noodles with your hands tied behind your back'
Cristián Berríos
@crisberrios
Feb 24 2015 16:26
I could say that it's probable that you're just checking values
Evan Davis
@davisec52
Feb 24 2015 16:26
perhaps
Cristián Berríos
@crisberrios
Feb 24 2015 16:26
and not property names + values
since I don't see any Object.keys around
Evan Davis
@davisec52
Feb 24 2015 16:27
ok
Cristián Berríos
@crisberrios
Feb 24 2015 16:30
try to use Object.keys to get property names and refactor the code into "self documenting" variables, so it's easier to understand the intent of the code
and that way it's easier to debug from an outside perspective
Evan Davis
@davisec52
Feb 24 2015 16:31
What are "self documenting" variables?
Cristián Berríos
@crisberrios
Feb 24 2015 16:32
like, if you want to store a person object, you declare personObj =
or a persons array... personsArr =
for (person in personsArr)
weell
well
Evan Davis
@davisec52
Feb 24 2015 16:33
Ok, thank you.
Cristián Berríos
@crisberrios
Feb 24 2015 16:33
better if you omit Obj and Arr depending on the situation
Evan Davis
@davisec52
Feb 24 2015 16:34
Right. Ok, I'll try some solutions using Object.keys.
Germaine Pannell
@germainep
Feb 24 2015 16:47
Hello guys
I was wondering if I could get some help with the Spinal Tap Case bonfire
Oliver
@Dadsaster
Feb 24 2015 16:49
Can you post the description?
Germaine Pannell
@germainep
Feb 24 2015 16:50
Convert a string to spinal case. Spinal case is all-lowercase-words-joined-by-dashes.
Serhii Borovskyi
@serhiicss
Feb 24 2015 16:50
var cashRegister = {
    total: 0,
    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;

        case "magazine": 
            this.add(4.99); 
            break;

        case "chocolate": 
            this.add(0.45); 
            break;

        }
        return true;
    }
};
Can anyone see a syntax problem here?
I get an unexpected token callback on this :(
Cristián Berríos
@crisberrios
Feb 24 2015 16:53
@germainep what have you tried on that bonfire?
@serhiicss let's see
Germaine Pannell
@germainep
Feb 24 2015 16:53
@crisberrios we have gotten all the test cases except the camel case one
Cristián Berríos
@crisberrios
Feb 24 2015 16:54
add: function(itemCost) { this.total += itemCost; };
replace semicolon by a comma
@germainep can you paste the code?
Germaine Pannell
@germainep
Feb 24 2015 16:55
@crisberrios we can't figure out how to separate the words in camel case
yes

```function spinalCase(str) {
// "It's such a fine line between stupid, and clever."
// --David St. Hubbins
var arr= [];
var newStr;
newStr = str.replace(/[\W_]/g, '-').toLowerCase();

return newStr;
}

spinalCase('This Is Spinal Tap');```

Cristián Berríos
@crisberrios
Feb 24 2015 16:56
ticks on their own line before and after
Germaine Pannell
@germainep
Feb 24 2015 16:57
function spinalCase(str) {
  // "It's such a fine line between stupid, and clever."
  // --David St. Hubbins
  var arr= [];
  var newStr;
  newStr = str.replace(/[\W_]/g, '-').toLowerCase();

  return newStr;
}

spinalCase('This Is Spinal Tap');
Cristián Berríos
@crisberrios
Feb 24 2015 16:57
and what is the case that's failing?
Germaine Pannell
@germainep
Feb 24 2015 16:57
camelcase
Cristián Berríos
@crisberrios
Feb 24 2015 16:57
I mean, the test input
Germaine Pannell
@germainep
Feb 24 2015 16:57
thisIsSpinalTap
Cristián Berríos
@crisberrios
Feb 24 2015 16:58
well, in that string, what's the word boundary?
how would you define the point where you want to insert a dash?
Germaine Pannell
@germainep
Feb 24 2015 16:59
any capital letter
of /[A-Z]/
Cristián Berríos
@crisberrios
Feb 24 2015 17:00
no
don't think in regex
just natural language
where should the dashes go?
Germaine Pannell
@germainep
Feb 24 2015 17:00
after each word
Cristián Berríos
@crisberrios
Feb 24 2015 17:00
(assuming the computer doesn't know what a word is)
well... it should be between a lowercase letter followed by an uppercase letter
Germaine Pannell
@germainep
Feb 24 2015 17:01
between a lower case character followed by an uppercase character
Cristián Berríos
@crisberrios
Feb 24 2015 17:01
and you can express that in regex
/([a-z]){1}([A-Z]{1})/
Serhii Borovskyi
@serhiicss
Feb 24 2015 17:02
@crisberrios Thanks!
Germaine Pannell
@germainep
Feb 24 2015 17:02
can you explain {1}
Cristián Berríos
@crisberrios
Feb 24 2015 17:03
and using round parentheses you can save the captured characters and insert the dash in between
like
str.replace(/([a-z]{1})([A-Z]{1})/g, function($1,$2) { return $1 + '-' + $2) });
I might be wrong on the syntax
Oliver
@Dadsaster
Feb 24 2015 17:07
clearly I need to work on my regex
Cristián Berríos
@crisberrios
Feb 24 2015 17:09
"thisIsSpinalTap".replace(/([a-z]{1})([A-Z]{1})/g, function(match) { 
    return match[0].toLowerCase() + '-' + match[1].toLowerCase();
});
Cristián Berríos
@crisberrios
Feb 24 2015 17:16
I think the 1st form should work, but I have also to get better at using capture groups
Ashley Drake
@aldraco
Feb 24 2015 17:37
thanks @crisberrios we got it figured out now!
(@germainep and i were pairing on it)
Cristián Berríos
@crisberrios
Feb 24 2015 17:37
good
Evan Davis
@davisec52
Feb 24 2015 17:40
@crisberrios I am really lost on the Bonfire "Where Art Thou" challenge. The code sample below does not do anything, though it is an attempt to compare the properties of source (an object) with collection (an array of objects). What is it that the problem wants us to understand in order to solve it?

function where(collection, source) {
  var arr = [];
  for(var i = 0; i < collection.length; i++) {
      var colKeys = Object.keys(collection[i]);
      for(var j in source) {
          var sourceKeys = Object.keys(source); 
              if(sourceKeys === colKeys) {
                  arr = colKeys;

          }
      }

  }

  return arr;
}
where([{ first: 'Romeo', last: 'Montague' }, { first: 'Mercutio', last: null }, { first: 'Tybalt', last: 'Capulet' }], { last: 'Capulet' });
Cristián Berríos
@crisberrios
Feb 24 2015 17:40
well, 1st rule: never define a variable inside a loop
Evan Davis
@davisec52
Feb 24 2015 17:41
I guess I don't understand. How can you set up a for-loop without defining the variable.
sorry,
right, var sourceKeys
but that is not actually the main problem, though, is it?
Cristián Berríos
@crisberrios
Feb 24 2015 17:42
function where(collection, source) { 
var arr = [];
var i = 0;
var colKeys;
var sourceKeys;
  for(i = 0; i < collection.length; i++) {
      colKeys = Object.keys(collection[i]);
      for(var j in source) {
          sourceKeys = Object.keys(source); 
              if(sourceKeys === colKeys) {
                  arr = colKeys;
          }
      }

  }

  return arr;
}
you can combine declarations in 1 line, but 1st is declaring variables
then...
ok... you store the object keys in colkeys
that's ok
and then the 2nd loop is not right
Evan Davis
@davisec52
Feb 24 2015 17:44
ok
Cristián Berríos
@crisberrios
Feb 24 2015 17:44
since you again are passing only values to the j variable
function where(collection, source) { 
var arr = [];
var i = 0;
var colKeys;
var  sourceKeys = Object.keys(source); 
  for(i = 0; i < collection.length; i++) {
      colKeys = Object.keys(collection[i]);
      for(var key in sourceKeys) {
               //still have to compare
          }
      }

  }

  return arr;
}
Evan Davis
@davisec52
Feb 24 2015 17:45
I tried making colKeys = source, but that did not work.
but I thought Object.keys(source) would give the key (last).
Cristián Berríos
@crisberrios
Feb 24 2015 17:46
see if that makes more sense
Ashley Drake
@aldraco
Feb 24 2015 17:47
general question about github contributions
what is the most acceptable way to get involved when you're new to the project - go ahead and fix something, and make a pull request? or ask first? I'm sure there are times when someone would rather assign a problem to someone who knows more about it.
Cristián Berríos
@crisberrios
Feb 24 2015 17:48
check the issues
Evan Davis
@davisec52
Feb 24 2015 17:48
@crisberrios I'll need a moment . . .
Ashley Drake
@aldraco
Feb 24 2015 17:48
not sure about how the personal dynamics tend to work
Cristián Berríos
@crisberrios
Feb 24 2015 17:48
and try to get an issue you can fix assigned to you
Ashley Drake
@aldraco
Feb 24 2015 17:49
@crisberrios is just saying "I'm willing to help" what you mean?
Cristián Berríos
@crisberrios
Feb 24 2015 17:49
or, if there is a bug, submit an issue and better yet submit a possible solution using a pull request
Ashley Drake
@aldraco
Feb 24 2015 17:50
ok
thanks
:)
Evan Davis
@davisec52
Feb 24 2015 17:54
@crisberrios What are the training points this problem is trying to get us to practice? I am just lost. Perhaps if I were sure of the training points, I could study those and arrive at a solution.
Germaine Pannell
@germainep
Feb 24 2015 17:56
@crisberrios thanks very much
Cristián Berríos
@crisberrios
Feb 24 2015 17:58
@davisec52 I think core key elements are
  • pointing to an element of an array
  • pointing to an object property name
  • pointing to an object property value.
  • filter values of a collection
also, more specific:
  • learn to use the information returned by Object.keys(obj)
  • learn to get a value of a specific key
  • learn to get the value of the same key in a different object
Evan Davis
@davisec52
Feb 24 2015 18:01
@crisberrios Great. For the second and third specific points, could you recommend links to material that would help me understand how to accomplish those objectives?
Cristián Berríos
@crisberrios
Feb 24 2015 18:03
any general JS book will do. Check the Objects chapter in Eloquent JS, Professional JS for web developers (non-free), JS: The Good parts (non-free) or JS succintly.
for a better way of dealing with loops you could also check the arrays chapter
in special the .filter method for this problem
Evan Davis
@davisec52
Feb 24 2015 18:06
So, basically, to understand this problem requires reading 1500 pages of javascript textbooks
I know Object,keys and filters are covered on the MDN pages.
Gregory Scheerlinck
@greg-js
Feb 24 2015 18:08
Nah, those books contain waaaaaaaay more than just those topics :)
Evan Davis
@davisec52
Feb 24 2015 18:08
Right, I know it was not cris's intention to recommend reading a library
Cristián Berríos
@crisberrios
Feb 24 2015 18:09
you can also try mdn.io :P
and I said any of those
Gregory Scheerlinck
@greg-js
Feb 24 2015 18:09
If you read one of them (don't start with The Good Parts though, keep it for your second or third book on JS) you'll learn loads, then experiment and solve problems (I recommend supplementing the bonfires with lots of codewars) until it becomes second nature
Evan Davis
@davisec52
Feb 24 2015 18:10
I have tried Eloquent JS and found it too difficult.
Gregory Scheerlinck
@greg-js
Feb 24 2015 18:10
and @crisberrios - I know it shouldn't bother me but arghh it hurts me so to not see a space after for or if!! ;-)
Evan Davis
@davisec52
Feb 24 2015 18:11
This message was deleted
Gregory Scheerlinck
@greg-js
Feb 24 2015 18:13
I haven't read Eloquent JS myself but I've seen it come very highly recommended. I read Professional JS for Web Developers as my intro book, it's not ideal but I thought it was great, though sometimes maybe a bit too exhaustive and now maybe a little outdated
Evan Davis
@davisec52
Feb 24 2015 18:14
Does Professional JS require a background in programming?
Gregory Scheerlinck
@greg-js
Feb 24 2015 18:16
The author assumes you have some familiarity with programming, but I personally thought it worked as an intro book. sometimes you have to do some googling but it's great if you prefer a book that goes into more depth than you need. it's NOT a very practical book though, there are no exercises like in Eloquent JS unfortunately
Angelica Ferdinand
@ESOComputing
Feb 24 2015 18:16
I don't feel Eloquent JavaScript assumes a background in programming... I'm still in the early chapters, but I feel it's fairly clear.
Sometimes I find myself re-reading stuff, but that's OK.
The book actually tells you to do that.
Cristián Berríos
@crisberrios
Feb 24 2015 18:16
@wdgreg I have to revise the coding styles... I've always used for(... since my days in C
Angelica Ferdinand
@ESOComputing
Feb 24 2015 18:17
@crisberrios JavaScript has a completely different approach from other more "traditional" languages... or so I've been hearing.
Gregory Scheerlinck
@greg-js
Feb 24 2015 18:17
@crisberrios it doesn't really matter, it's just that I've read and seen too much of Crockford not to have the lack of spaces affect me ;-)
Cristián Berríos
@crisberrios
Feb 24 2015 18:17
I still have to go through some of his books
Gregory Scheerlinck
@greg-js
Feb 24 2015 18:19
what really grinds my gears is that comma-first variable declaration style. grrrrrr
or people who think it's fun to omit semicolons when the engine puts them automatically (damn you, raganwald)
Cristián Berríos
@crisberrios
Feb 24 2015 18:19
like var a = 0
,b = something?
Gregory Scheerlinck
@greg-js
Feb 24 2015 18:20
yeah, that's comma first. I just really don't like it, it looks so alien to me
even though it's perfectly valid
Angelica Ferdinand
@ESOComputing
Feb 24 2015 18:21
is that comma first declaration JS?
That looks really weird.
Cristián Berríos
@crisberrios
Feb 24 2015 18:21
sure, same as var a,b,c
but you declare var a
,b
,c
Angelica Ferdinand
@ESOComputing
Feb 24 2015 18:21
To me that seems like an easy way to get lost... I dunno.
I like keeping things grouped that are grouped. \
Cristián Berríos
@crisberrios
Feb 24 2015 18:22
well, brb
Gregory Scheerlinck
@greg-js
Feb 24 2015 18:23
yeah, I read an article the other day and IIRC it said about 25% of JS programmers use comma-first
I really like the AirBnB style guide
Angelica Ferdinand
@ESOComputing
Feb 24 2015 18:51
that looks interesting...
Cristián Berríos
@crisberrios
Feb 24 2015 18:55
https://github.com/airbnb/javascript/issues/233#issuecomment-72163361 I think I'll add the missing rules to my .jshintrc
Ashley Drake
@aldraco
Feb 24 2015 19:38
ok ... css question. this is driving me nuts. ```
<link rel="stylesheet" href="/public/stylesheets/bootstrap-flatly.css" type="text/css">
should go to the root folder of my project and find the public folder and go down from there, right?
i'm using ejs if that makes any difference
Angelica Ferdinand
@ESOComputing
Feb 24 2015 19:42
If public is the top-level directory that should work.
The preceding / means the root level.
Nathan
@terakilobyte
Feb 24 2015 19:43
@aldraco is it an express app?
Ashley Drake
@aldraco
Feb 24 2015 19:43
it is an express app
Nathan
@terakilobyte
Feb 24 2015 19:43
if your static directory is set to public
then don’t include /public
Ashley Drake
@aldraco
Feb 24 2015 19:44
ahh... because all static assets are being routed through there anyway?
Nathan
@terakilobyte
Feb 24 2015 19:44
^
so try '/stylesheets/bootstrap-flatlly.css'
Ashley Drake
@aldraco
Feb 24 2015 19:45
worked perfectly.
Nathan
@terakilobyte
Feb 24 2015 19:45
:)
Ashley Drake
@aldraco
Feb 24 2015 19:45
thanks!
Nathan
@terakilobyte
Feb 24 2015 19:45
enjoy!
Germaine Pannell
@germainep
Feb 24 2015 20:35
has anyone attempted a recursive function in bonfires
Cristián Berríos
@crisberrios
Feb 24 2015 20:36
I think I've done some recursion
but don't rely on it too much for deep recursion
Germaine Pannell
@germainep
Feb 24 2015 20:37
I keep crashing it trying to do a recursive fibonacci number just wondering if it was just me
Cristián Berríos
@crisberrios
Feb 24 2015 20:38
maybe the function itself is crashing
Germaine Pannell
@germainep
Feb 24 2015 20:40
i think so i'll just d it another way
Nathan
@terakilobyte
Feb 24 2015 20:44
recursion is not the best solution to fibonaccis imo
as the typical fibonacci recursive method forces recalculation of the right side
that and js doesn’t properly support tail recursion yet
Cristián Berríos
@crisberrios
Feb 24 2015 20:45
It can be solved using the memoization approach
in the case you want to calculate the nth fibonacci number
Nathan
@terakilobyte
Feb 24 2015 20:46
^
a while loop that is workign off of known values in an array, and pushign new values to the end of that array to sustain itself is incredibly efficient
and while bonfire won’t actually cache (it doesn’t need to, js is stupid fast for the sizes of inputs we’re giving), in the real world it would cache to a data store somewhere
Cristián Berríos
@crisberrios
Feb 24 2015 20:53

var fibonacci = function () {
var memo = [0, 1];
var fib = function (n) {
var result = memo[n];
if (typeof result !== 'number') {
Memoization | 45
result = fib(n - 1) + fib(n - 2);
memo[n] = result;
}
return result;
};
return fib;
}();
stole this from js: The Good Parts
Nathan
@terakilobyte
Feb 24 2015 20:55
I still don’t like that method as it has a recursive call
Cristián Berríos
@crisberrios
Feb 24 2015 20:55
and this general version
var memoizer = function (memo, fundamental) {
var shell = function (n) {
var result = memo[n];
if (typeof result !== 'number') {
result = fundamental(shell, n);
memo[n] = result;
}
return result;
};
return shell;
};

//ejemplo
var fibonacci = memoizer([0, 1], function (shell, n) {
return shell(n - 1) + shell(n - 2);
});
//ejemplo 2
var factorial = memoizer([1, 1], function (shell, n) {
return n * shell(n - 1);
});
Oliver
@Dadsaster
Feb 24 2015 20:57
Convert the characters "&", "<", ">", '"', and "'", in a string to their corresponding HTML entities. - BONFIRE - What should 'Dolce & Gabbana' return?
'Dolce &#38; Gabbana'?
Nathan
@terakilobyte
Feb 24 2015 20:58
// fibs is our data store for now
var fibs = [0,1,1];
function findNthFib(n) {
  while (fibs.length < n) {
    fibs.push(fibs[fibs.length -1 + fibs[fibs.length - 2]);
  }
  return fibs[n];
}
Cristián Berríos
@crisberrios
Feb 24 2015 20:58
use entity names
yeah, much faster and also doesn't use recursion
Nathan
@terakilobyte
Feb 24 2015 21:01
not sure where I saw it, but it’s my goto for really any series
fibs can be cached, so any repeat calls will fail the condition in the while loop if that number already exists
it can be modified to calculate values as well
Oliver
@Dadsaster
Feb 24 2015 21:02
@crisberrios Thanks - I was using .charCodeAt + entity numbers
Nathan
@terakilobyte
Feb 24 2015 21:02
I made the bonfire fibonacci challenge off a modification to that
Oliver
@Dadsaster
Feb 24 2015 21:25
I'm curious how others solved this bonfire - Convert the characters "&", "<", ">", '"', and "'", in a string to their corresponding HTML entities.
function convert(str) {

    // replace ', ", <, >, & with equivalent HTML values
    var newString = "";

    // table of values for HTML replacement
    var escapeHtml = {
        34 : '&quot;',
        38 : '&amp;', 
        39 : '&apos;', 
        60 : '&lt;', 
        62 : '&gt;'};

    newString = str.replace(/[<>'"\&]/g, function(c) {

        //send back the value from escapeHtml table
        return escapeHtml[c.charCodeAt(0)];

    });
    return newString;
}

convert('Dolce & Gabbana');
I would be nice if after you solved a bonfire you could look at other people's solutions like at code wars
Nathan
@terakilobyte
Feb 24 2015 21:27
you will be able to shortly
Ashley Drake
@aldraco
Feb 24 2015 21:31
@Dadsaster i did that one similarly
just without the char codes, nice touch
Oliver
@Dadsaster
Feb 24 2015 21:32
Thanks
Nathan
@terakilobyte
Feb 24 2015 21:39
I like it, though you can clean it up by not using the character codes and instead using the symbol itself
Vince
@Vince33
Feb 24 2015 21:43
@terakilobyte Are our solutions being stored now?
Nathan
@terakilobyte
Feb 24 2015 21:43
yes
Vince
@Vince33
Feb 24 2015 21:44
cool so when you add functionality I will be able to see stuff I have already done
@terakilobyte I know you guys were mentioning it earir but didn't get a chance to see the thread of conv.
Nathan
@terakilobyte
Feb 24 2015 21:48
@Dadsaster
function convert(str) {

    // replace ', ", <, >, & with equivalent HTML values
    var newString = "";

    // table of values for HTML replacement


    newString = str.replace(/[<>'"\&]/g, function(c) {

        //send back the value from escapeHtml table
        return escape(c);

    });
    return newString;
}

function escape(html) {
  var escapeHtml = {
        '\"' : function() {
                return '&quot;';
               },
        '&'  : function() {
                return '&amp;';
               }, 
        '\'' : function() {
                return '&apos;';
               }, 
        '<'  : function() {
                return '&lt;';
               }, 
        '>'  : function() {
                return '&gt;';
               }
    };
  return escapeHtml[html]() || html;
}

convert('Dolce & Gabbana’);
Germaine Pannell
@germainep
Feb 24 2015 21:54
hi, guys I'm working on the Sum All Odd fionacci numbers bonfire and every time I create the fibonacci function it crashes the browser.
Nathan
@terakilobyte
Feb 24 2015 21:56
the sandbox only provides so much protection
turning it up all the way would keep us from having a lot of nice things
post wha tyou have rq
I’ll see if I can point out any obvious errors
Germaine Pannell
@germainep
Feb 24 2015 21:57
function sumFibs(num) {
  var fib = [];
  for(var i=0; i<num; i++){
    fib.push((i<2) ? i : fib[i-1] + fib[i-2]);
  }
  return fib;
}
this is what I have so far and I want to make sure I get the correct sequence then I can filter the odd numbers and get the correct sum
Nathan
@terakilobyte
Feb 24 2015 22:01
I have to run to work so someone else will have to take over, but iterating sequentially is most definitely not the answer
also, if i = 5
well, the logic in the for loop is ok, but you’re taking forever to get there
one of the arguments we pass in is pretty big
Germaine Pannell
@germainep
Feb 24 2015 22:10
@terakilobyte I dont know any other way to get a Fibonacci sequence i guess back to the grind stone
Nathan
@terakilobyte
Feb 24 2015 22:23
if you scroll up I posted a method earlier
Ashley Drake
@aldraco
Feb 24 2015 22:24
sum of all primes is a doozy
Nathan
@terakilobyte
Feb 24 2015 22:25
I’ve been working on higher level bonfires in my spare time
I’ll have another 20 or so to add to level 3 and 4
Ashley Drake
@aldraco
Feb 24 2015 22:26
the fire keeps growing ... :fire_engine:
Vince
@Vince33
Feb 24 2015 22:26
@terakilobyte killing me here
Nathan
@terakilobyte
Feb 24 2015 22:26
you all have a good day, off to work
Ashley Drake
@aldraco
Feb 24 2015 22:28
take care
Vince
@Vince33
Feb 24 2015 22:52
@germainep any luck?
Germaine Pannell
@germainep
Feb 24 2015 22:56
@Vince33 not yet I'm taking a break to clear my mind
Vince
@Vince33
Feb 24 2015 22:56
@germainep good Idea I am on the same one
Germaine Pannell
@germainep
Feb 24 2015 22:58
@Vince33 give me about 20 mins and we can pair if you want
Vince
@Vince33
Feb 24 2015 22:58
send me a pm I should be around
I am running out real fast but should be back
Germaine Pannell
@germainep
Feb 24 2015 22:59
ok