These are chat archives for FreeCodeCamp/HelpJavaScript

6th
Nov 2017
AbrisM
@AbrisM
Nov 06 2017 00:36
Thanks @mstellaluna
CamperBot
@camperbot
Nov 06 2017 00:36
abrism sends brownie points to @mstellaluna :sparkles: :thumbsup: :sparkles:
:star2: 1032 | @mstellaluna |http://www.freecodecamp.com/mstellaluna
silver537
@silver537
Nov 06 2017 01:13
Honestly discord is a lot better than gitter.
Bryan Reese
@breese8009
Nov 06 2017 02:25
using mongoose, its console.logging it right but not actually saving to db saving

function update(req, res) {

  db.Location.findById(req.params.locations_id, function(err, location){
    if(err){
      console.log(`this didnt work ${location}`)
    } 

    location.city = req.body.city;
    location.near_address = req.body.description;
    location.photo = req.body.photo;

    location.save(function(err, savedLocation){
      console.log(savedLocation)
      if(err) console.log(err+" this didnt work")
        else
          location.save()
          console.log(location, "has been seen")

    })

  })
}
Moisés Man
@moigithub
Nov 06 2017 03:26
if err u should stop the execution
and why save() 2 times ?
Long Nguyen
@longnt80
Nov 06 2017 05:35
how do I create a function like this:
[1,2,3,4].sum // result is 10
?
Ashwin
@ashwins93
Nov 06 2017 05:53
without parens it is just a property
you could do it with a getter
@longnt80
Object.defineProperty(
  Array.prototype, 
  'sum', 
  { 
    get: function() { 
      return this.reduce((acc, next) => acc + next); 
    } 
  }
);
[1, 2, 3, 4].sum; // 10
Markus Kiili
@Masd925
Nov 06 2017 06:25
@ashwins93 Maybe you should add an initial accumulator so an empty array won't throw an error?
Ashwin
@ashwins93
Nov 06 2017 06:28
@Masd925 something like this?
Object.defineProperty(
  Array.prototype, 
  'sum', 
  { 
    get: function() { 
      return this.reduce((acc = 0, next) => acc + next); 
    } 
  }
);
Markus Kiili
@Masd925
Nov 06 2017 06:29
@ashwins93 return this.reduce((acc, next) => acc + next, 0);
Ashwin
@ashwins93
Nov 06 2017 06:56
Oh like that thanks !
Long Nguyen
@longnt80
Nov 06 2017 07:54
@ashwins93 @Masd925 thank you!
CamperBot
@camperbot
Nov 06 2017 07:54
longnt80 sends brownie points to @ashwins93 and @masd925 :sparkles: :thumbsup: :sparkles:
:cookie: 414 | @ashwins93 |http://www.freecodecamp.com/ashwins93
:star2: 4562 | @masd925 |http://www.freecodecamp.com/masd925
I dont't know how do it?
Jia Li
@f43310
Nov 06 2017 08:18
Anybody can help me?
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 08:56
@f43310 what do you need help with?
Why is this answer wrong?
Markus Kiili
@Masd925
Nov 06 2017 10:59
@inputherf Could you copy-paste the code here?
inputherf
@inputherf
Nov 06 2017 11:26

// 举例
var ourStr = "I come first. " + "I come second.";

// 请只修改这条注释以下的代码

var myStr = 'This is the start.' + 'This is the end.';

Markus Kiili
@Masd925
Nov 06 2017 11:31
@inputherf There needs to be a space after the first full stop.
hillsie
@Hillsie
Nov 06 2017 11:46
Hi There, doing some revision again. Q:
why does [2,4,6] == [2,4,6] return false ?
but when comparing [2,4,6] == ‘2,4,6’ the array is coerced to a string and it returns true
Markus Kiili
@Masd925
Nov 06 2017 11:49
@Hillsie When you compare objects with == or ===, it compares object identity (~memory location).
Not content.
hillsie
@Hillsie
Nov 06 2017 11:51
@Masd925 Thanks. So conversely, when you compare a string and an object, it coerces the object to be a string?
CamperBot
@camperbot
Nov 06 2017 11:51
hillsie sends brownie points to @masd925 :sparkles: :thumbsup: :sparkles:
:star2: 4563 | @masd925 |http://www.freecodecamp.com/masd925
Markus Kiili
@Masd925
Nov 06 2017 11:51
@Hillsie It is better to avoid using automatic coercion, especially ==. The abstract equality == involves coercion to same type.
Blauelf
@Blauelf
Nov 06 2017 11:54

@Hillsie The == "abstract equality" operator is defined in the standard: https://www.ecma-international.org/ecma-262/7.0/index.html#sec-abstract-equality-comparison

8. If Type(x) is either String, Number, or Symbol and Type(y) is Object, return the result of the comparison x == ToPrimitive(y).
9. If Type(x) is Object and Type(y) is either String, Number, or Symbol, return the result of the comparison ToPrimitive(x) == y.

In this case, it would convert the array to a string.

hillsie
@Hillsie
Nov 06 2017 11:55
@Masd925 @Blauelf Thanks guys.
CamperBot
@camperbot
Nov 06 2017 11:55
hillsie sends brownie points to @masd925 and @blauelf :sparkles: :thumbsup: :sparkles:
api offline
:star2: 4645 | @blauelf |http://www.freecodecamp.com/blauelf
Markus Kiili
@Masd925
Nov 06 2017 11:57
@Hillsie And in this case ToPrimitive would use .toString() and in effect coerce it to a string before comparing.
Blauelf
@Blauelf
Nov 06 2017 11:59
Which makes me wonder if I could cause odd behaviour if I implemented both toString and valueOf in an inconsistent way. There are many different coercion rules, there must be space for weirdness :D
hillsie
@Hillsie
Nov 06 2017 11:59
:smile: [2,4,6].ToPrimitive == [2,4,6].ToPrimitive; true
Blauelf
@Blauelf
Nov 06 2017 11:59
@Hillsie undefined == undefined?
All those global variables like undefined, Infinity, or NaN...
function blubb() {
  var undefined = 1;
  if (undefined) {
    console.log("undefined is truthy!");
  }
}
blubb();
hillsie
@Hillsie
Nov 06 2017 12:03
weird. So you can assign a value to undefined global variable.
Blauelf
@Blauelf
Nov 06 2017 12:04
Global variable undefined has been made read-only. But you can create local variables that shadow the global one. Some people use void 0 instead of undefined for that reason.
Markus Kiili
@Masd925
Nov 06 2017 12:05
@Hillsie ToPrimitive is an internal function that you cannot use yourself.
hillsie
@Hillsie
Nov 06 2017 12:09
Thanks, It seems to works in the node Repl, but I’ll avoid it.
May I change the subject and ask help with understanding pre ES6 variable hoisting and what ES6 does about variable hoisting. I think this is block scope vs. function scope thing. I’d like to be able to explain it the benefits of let and const on variable hoisting.
Blauelf
@Blauelf
Nov 06 2017 12:10

@Hillsie The standard uses all kinds of functions and symbols that are not accessible to JavaScript code, but describe how the language is meant to work. You'll also find things like [[prototype]] which doesn't exist in that form (there are Object.setPrototypeOf and Object.getPrototypeOf to interface that property).

So... [1,2,3].ToPrimitive == [23,42].ToPrimitive :) (as I said, you're comparing undefineds)

hillsie
@Hillsie
Nov 06 2017 12:11
Yes, that has no real use.
Ok, got it.
Thanks guys. The explaination to my original post is that when doing the comparison, you’re comparing object memory locations.
hillsie
@Hillsie
Nov 06 2017 12:26

variable hoisting:

function b() {
    console.log(myVar);
}
function a() {
    var myVar = 2;
    b();
}
var myVar = 1;
a();

This baffels me a bit.

inputherf
@inputherf
Nov 06 2017 12:28
@Masd925 thanks~~
CamperBot
@camperbot
Nov 06 2017 12:28
inputherf sends brownie points to @masd925 :sparkles: :thumbsup: :sparkles:
:star2: 4564 | @masd925 |http://www.freecodecamp.com/masd925
hillsie
@Hillsie
Nov 06 2017 12:30
Thanks again @Blauelf and @Masd925
CamperBot
@camperbot
Nov 06 2017 12:30
hillsie sends brownie points to @blauelf and @masd925 :sparkles: :thumbsup: :sparkles:
api offline
api offline
Markus Kiili
@Masd925
Nov 06 2017 12:31
@Hillsie When function b logs myVar, it finds the variable declared on the global scope.
JS has lexical scoping, so scopes are defined by how functions sit in the code in respect to each other.
Razvan Jackson
@RazvanJackson
Nov 06 2017 12:33
Who knows C++. I have some problems
Rohit Yadav
@rohyadav
Nov 06 2017 12:54
@RazvanJackson i shared with you that question
i was asking earliear
Traek Wells
@TraekWells
Nov 06 2017 13:02
Can anyone help me refactor this code? It works just fine but just wondering if there was a better way to write this and maybe save some lines.
// Listen for click event
getWeatherButton.addEventListener("click", getWeather);

function getWeather() {
  // Variables
  var getWeatherButton = document.querySelector("#getWeatherButton");
  var hero = document.querySelector(".hero");
  var resultsSection = document.querySelector(".resultsSection");
  var icon = document.querySelector("#icon");
  var description = document.querySelector("#description");
  var temperature = document.querySelector("#temperature");
  var toCelsius = document.querySelector("#celsius");
  var toFahrenheit = document.querySelector("#fahrenheit");
  var city = document.querySelector("#city");
  var celsius;
  var fahrenheit;

  hero.classList.add("quarterHeight");

  if(!navigator.geolocation) {
    results.innerHTML = "<p>Geolocation is disabled on your browser. You're on your own.</p>";
    return;
  }
  function success(position) {
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;
    var xhr = new XMLHttpRequest();

    xhr.open('GET', 'https://fcc-weather-api.glitch.me/api/current?lat=' + latitude + '&lon=' + longitude);
    xhr.onload = function() {
      if(this.status == 200) {
        resultsSection.style.display = "block";
        // Get and parse the data
        var data = JSON.parse(this.responseText);
        city.textContent = data.name;
        celsius = (data.main.temp).toFixed(0) + "&#176; C";
        fahrenheit = ((data.main.temp) * 9/5 + 32).toFixed(0) + "&#176; F";
        description.textContent = data.weather[0].description;
        icon.src = data.weather[0].icon;
        temperature.innerHTML = fahrenheit;
      }
    }
    xhr.send();
  }
  function error() {
    results.innerHTML = "Unable to get your location. Are you on Earth?";
  }
  navigator.geolocation.getCurrentPosition(success, error); 

  //Listen for click events on the celsius and fahrenheit buttons
  function convertToCelsius() {
    temperature.innerHTML = celsius;
  }
  function convertToFahrenheit() {
    temperature.innerHTML = fahrenheit;
  }
  toCelsius.addEventListener("click", convertToCelsius);
  toFahrenheit.addEventListener("click", convertToFahrenheit);
}
Blauelf
@Blauelf
Nov 06 2017 13:27
@TraekWells You add event listeners each time you get the weather. If you can do that multiple times, you add another listener without removing the other. You'll probably see only the result of the last installed, but those pile up.
Traek Wells
@TraekWells
Nov 06 2017 13:33
@Blauelf So I should remove the previous eventlisten if someone clicks the get weather button for a second time?
Blauelf
@Blauelf
Nov 06 2017 13:42

I'd do it differently. Reuse variables. Maybe use an IIFE, like

// Listen for click event
getWeatherButton.addEventListener("click", getWeather);

var getWeather = function() {
  // Variables
  var getWeatherButton = document.querySelector("#getWeatherButton");
  var hero = document.querySelector(".hero");
  var resultsSection = document.querySelector(".resultsSection");
  var icon = document.querySelector("#icon");
  var description = document.querySelector("#description");
  var temperature = document.querySelector("#temperature");
  var toCelsius = document.querySelector("#celsius");
  var toFahrenheit = document.querySelector("#fahrenheit");
  var city = document.querySelector("#city");
  var celsius;
  var fahrenheit;

  function success(position) {
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;
    var xhr = new XMLHttpRequest();

    xhr.open('GET', 'https://fcc-weather-api.glitch.me/api/current?lat=' + latitude + '&lon=' + longitude);
    xhr.onload = function() {
      if (this.status == 200) {
        resultsSection.style.display = "block";
        // Get and parse the data
        var data = JSON.parse(this.responseText);
        city.textContent = data.name;
        celsius = (data.main.temp).toFixed(0) + "&#176; C";
        fahrenheit = ((data.main.temp) * 9 / 5 + 32).toFixed(0) + "&#176; F";
        description.textContent = data.weather[0].description;
        icon.src = data.weather[0].icon;
        temperature.innerHTML = fahrenheit;
      }
    }
    xhr.send();
  }

  function error() {
    results.innerHTML = "Unable to get your location. Are you on Earth?";
  }

  //Listen for click events on the celsius and fahrenheit buttons
  function convertToCelsius() {
    if (celsius !== undefined) {
      temperature.innerHTML = celsius;
    }
  }

  function convertToFahrenheit() {
    if (fahrenheit !== undefined) {
      temperature.innerHTML = fahrenheit;
    }
  }
  toCelsius.addEventListener("click", convertToCelsius);
  toFahrenheit.addEventListener("click", convertToFahrenheit);

  return function getWeather() {
    hero.classList.add("quarterHeight");

    if (!navigator.geolocation) {
      results.innerHTML = "<p>Geolocation is disabled on your browser. You're on your own.</p>";
      return;
    }

    navigator.geolocation.getCurrentPosition(success, error);
  }
}();

The getWeather in return function getWeather is just for nicer debug output, as it sets the name for the function expression.

The outer function mostly provides scope of persistent "private" variables for the returned function.
Traek Wells
@TraekWells
Nov 06 2017 13:55
@Blauelf Awesome man. Is there a performance benefit to doing it this way?
I would say the way you have it is a little bit easier to read
FlashHero
@FlashHero
Nov 06 2017 13:57
Why wouldn't this recursive function work ? help please
function get(str,letter,track){
    var current=str[track];
    console.log(current);
    if(current==letter){
      return letter;
    }else if(track==str.length){
    return 'it doesn\'t exist';
  }else{
    get(str,letter,track+=1);
  }
}
get('hello','e',0);
korzo
@korzo
Nov 06 2017 14:00
@FlashHero because you return nothing here get(str,letter,track+=1);
Blauelf
@Blauelf
Nov 06 2017 14:08
@TraekWells Not sure there's much of a benefit in this case, but at least you're not adding additional listeners each time. In my opinion, dynamically adding or removing listeners is the source for many memory leaks. Garbage collection can only collect what's no longer referenced from any root.
FlashHero
@FlashHero
Nov 06 2017 14:09
@korzo can you elaborate please
korzo
@korzo
Nov 06 2017 14:10
@FlashHero
function get(str,letter,track){
    var current=str[track];
    console.log(current);
    if(current==letter){
      return letter;
    }else if(track==str.length){
    return 'it doesn\'t exist';
  }else{
    return get(str,letter,track+=1);
  }
}
get('hello','e',0);
@FlashHero when calling function recursively, you have to return the call, otherwise the returned value will never reach the top most call
FlashHero
@FlashHero
Nov 06 2017 14:12
@korzo oh I get it now thanks
CamperBot
@camperbot
Nov 06 2017 14:12
flashhero sends brownie points to @korzo :sparkles: :thumbsup: :sparkles:
:cookie: 749 | @korzo |http://www.freecodecamp.com/korzo
FlashHero
@FlashHero
Nov 06 2017 14:16
@korzo you on
korzo
@korzo
Nov 06 2017 14:22
@FlashHero glad to help
FlashHero
@FlashHero
Nov 06 2017 14:22
I visualized in python tutor, Can you clear out my one doubt of why track which is 0 turns itself into 1 when the function is called in a different frame and then the track is supposed to be 1, the first call variable track which is supposed to be 0 turn itself into 1, that's a bit confusing to me
Blauelf
@Blauelf
Nov 06 2017 14:27
@FlashHero += is add-assign. An assignment also has the value it assigns.
Use + instead of += if you don't want that to happen.
FlashHero
@FlashHero
Nov 06 2017 14:30
@Blauelf if I do ++ it doesn't even run
Blauelf
@Blauelf
Nov 06 2017 14:31
You mean return get(str, letter, ++track);?
FlashHero
@FlashHero
Nov 06 2017 14:31
@Blauelf no track++
Blauelf
@Blauelf
Nov 06 2017 14:32
track++ evaluates to the pre-increment value (so calls the function with 0 again), ++track evaluates to the post-increment value.
Both increment, just vary in the value of the expression.
FlashHero
@FlashHero
Nov 06 2017 14:34
@Blauelf so how do i prevent it starting from index 1
Bryan Reese
@breese8009
Nov 06 2017 14:35
soooo using mongoose and trying to do an update function... it says its saved in the terminal and shows the right object but then doesnt update in db. Which is really weird considering im seeing the console.log after the .save()
function update(req, res) {

  db.Location.findById(req.params.locations_id, function(err, location){
    if(err){
      console.log(`this didnt work ${location}`)
    } 

    location.city = req.body.city;
    location.near_address = req.body.description;
    location.photo = req.body.photo;

    location.save(function(err, savedLocation){
      console.log(savedLocation)
      if(err) console.log(err+" this didnt work")
        else
          location.save()
          console.log(location, "has been saved")

    })

  })
}
Blauelf
@Blauelf
Nov 06 2017 14:35
@FlashHero Prevent what? You mean return get(str, letter, track + 1);?
Rohit Yadav
@rohyadav
Nov 06 2017 14:35
<ol id="list">
        <li>item 1</li>
        <li>item 2</li>
        <li>item 3</li>
        <button>click</button>
    </ol>
$(document).ready(function(){
  $("#list").on("click","li", function(){
    console.log($(this).text());
    var ab = $(this).index("li")+1;
    console.log(ab);
  })
   });

$(document).ready(function(){
        $("button").click(function(){
            $("ol").append("<li>list item <a href='javascript:void(0);' class='remove'>×</a></li>") 
        });
        $(document).on("click", "a.remove" , function() {
            $(this).parent().remove();
        });
    });
right now when i add item then item will add like this
list item ×
Blauelf
@Blauelf
Nov 06 2017 14:36
@FlashHero Is get('hello', undefined, 0) meant to return undefined? If not, maybe re-order your conditions.
Rohit Yadav
@rohyadav
Nov 06 2017 14:36
what i want "list item 3 x"
like when use added it also add index of that list
how can i achieve this
FlashHero
@FlashHero
Nov 06 2017 14:37
@Blauelf No this is what i wanted because it was starting from index 1 (str, letter, track + 1 thanks
CamperBot
@camperbot
Nov 06 2017 14:37
flashhero sends brownie points to @blauelf :sparkles: :thumbsup: :sparkles:
:star2: 4646 | @blauelf |http://www.freecodecamp.com/blauelf
Rohit Yadav
@rohyadav
Nov 06 2017 14:37
??
anyone here?
help me out
or update this snippet
???
taltmann42
@taltmann42
Nov 06 2017 14:40
@rohyadav you can get the number of list items with $("#list li").length + 1 and use that as index
Rohit Yadav
@rohyadav
Nov 06 2017 14:42
@taltmann42 can you please update the snippet
i did not get it
taltmann42
@taltmann42
Nov 06 2017 14:43
@rohyadav you can store the number of items in a variable (e.g. listLength) in the button-click event. then use string concatenation in the jquery append function: $("ol").append("list item "+listLength+"...")
Diego Mayer
@Chrono79
Nov 06 2017 14:44
@taltmann42 Not so good, when you delete one item (not the last one), you'll get another item with the last index next time you create an item
taltmann42
@taltmann42
Nov 06 2017 14:46
@Chrono79 right, then keeping track of the number in a variable and update on insert/delete?
Rohit Yadav
@rohyadav
Nov 06 2017 14:46
got an error
Bryan Reese
@breese8009
Nov 06 2017 14:49
anyone here good w mongoose?
Rohit Yadav
@rohyadav
Nov 06 2017 14:50
got an error
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 14:51
@breese8009 define good :D
just post the issue and lest see
Bryan Reese
@breese8009
Nov 06 2017 14:52
@tiagocorreiaalmeida
@tiagocorreiaalmeida someone who has used it before lol
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 14:54
never used it that wait I normal use someof the options they offer something like findbyIdandupdate
Bryan Reese
@breese8009
Nov 06 2017 14:54
So i was looking for that
but their docs are garbage
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 14:54
and aswell create models for every collection I do use so the code looks a bit strange this wait but for step
Bryan Reese
@breese8009
Nov 06 2017 14:55
and had trouble finding it so i sorta did it my own way
lol
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 14:55
ahaah np
btw are you using vscode?
Bryan Reese
@breese8009
Nov 06 2017 14:55
nah
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 14:55
I know those methods exists thanks to vscode :D
I started typing findById
Bryan Reese
@breese8009
Nov 06 2017 14:55
vscode is cheating ;P
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 14:55
and then andupdate showed up
ahahah :D
Bryan Reese
@breese8009
Nov 06 2017 14:55
lol im jk
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 14:55
sec im not at home but will show you how to do it got somewhere in github
Bryan Reese
@breese8009
Nov 06 2017 14:56
@tiagocorreiaalmeida thank u sir
CamperBot
@camperbot
Nov 06 2017 14:56
breese8009 sends brownie points to @tiagocorreiaalmeida :sparkles: :thumbsup: :sparkles:
:cookie: 428 | @tiagocorreiaalmeida |http://www.freecodecamp.com/tiagocorreiaalmeida
taltmann42
@taltmann42
Nov 06 2017 14:56
@breese8009 not an expert but it looks like you're saving it twice? Also, since you're not using curly braces after your else, only the line after it is really inside the else. console.log(location, "has been saved") is printed everytime even if there's an error
Bryan Reese
@breese8009
Nov 06 2017 14:57
lol postman not happy with that
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 14:58
didnt had on in hand but foumnd in the docs
Model.findByIdAndUpdate(id, [update], [options], [callback])
Model.findByIdAndUpdate(id, { $set: { name: 'jason bourne' }}, options, callback)
one of the options can be new:true
to return the updatged doc
if you still confused just let me know you can show me the collection and I can write the thing no worries
plus mongoose accepts promises not sure if you are aware of it and prefer it
Bryan Reese
@breese8009
Nov 06 2017 15:01
@taltmann42 curly brackets matter why?
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 15:01
Available options

new: bool - if true, return the modified document rather than the original. defaults to false (changed in 4.0)
upsert: bool - creates the object if it doesn't exist. defaults to false.
fields: {Object|String} - Field selection. Equivalent to .select(fields).findOneAndUpdate()
sort: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
maxTimeMS: puts a time limit on the query - requires mongodb >= 2.6.0
runValidators: if true, runs update validators on this command. Update validators validate the update operation against the model's schema.
setDefaultsOnInsert: if this and upsert are true, mongoose will apply the defaults specified in the model's schema if a new document is created. This option only works on MongoDB >= 2.4 because it relies on MongoDB's $setOnInsert operator.
passRawResult: if true, passes the raw result from the MongoDB driver as the third callback parameter
context (string) if set to 'query' and runValidators is on, this will refer to the query in custom validator functions that update validation runs. Does nothing if runValidators is false.
runSettersOnQuery: bool - if true, run all setters defined on the associated model's schema for all fields defined in the query and the update.
that after selecting the id and setting the change fields
taltmann42
@taltmann42
Nov 06 2017 15:02
@breese8009 in JS, when you omit the curly brackets, only the next line is part of the if/else body
Margaret Greaney
@greaneym
Nov 06 2017 15:02
hello, I have a greater appreciation for the react rogue game and its complexity after reading more about it. I hope to use rot.js and think many people did for the codecamp. How did people store the js assets on codepen.io? Did you make a big blob.js and add that, or did you have a cdn source for the js. I mean not rot.min.js but all the other js files from the demo. Thanks for any help.
Bryan Reese
@breese8009
Nov 06 2017 15:03
@taltmann42 interesting because this lil function works
function foo(str){
  if(str === "me") console.log("me")
  else
  str = "you"
  console.log(str)
}
foo("m2")
Margaret Greaney
@greaneym
Nov 06 2017 15:03
Secondly I don't understand how to add an img tile , mapped to the ascii chars as outlined in the online manual. Does anyone know of a good example of how to do that?
Bryan Reese
@breese8009
Nov 06 2017 15:04
function foo(str){
  if(str === "me") console.log("me")
  else
  str = "you"
  console.log(str)
  console.log("got here")
  console.log("got here as well")
  return "got here"
}
foo("m2")
taltmann42
@taltmann42
Nov 06 2017 15:04
@breese8009 but foo("me") prints "me" twice
Bryan Reese
@breese8009
Nov 06 2017 15:05
ah ha
i see what ur sayin now
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 15:06
anyway got the update workign? ;)
Bryan Reese
@breese8009
Nov 06 2017 15:06
even w curly brackets unfortunately the function i had is doing the same
:(
i havent put the fixes on it yet
brb
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 15:06
plus dont you prefer to use promises as I said above?
Bryan Reese
@breese8009
Nov 06 2017 15:07
i do
Margaret Greaney
@greaneym
Nov 06 2017 15:07
My progress so far is adding a react "reset" or start new game button on this rot.js example.
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 15:07
you can set mongoose to promises ;)
const mongoose = require("mongoose");

mongoose.Promise = global.Promise;
mongoose.connect(process.env.DB_URI);

module.exports =mongoose;
just an example
after that all the mongoose connections/requests will work as promises
Bryan Reese
@breese8009
Nov 06 2017 15:08
I never seen it, I use promises for ajax calls an what not
Margaret Greaney
@greaneym
Nov 06 2017 15:09
I will check back later.
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 15:09
here is an example @breese8009
    Poll.find({author:req.user._id}).then((polls)=>{
        if(polls){
            res.render("my_polls",{polls});
        }
    }).catch((e)=>{
        console.log(e);
    })
Poll is a mongoose modell but look at it as your db.collection.find method
Bryan Reese
@breese8009
Nov 06 2017 15:09
oh shit
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 15:09
what?:D
Bryan Reese
@breese8009
Nov 06 2017 15:09
lol
i never seen this before
not in mongoose
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 15:10
I only know this things cause I took a good react course on udemy it was my start on backend
Bryan Reese
@breese8009
Nov 06 2017 15:10
they taught react and backend together
?
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 15:11
typo
nodejs ;)
mybad xd
Bryan Reese
@breese8009
Nov 06 2017 15:11
oh lol
i c
thats cool
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 15:11
btw there's a thing called robomongo if you never heard of it you might like it aswell :D
its like your sql server or your phpmyadmin but on mongo
Bryan Reese
@breese8009
Nov 06 2017 15:12
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 15:13
yeah
Bryan Reese
@breese8009
Nov 06 2017 15:13
@tiagocorreiaalmeida thanks man
CamperBot
@camperbot
Nov 06 2017 15:13
breese8009 sends brownie points to @tiagocorreiaalmeida :sparkles: :thumbsup: :sparkles:
api offline
Bryan Reese
@breese8009
Nov 06 2017 15:13
:{)
:)}
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 15:16
there's something else you need to be aware aswell
mongoose methods like .find dont trhow errors if it doesnt find any data that matches
the first step to take after find its check if the data exists with a normal if statement
cause as you may imagine it did what you asked for and returned what you asked for in thsi case nothing :D
Bryan Reese
@breese8009
Nov 06 2017 15:30
its still not saving :(
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 15:37
is there any error on the terminal?
can I see the code once again?
Chris
@ConnectCodes
Nov 06 2017 15:44
Hi guys, I am solving Smallest common mutiple challenge , can calculate least common mutiple of two small numbers (e.g 90, 80 for) but bigger numbers (318780, 201894) gives the wrong answer ! I used while loop to find the Greatest Common Divisor then use that to find LCM. https://codepen.io/Codeconnect/pen/yPJjmL?editors=0012
Christopher McCormack
@cmccormack
Nov 06 2017 16:02
@ConnectCodes have you tested this with other smaller values? I'm not sure I've seen this implementation before
Chris
@ConnectCodes
Nov 06 2017 16:02
@cmccormack Yes I tested it with 90 and 80 and it works
Christopher McCormack
@cmccormack
Nov 06 2017 16:03
@ConnectCodes other than those...
I don't believe that code will work for this exercise - it only works for your example because the first time you loop through the while loop you make b===1 which then ends the loop on the next check
Diego Mayer
@Chrono79
Nov 06 2017 16:04
@ConnectCodes 5 and 12 returns 12
Christopher McCormack
@cmccormack
Nov 06 2017 16:04
putting other values in gives improper results
@Chrono79 :wave:
Diego Mayer
@Chrono79
Nov 06 2017 16:04
Hi :wave:
Chris
@ConnectCodes
Nov 06 2017 16:06
@cmccormack actually, it works on some values and in others doesn't
Christopher McCormack
@cmccormack
Nov 06 2017 16:07
@ConnectCodes ok - you have to make it work for all values :)
I'm not sure why you're doing this - b = a % b;
Chris
@ConnectCodes
Nov 06 2017 16:08

I don't believe that code will work for this exercise - it only works for your example because the first time you loop through the while loop you make b===1 which then ends the loop on the next check

I tried to use Euclidean algorithm in while. So I cannot use this method?

I'm not sure why you're doing this - b = a % b; I am trying to use long devision to calculate Greatest common mutiple. Then use it in LCM = a*b/GCD
Christopher McCormack
@cmccormack
Nov 06 2017 16:11
Isn't that for GCD not LCM?
Jason Luboff
@JLuboff
Nov 06 2017 16:12
Morning :wave:
Christopher McCormack
@cmccormack
Nov 06 2017 16:12
What are you referencing?
@JLuboff heya
Chris
@ConnectCodes
Nov 06 2017 16:13
What are you referencing?
Using Reduction by the greatest common divisor section
https://en.wikipedia.org/wiki/Least_common_multiple#Reduction_by_the_greatest_common_divisor
While loop is Using Euclid's algorithm, long devision https://en.wikipedia.org/wiki/Greatest_common_divisor#Using_Euclid.27s_algorithm
Jason Luboff
@JLuboff
Nov 06 2017 16:13
@cmccormack How was Pig & Pickle?
Christopher McCormack
@cmccormack
Nov 06 2017 16:13
@JLuboff I love that place. No better place for meat and pickled things :D
Jason Luboff
@JLuboff
Nov 06 2017 16:14
Nice
Christopher McCormack
@cmccormack
Nov 06 2017 16:15
@ConnectCodes not sure you read the entire thing. I copied the pseudocode for LCM and it's working fine - you don't have a proper implementation of GCD
@JLuboff yeah if you're ever in the area let me know and we can go
Jason Luboff
@JLuboff
Nov 06 2017 16:16
@cmccormack For sure.But I mean.. is there any reason to go to Scottsdale?
Christopher McCormack
@cmccormack
Nov 06 2017 16:17
@JLuboff Some of the best restaurants in AZ are in Scottsdale, and it's very nice this time of year
Great night life too
Jason Luboff
@JLuboff
Nov 06 2017 16:17
I'm about to have a baby, ain't nobody going to have time for a night life :joy:
Christopher McCormack
@cmccormack
Nov 06 2017 16:17
haha just sayin
@ConnectCodes here's an implementation of GCD directly from the pseudocode that should work:
function gcd(a, b) {
  var temp;
  while (b !== 0) {
    temp = b;
    b = a % b;
    a = temp;
  }
  return a;
}
Jason Luboff
@JLuboff
Nov 06 2017 16:18
@cmccormack What else to do there besides eat?
Chris
@ConnectCodes
Nov 06 2017 16:18
@ConnectCodes So my while loop is wrong? I made it based on this from GCD wikipedia: A much more efficient method is the Euclidean algorithm, which uses a division algorithm such as long division in combination with the observation that the gcd of two numbers also divides their difference. To compute gcd(48,18), divide 48 by 18 to get a quotient of 2 and a remainder of 12. Then divide 18 by 12 to get a quotient of 1 and a remainder of 6. Then divide 12 by 6 to get a remainder of 0, which means that 6 is the gcd. Note that we ignored the quotient in each step except to notice when the remainder reached 0, signalling that we had arrived at the answer.
Christopher McCormack
@cmccormack
Nov 06 2017 16:18
Nice greenbelt for biking/jogging/frisby golf/golf/etc...
It's a very touristy friendly place
Jason Luboff
@JLuboff
Nov 06 2017 16:19
Hmm alright alright
Christopher McCormack
@cmccormack
Nov 06 2017 16:20
Summers still suck but in winter it's my favorite place
Jason Luboff
@JLuboff
Nov 06 2017 16:21
Hmmm... 6.5 hour drive with no traffic..
Christopher McCormack
@cmccormack
Nov 06 2017 16:21
@JLuboff that's not too bad! haha
Chris
@ConnectCodes
Nov 06 2017 16:21

@ConnectCodes here's an implementation of GCD directly from the pseudocode that should work:

function gcd(a, b) {
  var temp;
  while (b !== 0) {
    temp = b;
    b = a % b;
    a = temp;
  }
  return a;
}

Okay one question, why a = temp in the end?

Jason Luboff
@JLuboff
Nov 06 2017 16:21
Its longer than going to Vegas! Vegas I can get to in 4.5 hours
Christopher McCormack
@cmccormack
Nov 06 2017 16:23
@ConnectCodes best to read the article through, I'm no math teacher. The implementation I used was from Wikipedia - https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations
Chris
@ConnectCodes
Nov 06 2017 16:24
@cmccormack Okay thanks !
CamperBot
@camperbot
Nov 06 2017 16:24
connectcodes sends brownie points to @cmccormack :sparkles: :thumbsup: :sparkles:
:star2: 1256 | @cmccormack |http://www.freecodecamp.com/cmccormack
Christopher McCormack
@cmccormack
Nov 06 2017 16:24
@JLuboff hmm tough choice - Scottsdale is definitely cleaner and less crowded
at least the parts worth visiting
Jason Luboff
@JLuboff
Nov 06 2017 16:24
Well...we'll see what happens
Christopher McCormack
@cmccormack
Nov 06 2017 16:25
if you're ever force to visit for work or something, how about that :)
Jason Luboff
@JLuboff
Nov 06 2017 16:25
Sure!
And if you ever decide to come out California-way, you let me know
Christopher McCormack
@cmccormack
Nov 06 2017 16:26
I think we may be soon fiance wants to go to Disneyland
Jason Luboff
@JLuboff
Nov 06 2017 16:26
....that'll be expensive..
Margaret Greaney
@greaneym
Nov 06 2017 16:27
hi, I just found a very good resource about rogue games and think it will help people like me with little game background experience, http://arborwebsolutions.com/roguelikes/
Jason Luboff
@JLuboff
Nov 06 2017 16:27
Even for me, Disneyland is basically a mini-vacation and thats just for going for the day lol
$100+ for one damn ticket
Margaret Greaney
@greaneym
Nov 06 2017 16:27
for example, there's info about how to add tiles to the rot.js example.
Christopher McCormack
@cmccormack
Nov 06 2017 16:27
@greaneym did you figure out your keyboard inputs?
@JLuboff yeah we'll have to save up, wedding is December so maybe next fall
Margaret Greaney
@greaneym
Nov 06 2017 16:29
@cmccormack , I just have a "reset" button working so far. I haven't yet figured out how to use the synthetic react events but will keep trying.
how did you serve the js assets on codepen.io, as a minified bundle or did you point a resource as https path?
Jason Luboff
@JLuboff
Nov 06 2017 16:30
@cmccormack Oh wedding is coming up! Congrats! But ya I think last time we went... it was probably a $400 day. 2xTickets, parking (I think its $20-30?), food, gas ,etc. We just drove up for the day (only a 1.5-2 hour drive). It is fun though, gotta give it that
Christopher McCormack
@cmccormack
Nov 06 2017 16:30
I'm not sure what you mean by that @greaneym do you have a link?
@JLuboff ok maybe fall 2020 then haha
Jason Luboff
@JLuboff
Nov 06 2017 16:31
:D
Margaret Greaney
@greaneym
Nov 06 2017 16:32
I haven't put it on codepen yet because I'm trying to figure how to add js assets for the example. Just using the roguelike tutorial and working with interacting react to start with.
Christopher McCormack
@cmccormack
Nov 06 2017 16:34
@greaneym you mentioned my code was native js earlier but you wanted to try and use react components but I"m not sure I follow your logic there - events are in the DOM. The example I showed you was attaching keyboard handling code to window which is not something you can componetize as React is anchoring within the body
But I am curious to see what you find, if there is a more Reacty way to do it
If you only want the keyboard inputs while your map has focus then it should be pretty simple to add functional components that handle the inputs
Margaret Greaney
@greaneym
Nov 06 2017 16:37
@cmccormack , thanks again for your example. It is just that I read that react has what are called "synthetic events" and these are for using in place of native js, as I understand it anyway. I could be wrong. That is why I was originally asking about if we're supposed to use react for the x,y inputs as opposed to just as a container for the view and leave the rest of the game as is.
CamperBot
@camperbot
Nov 06 2017 16:37
greaneym sends brownie points to @cmccormack :sparkles: :thumbsup: :sparkles:
:star2: 1257 | @cmccormack |http://www.freecodecamp.com/cmccormack
Christopher McCormack
@cmccormack
Nov 06 2017 16:39
@greaneym yeah for keyboard events that are scoped to your main component or inner components those synthetic events are what I would suggest, and are easier than implementing in vanilla.
I forgot they called those synthetic events :)
alpox
@alpox
Nov 06 2017 16:40
@greaneym Synthetic events refer only to the event object you get passed to event handlers. They are nothing else than normal DOM event objects except that they look the same on all browsers - so its a very thin wrapper so to say
Margaret Greaney
@greaneym
Nov 06 2017 16:41
@cmccormack @alpox, thanks for extra info
CamperBot
@camperbot
Nov 06 2017 16:41
greaneym sends brownie points to @cmccormack and @alpox :sparkles: :thumbsup: :sparkles:
api offline
:star2: 1440 | @alpox |http://www.freecodecamp.com/alpox
Margaret Greaney
@greaneym
Nov 06 2017 16:42
I will come back later
Christopher McCormack
@cmccormack
Nov 06 2017 16:42
@JLuboff did you finish backend stuff on FCC?
Jason Luboff
@JLuboff
Nov 06 2017 16:44
@cmccormack I have to finish my Pinterest clone which I haven't really done much on
Otherwise, that is the final project for me to claim my cert
Christopher McCormack
@cmccormack
Nov 06 2017 16:44
nice! Going to start visualization after?
Jason Luboff
@JLuboff
Nov 06 2017 16:45
Yup! I just have to find the enthusiam to finish this one first.. lol
Christopher McCormack
@cmccormack
Nov 06 2017 16:45
Just toss something super generic together :D
I still haven't finished the url shortener, not enough time in the day!
Jason Luboff
@JLuboff
Nov 06 2017 16:49
Ya I will.. I have the authentication setup already (it requires using twitter..) but thats about it. Just a stupid project but oh well lol
url shortener isn't too bad.
Going from the API projects to the Dynamic web apps is a big jump in what you need to do
Kaz Baig
@kbaig
Nov 06 2017 16:51
@JLuboff It was so big of a jump that I stopped looking at fCC for a long while. Learned Angular and React at a basic level, did almost all of the data vis cert, and now I finally feel ready to get to it
Christopher McCormack
@cmccormack
Nov 06 2017 16:51
yeah I bet
Kaz Baig
@kbaig
Nov 06 2017 16:52
But I still need to learn basic and OAuth, which I'll do while doing the voting app
Jason Luboff
@JLuboff
Nov 06 2017 16:54
@kbaig Ya, it was a crazy jump. I stopped for a while to learn more before going back to it. Passport is great for auth. There's tons of different strategies (local, github, facebook, twitter, etc etc) and fairly easy to setup and get going with
Kaz Baig
@kbaig
Nov 06 2017 16:55
@JLuboff I did see that and mongoose for the first time when looking at the boilerplate we were given
I couldn't understand any part of the boilerplate at the time
Jason Luboff
@JLuboff
Nov 06 2017 16:55
@kbaig I wouldn't even bother with the boilerplate.. I went through the tutorial (typing everything out etc) And then couldn't figure out how to use it for the voting app so did everything myself lol
Kaz Baig
@kbaig
Nov 06 2017 16:56
Yeah I don't really want to learn how to set up vanilla MVC when I'm comfortable with React and it's not relevant to my goals anyway
Jason Luboff
@JLuboff
Nov 06 2017 16:58
Also... could never get mongoose to work so just stuck with vanilla Mongo
Kaz Baig
@kbaig
Nov 06 2017 16:58
What part did you struggle with?
in mongoose
Jason Luboff
@JLuboff
Nov 06 2017 16:59
I can't recall at this point, the last time I tried using mongoose was probably 6 months ago
Kaz Baig
@kbaig
Nov 06 2017 16:59
I'm probably going to get to that piece in the next couple of days. Right now, I have a basic front end built out and am working on the back end routes
Christopher McCormack
@cmccormack
Nov 06 2017 16:59
Are you just using ajax calls to your own api to use react + node?
Kaz Baig
@kbaig
Nov 06 2017 16:59
Correct
Christopher McCormack
@cmccormack
Nov 06 2017 16:59
I've been trying to figure out the best way to do it
you using react router or node routes?
Jason Luboff
@JLuboff
Nov 06 2017 17:00
Ajax works well in my experience (not with react, but in general)
Kaz Baig
@kbaig
Nov 06 2017 17:00
I am using React router
and express for back end
Christopher McCormack
@cmccormack
Nov 06 2017 17:00
@JLuboff yeah react lets you use react router which is a really nice and fast way to do routes
Kaz Baig
@kbaig
Nov 06 2017 17:01
Also using Redux, will add thunk middleware when hooking front and back together but haven't looked into how to do that just yet
Christopher McCormack
@cmccormack
Nov 06 2017 17:01
@JLuboff what's funny is ajax calls from React can actually be sort of complicated because of the lifecycle methods, you have to call it at the right time so as not to rerender or hold up rendering
Jason Luboff
@JLuboff
Nov 06 2017 17:02
lifecycle? :worried:
Christopher McCormack
@cmccormack
Nov 06 2017 17:02
took me longer than I'd like to admit to figure it out
You really should start React asap - it's like a whole new world and really changes they way you think about writing JS
Kaz Baig
@kbaig
Nov 06 2017 17:03
Currently, my plan is to finish the back end using a local mockup of the data. That way, I can build up what my schema is going to look like based on what the api needs to be capable of doing. Once the front end and back end are talking and functioning as desired, I'll actually use mongoose to implement an actual DP
Christopher McCormack
@cmccormack
Nov 06 2017 17:03
Making static pages feels clunky to me now, even with templates
Jason Luboff
@JLuboff
Nov 06 2017 17:04
I just want to finish backend...its taken me so damn long as it is (I tihkn I started in March?)
Kaz Baig
@kbaig
Nov 06 2017 17:04
@cmccormack I believe that's where redux thunk middleware comes in so simplify things, so AJAX with lifecycle hooks becomes simpler
Christopher McCormack
@cmccormack
Nov 06 2017 17:05
@kbaig I think my last project I used async/await which also helped a ton
Kaz Baig
@kbaig
Nov 06 2017 17:06
@cmccormack god bless acync/await, im using it rn
Christopher McCormack
@cmccormack
Nov 06 2017 17:07
once I got the hang of it it made things so much easier!
no more .then chaining
Kaz Baig
@kbaig
Nov 06 2017 17:07
yeeep
Christopher McCormack
@cmccormack
Nov 06 2017 17:07
Have you figured out an equivelent in node?
Jason Luboff
@JLuboff
Nov 06 2017 17:07
Newest version of node supports async/await
Kaz Baig
@kbaig
Nov 06 2017 17:08
You mean 9.0?
Jason Luboff
@JLuboff
Nov 06 2017 17:08
Well, Its been for a few versions now
I don't know the exact version they implemented it
There's also the async library https://caolan.github.io/async/
Kaz Baig
@kbaig
Nov 06 2017 17:09
But it's not in the LTS version for sure
I'm actually running babel on my back end to get async/await
Christopher McCormack
@cmccormack
Nov 06 2017 17:10
yeah the async library is different than async/await so I didn't bother to implement it just yet but I guess I could
@kbaig webpack loaders?
or just straight babel compiling
Christopher McCormack
@cmccormack
Nov 06 2017 17:12
oh nice just scripts
Kaz Baig
@kbaig
Nov 06 2017 17:12
ye pretty quick plug if you want some of the latest features
I also get a little annoyed that I have to use require for back end when I'm using import for front
Christopher McCormack
@cmccormack
Nov 06 2017 17:13
I'm so burnt out on preloading and all that, did so much work buildling my own webpack configs and working through optimizations - now I don't feel like using it at all haha
Kaz Baig
@kbaig
Nov 06 2017 17:13
I don't plan on bothering with that for quite some time, if at all
If I wanted to be a dev I would :)
Christopher McCormack
@cmccormack
Nov 06 2017 17:14
honestly all the work I did probably didn't make it any better or faster than create-react-app, but I wanted to learn
Kaz Baig
@kbaig
Nov 06 2017 17:14
That's a pretty good reason to do the work though
Christopher McCormack
@cmccormack
Nov 06 2017 17:14
create-react-app has some proprietary optimizations
yeah for sure
I still think stuff like webpack, while useful, is still too convoluted and I expect something nicer and easier to take over fairly soon
Jason Luboff
@JLuboff
Nov 06 2017 17:26
I hate Windows 98
Christopher McCormack
@cmccormack
Nov 06 2017 17:26
Damn, that's a retro statement
Jason Luboff
@JLuboff
Nov 06 2017 17:29
Yup. Not too happy about having to deal with it..
Christopher McCormack
@cmccormack
Nov 06 2017 17:29
With Node - how can you send a response to a user but then do a redirect after a few seconds?
Jason Luboff
@JLuboff
Nov 06 2017 17:29
Hmmm...never had to do that.
I would think setInterval or setTimeout... but.. if you already sent res.send then it wouldn't go to the redirect.. I don't think
Moisés Man
@moigithub
Nov 06 2017 17:31
handle on client i guess...
Christopher McCormack
@cmccormack
Nov 06 2017 17:31
@JLuboff yeah that's correct, you get a setHeader error
Kaz Baig
@kbaig
Nov 06 2017 17:31
@cmccormack you can send the response as usual and then call res.redirect('/...') with a timeout
Christopher McCormack
@cmccormack
Nov 06 2017 17:31
@moigithub that's what I was thinking, just include a small JS that does the redirect
Kaz Baig
@kbaig
Nov 06 2017 17:32
assuming you're using express
Christopher McCormack
@cmccormack
Nov 06 2017 17:32
@kbaig tried that but got a setHeader error as expected
I am using express
was hoping to do something that follows this logic:
  app.get('/:shorturl', (req, res, next) => {
    urldb.find({'short_url': req.params.shorturl}).toArray((err, docs) =>{
      if (docs.length === 0){
        res.send('Invalid Short URL')
        setTimeout(()=>{
          res.redirect('/')
        }, 2000)
      } else {
        res.redirect(docs[0].original_url)
      }
    })
  })
Kaz Baig
@kbaig
Nov 06 2017 17:34
can you show the error you're getting
Christopher McCormack
@cmccormack
Nov 06 2017 17:34
I believe the session is closed after res.send
Error: Can't set headers after they are sent.
i'll try and add a script tag that does the redirect and see if that works
Jason Luboff
@JLuboff
Nov 06 2017 17:36
Ya, what you have above won't work
since oyu have the timeout after your res.send
Stephen James
@sjames1958gm
Nov 06 2017 17:38
@cmccormack Not sure there is any reason to redirect the user anywhere in this case. IMO, just send back an error code 404
Christopher McCormack
@cmccormack
Nov 06 2017 17:38
@sjames1958gm I agree with you just though it would be nice to redirect them to the instruction page
Jason Luboff
@JLuboff
Nov 06 2017 17:38
@cmccormack Use flash message then
@cmccormack It'll redirect back to home page, and you can have it display a message on the same page
Christopher McCormack
@cmccormack
Nov 06 2017 17:39
this ended up working decently, if not super hacky:
  app.get('/:shorturl', (req, res, next) => {
    urldb.find({'short_url': req.params.shorturl}).toArray((err, docs) =>{
      if (docs.length === 0){
        let response = 'Invalid Short URL. '
        response += '<script>'
        response += 'setTimeout(()=>{'
        response += 'window.location = "/"'
        response += '}, 2000)'
        response += '</script>'
        res.send(response)
      } else {
        res.redirect(docs[0].original_url)
      }
    })
  })
Isaiah Trembley
@IsaiahCT
Nov 06 2017 17:39
he
howcome hackety hack doens't work?
Christopher McCormack
@cmccormack
Nov 06 2017 17:41
@JLuboff I thought of adding an element to my nunjucks template that only appeared when there was an error - is that similar to your thinking?
Jason Luboff
@JLuboff
Nov 06 2017 17:41
@cmccormack Correct
Christopher McCormack
@cmccormack
Nov 06 2017 17:41
ok maybe I'll do that instead :) thanks @JLuboff @sjames1958gm @kbaig
CamperBot
@camperbot
Nov 06 2017 17:41
cmccormack sends brownie points to @jluboff and @sjames1958gm and @kbaig :sparkles: :thumbsup: :sparkles:
:cookie: 409 | @kbaig |http://www.freecodecamp.com/kbaig
:star2: 2245 | @jluboff |http://www.freecodecamp.com/jluboff
:star2: 8672 | @sjames1958gm |http://www.freecodecamp.com/sjames1958gm
Jason Luboff
@JLuboff
Nov 06 2017 17:41
@cmccormack Essentially flash message will initiate an object that you can then send to your frontend
Christopher McCormack
@cmccormack
Nov 06 2017 17:44
is that a node thing or just a css component?
Jason Luboff
@JLuboff
Nov 06 2017 17:45
its a package, I use connect-flash
Christopher McCormack
@cmccormack
Nov 06 2017 17:45
oh ok
Christopher McCormack
@cmccormack
Nov 06 2017 19:20
Think I'm done - anyone mind testing it out? https://shortyurl.glitch.me/
Jason Luboff
@JLuboff
Nov 06 2017 19:25
@cmccormack Good job, working for me. Accepted https, http, and ftp
Christopher McCormack
@cmccormack
Nov 06 2017 19:29
awesome thank you for checking
@JLuboff thanks
CamperBot
@camperbot
Nov 06 2017 19:29
cmccormack sends brownie points to @jluboff :sparkles: :thumbsup: :sparkles:
:star2: 2246 | @jluboff |http://www.freecodecamp.com/jluboff
Jason Luboff
@JLuboff
Nov 06 2017 19:33
:+1:
Christopher McCormack
@cmccormack
Nov 06 2017 19:41
@JLuboff what api did you use for the image search?
Jason Luboff
@JLuboff
Nov 06 2017 19:42
@cmccormack Google api with the node-google-image-searchpackage
Christopher McCormack
@cmccormack
Nov 06 2017 19:43
man there really is a package for everything isn't there
thanks again @JLuboff
CamperBot
@camperbot
Nov 06 2017 19:43
cmccormack sends brownie points to @jluboff :sparkles: :thumbsup: :sparkles:
api offline
Jason Luboff
@JLuboff
Nov 06 2017 19:44
Absolutely. And while it makes things easier, for my last couple projects, I ended up making my own "service" to hit the apis just for the sake of knowing how and having a bit more control
Christopher McCormack
@cmccormack
Nov 06 2017 19:44
What do you mean by service?
code wrapped around the api calls?
Jason Luboff
@JLuboff
Nov 06 2017 19:45
@cmccormack Just basically creating my own method for getting info from the API. See here: https://github.com/JLuboff/GameTrader/tree/master/services
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 19:47
hello everyone
Christopher McCormack
@cmccormack
Nov 06 2017 19:47
oh ok that would make it a bit easier
Jason Luboff
@JLuboff
Nov 06 2017 19:47
:wave:
Christopher McCormack
@cmccormack
Nov 06 2017 19:47
Hi @tiagocorreiaalmeida
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 19:47
how's everything?
Jason Luboff
@JLuboff
Nov 06 2017 19:48
Doing alright
Christopher McCormack
@cmccormack
Nov 06 2017 19:48
Good good
@tiagocorreiaalmeida what are you working on these days?
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 19:49
started yesterday the stock the market chart
I atually used one day to get 1 api and the charts thing working :D
just the basic ahah
Jason Luboff
@JLuboff
Nov 06 2017 19:50
@tiagocorreiaalmeida At this rate...you're going to finish your backend cert before me ;)
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 19:50
now trying to understand if im avaible to remove just 1 element from the chart directly or I need to recreate it
Christopher McCormack
@cmccormack
Nov 06 2017 19:50
I'm actually excited to do that one soon
Jason Luboff
@JLuboff
Nov 06 2017 19:51
@tiagocorreiaalmeida Are you using chart.js?
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 19:51
had to recap some sockets but nothing that andrew mead couldnt help :D
I atually took some hours to get working the one they use on the exmple
highcharts
I think that's the name
Jason Luboff
@JLuboff
Nov 06 2017 19:51
Don't you have to pay for that one?
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 19:52
I was reading around and for what I saw if its not for comercial porpuse you are free to use it I might be wrong tough
Jason Luboff
@JLuboff
Nov 06 2017 19:52
Oh..huh. I thought you had to pay for it so said fuck it and used chart.js but its not as nice
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 19:53
this does chart does the job for you the only thing yo uneed to do is send the data asceding but that's not big of problem, let me see it
Christopher McCormack
@cmccormack
Nov 06 2017 19:54
Is chart.js doing the animations or was that you? @JLuboff
Jason Luboff
@JLuboff
Nov 06 2017 19:54
@cmccormack chart.js handles it
Christopher McCormack
@cmccormack
Nov 06 2017 19:54
nice
looks good, probably using d3 underneath I bet
Jason Luboff
@JLuboff
Nov 06 2017 19:55
chart.js is a decent library, but the documentation is lacking (aren't they always..). I'll be happy to learn D3 when I get a chance
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 19:55
btw @JLuboff I was confusing what api use to get the data there was people talking about yaho financialbut it was outdate or something, so I checked your github to get a reference hope that wasnt a bad idea since its an open source place tough, looks great, when I remove one stock do you recreate the chart or there's a option to do it?
@cmccormack adding stocks and im removing :D
like refresh chart
Jason Luboff
@JLuboff
Nov 06 2017 19:56
@tiagocorreiaalmeida Ya I don't mind. I think we've all done that at some point.
For chart.js, you have to destroy and recreate the chart each time
Oh man..somebody deleted all the stocks! :P
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 19:57
removed all to see how it behaved, looks great
yeah was me
:D
Christopher McCormack
@cmccormack
Nov 06 2017 19:58
haha
but a block for @tiagocorreiaalmeida in your api @JLuboff
blacklist him
Jason Luboff
@JLuboff
Nov 06 2017 19:59
Ain't my API to blacklist him from :D
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 19:59
you can get my ip and block it tough :D
image.png
this is what I got so far
time to improve it
Christopher McCormack
@cmccormack
Nov 06 2017 20:00
what's the piece with handlebars at the bottom?
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 20:01
what piece?
Christopher McCormack
@cmccormack
Nov 06 2017 20:01
above the horizontal scrollbar
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 20:01
its to change the data range
image.png
and the scroll lets you scroll using the rage from above
image.png
Christopher McCormack
@cmccormack
Nov 06 2017 20:02
I like that a lot
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 20:02
its atually greate I took a while to figure out how to make this work, the best its that they atually give you demos of jsfidle working for you to wrokaround with it
alpox
@alpox
Nov 06 2017 20:05
@tiagocorreiaalmeida Is that highcharts? :D
Ah yea
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 20:05
yeah :D
found out you can refresh data holy :D
alpox
@alpox
Nov 06 2017 20:05
I always loved to make charts with plain d3 though :D
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 20:06
well on my work today a guy got infected with ransomware they wanted bitcoins :D
Ronique Ricketts
@RoniqueRicketts
Nov 06 2017 20:06
@tiagocorreiaalmeida really Sorry wrong chat. lol
Christopher McCormack
@cmccormack
Nov 06 2017 20:07
@alpox yeah once you get the hang of it it's really nice
Ronique Ricketts
@RoniqueRicketts
Nov 06 2017 20:07
I am just trading up my coins today the market is really nice to me today :)
alpox
@alpox
Nov 06 2017 20:08
@cmccormack Yea it really is. It didn't even take much to create areas over points, create a convex hull to cover them all nicely with outer boundaries etc.
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 20:08
np @RoniqueRicketts how's express going?
alpox
@alpox
Nov 06 2017 20:08
There are just no restrictions, thats whats awesome @cmccormack
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 20:08
@alpox sadly I know 0 about d3 but the time for that will come
Christopher McCormack
@cmccormack
Nov 06 2017 20:09
I sort of feel like you guys should have done data vis first just so you had cool stuff to play with when you started doing backend projects :)
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 20:09
I atually felt that on the first projects but well :D
project*
Jason Luboff
@JLuboff
Nov 06 2017 20:10
@cmccormack I think you're right...but backend had more relevance to what I need to know hence my approach
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 20:10
@JLuboff did you handled outdated data ?
like data from today is not correct data for tomorrow
Jason Luboff
@JLuboff
Nov 06 2017 20:11
I only show stocks that were added that day
Christopher McCormack
@cmccormack
Nov 06 2017 20:11
gotta run ttyal
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 20:18
how's the pinterest going?
Jason Luboff
@JLuboff
Nov 06 2017 20:18
Haven't done anything beyond getting twitter auth setup lol
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 20:21
well that's something :D I wanna try to get at least the book thing done unthil december middle
correct me if im wrong but the difference from an twitter auth steup or others vs local strategy its just about making the emails our username unique and take care of the password hash right?
will try to use it next time
Jason Luboff
@JLuboff
Nov 06 2017 20:22
Ya, with local strat you're having to create username/passwords vs others that you're getting the information from that source
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 20:23
ok thanks @JLuboff
CamperBot
@camperbot
Nov 06 2017 20:23
tiagocorreiaalmeida sends brownie points to @jluboff :sparkles: :thumbsup: :sparkles:
:star2: 2247 | @jluboff |http://www.freecodecamp.com/jluboff
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 20:47
so I Just found the best gif on the internet im just not sure if this is the right place to share it :D
Jason Luboff
@JLuboff
Nov 06 2017 20:49
Can't be worse than some of the things posted here
Ronique Ricketts
@RoniqueRicketts
Nov 06 2017 20:50
@tiagocorreiaalmeida Its going ok.
I've considered doing one of the FCC challenges. Not sure I am ready but its worth a try
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 20:55
@RoniqueRicketts jump on it if you dont know how to do somethings you will search for them and findout how to do them so its a good step to learn
Ronique Ricketts
@RoniqueRicketts
Nov 06 2017 21:22
@tiagocorreiaalmeida Well true
ok I started. LOl that video is so funny. hahaha
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 21:25
gl mate anything this is the place to ask for help :D
Ronique Ricketts
@RoniqueRicketts
Nov 06 2017 21:26
Yes I will surely bother you nerds for help
Jason Luboff
@JLuboff
Nov 06 2017 21:26
I mean...technically the HelpBackEnd is the room to ask for node help...but..I'll let it slide.. :P
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 21:27
tbh if wasnt the help of the people on this rooms I would have quited js in the first weeks of fcc
it's great to have somewhere you can get opinions/help when learning
Ronique Ricketts
@RoniqueRicketts
Nov 06 2017 21:27
@JLuboff NO!!! your wrong. We nerds come here for help. :)
Lorena
@lore-code
Nov 06 2017 21:35
Who wants to pair program with me the pig latin algorithm? I almost have it :worried:
Kristina
@kikidesignnet
Nov 06 2017 21:39
Help? My JS Calculator is almost done except for two problems: https://codepen.io/kikibres/pen/MEQvqv.
Problems
  1. When trying to input "0", subMath shows 00 - 6
  2. Equation doesn't work properly. For example, I entered 1 + 2 and then press "=", showing 3, but when I try to click on "+" afterward, it add 3 to the subMath, showing 1 + 23. Therefore, after "=" sign, mainMath should be equal to the final answer.
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 21:40
@lore-code just tell you what's missing and maybe we can help you with that
Lorena
@lore-code
Nov 06 2017 21:41
@tiagocorreiaalmeida okay will do later on then. Thanks.
CamperBot
@camperbot
Nov 06 2017 21:41
lore-code sends brownie points to @tiagocorreiaalmeida :sparkles: :thumbsup: :sparkles:
:cookie: 429 | @tiagocorreiaalmeida |http://www.freecodecamp.com/tiagocorreiaalmeida
Kristina
@kikidesignnet
Nov 06 2017 21:49
Hello? Anyone there?
poke? Guess I'll just post on Freecodecamp forum....
Isaiah Trembley
@IsaiahCT
Nov 06 2017 21:49
hi
I am here
sorry
Ronique Ricketts
@RoniqueRicketts
Nov 06 2017 21:49
Hello @kikidesignnet
Isaiah Trembley
@IsaiahCT
Nov 06 2017 21:50
what is the needed info?
Kristina
@kikidesignnet
Nov 06 2017 21:50
Problems
1.When trying to input "0", subMath shows 00 - 6
2.Equation doesn't work properly. For example, I entered 1 + 2 and then press "=", showing 3, but when I try to click on "+" afterward, it add 3 to the subMath, showing 1 + 23. Therefore, after "=" sign, mainMath should be equal to the final answer.
I have to go sorry! Talk to you later!!!
I'll post on Freecodecamp forum later!
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 22:00
hey so for what I saw you have 2 options
you either clean the string to only 1 value after getting the result in this case to the result ammount and then you keep going on and everything will work as expected
other options is store result in a variable and display it, if the user start's to add a value to the existent result you go back and pick the string you have something like "3+2+4+5" and add what the user did
in other words if you had this to your code
  function solveEqual() {
        console.log(subMath);
    console.log(mainMath);
and look at the console you will see the issue
image.png
you are changing the mainmath eveything you calculate something, hope it wasnt confusing tough
everytime*
Armaan Dhanji
@armaandh
Nov 06 2017 22:29
would anyone be able to help me with a google maps api key issue i'm having?
it keeps saying i'm not including an api key...even though i'm sure I am
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 22:29
try to console.log the url you are using to make the request and copy it and place on brwoser and see if returns your data or any error
i was making a dummy website to practice some html
and css
if you go to that github pages site and see the console
I get the error:
Google Maps API error: MissingKeyMapError
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 22:31
can I see how you are setting the key?beside the key itself change the key value to somethingelse
Armaan Dhanji
@armaandh
Nov 06 2017 22:31
sure
i'm using
it has a function like this
function generateGoogleMapStatic(width, height) {
var width = width || 640,
height = height || 640,
mapApiKey = 'insertKeyHere',
and a second function like this
googleMapGenerator.options = {
containerClass: 'map',
frameClass: 'mapframe',
printClass: 'map
print',
legendClass: 'map__legend',
mapDynamicClass: 'gm-style',
apiKey: 'null',
legend: null,
print: null,
I cut them off for brevity
here's a code pen of it working
when I run my site with local host i don't have issues
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 22:33
are you setting your key as a string aswell or?
Armaan Dhanji
@armaandh
Nov 06 2017 22:33
but the API key missing error comes up on github pages
yes i'm passing my api key into the object
as a string
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 22:34
uh that's strange
since this is a dummy website, I don't mind the api key being visible
you can see how I set it on that link there
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 22:35
can you console log this
  mapApiKey = '&amp;key=' + settings.key;
and tell me what comes back?
nm it wont run
you dont have it defined
well that's alot of code to try to debug :D but you say it works locally?
Moisés Man
@moigithub
Nov 06 2017 22:37
why &amp;
i think u NO need to escape that
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 22:38
he doesnt defines it at all the code above only runs if an object proprety exists if im not mistaken
Armaan Dhanji
@armaandh
Nov 06 2017 22:40
yeah um...
locally it works...
which is weird..
Moisés Man
@moigithub
Nov 06 2017 22:41
soo whats a full url STRING (with key) generated
can u paste it here ? so i can open on my browser :)
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 22:42
^
Armaan Dhanji
@armaandh
Nov 06 2017 22:42
the full URL string
would that be what the function is generating?
Moisés Man
@moigithub
Nov 06 2017 22:43
ye
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 22:45
there's a little thing hided on your app start page
I think that's a ? signal
if you click there you enable the key
Armaan Dhanji
@armaandh
Nov 06 2017 22:47
armaandh/canada-site@7b0c0b1
just tried to console log the key
my app start page?
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 23:28
@armaandh sorry
for taking so long :|
in your google app page
yes
can you got there now?
Armaan Dhanji
@armaandh
Nov 06 2017 23:31
hey
i managed to just hardcode the api right into the url
towards the end there was something like
script.src = 'https://maps.googleapis.com/maps/api/js?' + 'key=XXXXXXXXXXXXX&' + 'callback=googleMapGenerator';
i used string concatenation and just punched it in
i'm not sure why it wasn't able to read the key out of the object..
since there was a config object which specifically had an option for entering the apiKey
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 23:32
happy you figured it out mate
Armaan Dhanji
@armaandh
Nov 06 2017 23:32
but alas
thanks for your help Tiago
and to everyone else
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 23:33
I didnt helped at all and made you wait but np
Jason Luboff
@JLuboff
Nov 06 2017 23:52
Now that I looked at the example Pinterest clone...I guess its not all that bad lol
Tiago Correia
@tiagocorreiaalmeida
Nov 06 2017 23:59
the concept or?:D