These are chat archives for FreeCodeCamp/HelpJavaScript

25th
Nov 2017
Stephen James
@sjames1958gm
Nov 25 2017 00:30
@cmurphy580 Yes you would need to save to something like local storage
@cmurphy580 The React recipe project has you save the recipes to local storage
Emerson Lopes
@lupuselit
Nov 25 2017 02:04
Closure is when we can access data from a function even after the function had returned something else??
Ken Haduch
@khaduch
Nov 25 2017 02:20
@emersonlbr - here's a good article about closure: http://javascriptissexy.com/understand-javascript-closures-with-ease/
Emerson Lopes
@lupuselit
Nov 25 2017 02:22
Thanks!
WAINGOR
@WAINGOR
Nov 25 2017 09:45
Hi guys! Its been a while since I roamed these forums :D I am looking through some old excercises and cant figure out why FCC says this is wrong (excercise factorialize) :smile:
var K = 1;
function factorialize(factorLVW) {
        for (var i=1; i<=factorLVW; i++) {
        K*=i;
    }
return K;
}

factorialize(0);
abraham anak agung
@padunk
Nov 25 2017 10:08
@WAINGOR u have global variable
Jim Tryon
@jimtryon
Nov 25 2017 10:15
Yeah, what @padunk said
WAINGOR
@WAINGOR
Nov 25 2017 10:20
@padunk so? this var is still available inside the function right?
and it produces the right result...
Jim Tryon
@jimtryon
Nov 25 2017 10:22
well, actually the code inside is commented out for some reason
You can tell because it is in dark grey
Stephen James
@sjames1958gm
Nov 25 2017 10:50
@WAINGOR When FCC runs the tests it cannot reset your global back to 1 so after the first test K is not reset to 1 and the later tests fail
Tiago Correia
@tiagocorreiaalmeida
Nov 25 2017 12:44
gm everyone
Ken Haduch
@khaduch
Nov 25 2017 13:50
@WAINGOR - welcome back!
@tiagocorreiaalmeida - good morning
Tiago Correia
@tiagocorreiaalmeida
Nov 25 2017 15:46
this looks dead lately :D
alpox
@alpox
Nov 25 2017 16:19
@tiagocorreiaalmeida *spreading life
Tiago Correia
@tiagocorreiaalmeida
Nov 25 2017 16:23
@alpox do you like lotr?
alpox
@alpox
Nov 25 2017 16:29
@tiagocorreiaalmeida sure i do
AbrisM
@AbrisM
Nov 25 2017 19:37
https://jsfiddle.net/wme60v7q/ Hi, does someone know how to run this code WITHOUT jquery? Just plain old javascript and html?
Uriel Guzmán
@Burinson
Nov 25 2017 19:55
function orbitalPeriod(arr) {
  var GM = 398600.4418;
  var earthRadius = 6367.4447;
  var alt = arr[0].avgAlt;
  var twoPI = 6.2832;
  var ERandALT = earthRadius + alt;
  var cubic = Math.pow(ERandALT, 3);
  var division = cubic / GM;
  var sqrt = Math.sqrt(division);
  var orbitalPeriod = Math.round(sqrt * twoPI);
  var oldKey = Object.keys(arr[0])[1];
   oldKey = "orbitalPeriod";

  console.log(oldKey);







  return arr;
}

orbitalPeriod([{name : "sputnik", avgAlt : 35873.5553}]);
Guys, how can I replace avgAlt with orbitalPeriod
And the corresponding values of course
I want to rename the key
Moisés Man
@moigithub
Nov 25 2017 20:06
create a new object
AbrisM
@AbrisM
Nov 25 2017 20:09
https://jsfiddle.net/wme60v7q/ Does anyone know how to convert the jquery methods into regular javascript?
Uriel Guzmán
@Burinson
Nov 25 2017 20:14
@moigithub
function orbitalPeriod(arr) {
  var GM = 398600.4418;
  var earthRadius = 6367.4447;
  for (var i in arr) {
  var alt = arr[i].avgAlt;
  var twoPI = 2 * 3.14159265359;
  var ERandALT = earthRadius + alt;
  var cubic = Math.pow(ERandALT, 3);
  var division = cubic / GM;
  var sqrt = Math.sqrt(division);
  var OP = Math.round(sqrt * twoPI);
  var oldKey = Object.keys(arr[i])[1];

    delete arr[i].avgAlt;
  arr[i].orbitalPeriod = OP; 
  }

  return arr;
}

orbitalPeriod([{name: "iss", avgAlt: 413.6}, {name: "hubble", avgAlt: 556.7}, {name: "moon", avgAlt: 378632.553}]);
@moigithub Solved it, thanks anyway
CamperBot
@camperbot
Nov 25 2017 20:15
burinson sends brownie points to @moigithub :sparkles: :thumbsup: :sparkles:
:star2: 3720 | @moigithub |http://www.freecodecamp.org/moigithub
Pieter Stokkink
@forkerino
Nov 25 2017 20:33
@Burinson That code can be improved a bit. Generally, you would only need to create a variable if you are going to use something more than once (although for GM and earthRadius, I would use a variable), which would remove a lot of the intermediate steps and makes it a lot easier to read, in my opinion. Also make sure you do proper indentation inside the for loop. If you want to improve it further, it would make a lot of sense to use .map instead of the for loop.
@AbrisM have you tried googling them? as in $.each() in vanilla JavaScript There are tons of examples on how to convert them online.
Uriel Guzmán
@Burinson
Nov 25 2017 20:55
@forkerino I will improve it with your suggestion. I did it that way because for me it's easier to digest if I do everything step by step (like in algebra) instead mushing the whole equation together. Is it a bad practice to do this? Thanks.
CamperBot
@camperbot
Nov 25 2017 20:55
burinson sends brownie points to @forkerino :sparkles: :thumbsup: :sparkles:
:star2: 2954 | @forkerino |http://www.freecodecamp.org/forkerino
Pieter Stokkink
@forkerino
Nov 25 2017 21:15
@Burinson It doesn't have to be bad, but if you do make intermediate variables, it will be harder to read for other coders or your future self. Especially if you use non-descript variable names like cubic or division (cubic of what? division of what? can you find a name that will be more self-explanatory?). In this case having it all on one line might indeed be a bit too much, but there are a couple candidates I would get rid of immediately (twoPi, alt) and some that would do better with proper naming or removing them. If I would break the formula up, I would use something like var distanceFromCenterCubed = Math.pow(earthRadius + arr[i].avgAlt, 3) to start. Not too broken down, not too complex.
Uriel Guzmán
@Burinson
Nov 25 2017 21:24
@forkerino Alright
function orbitalPeriod(arr) {
  var GM = 398600.4418;
  var earthRadius = 6367.4447;
  for (var i in arr) {
  var distanceFromCenteredCube = Math.pow(earthRadius + arr[i].avgAlt, 3);
  var squareRoot = Math.sqrt(distanceFromCenteredCube / GM);
  var OP = Math.round(squareRoot * 2 * 3.14159265359); // Orbital period rounded.

    delete arr[i].avgAlt; // Delete key.
  arr[i].orbitalPeriod = OP; // Create new property
  }

  return arr;
}

orbitalPeriod([{name: "iss", avgAlt: 413.6}, {name: "hubble", avgAlt: 556.7}, {name: "moon", avgAlt: 378632.553}]);
Do you think that's simplified enough?
Pieter Stokkink
@forkerino
Nov 25 2017 21:30
@Burinson I think so, although you could use Math.PI instead of hardcoding pi.
AbrisM
@AbrisM
Nov 25 2017 22:06
@forkerino Yes but they don't have the specific ones that i'm looking for hahaha
Stephen James
@sjames1958gm
Nov 25 2017 22:13
@Burinson one final comment - I think that using array.map would be better than changing the input array.
Daniel Romero
@Ranacode
Nov 25 2017 22:55
Don't know what is the main reason that throwing me this error
Unhandled rejection Error: pool is draining and cannot accept work
    at Pool.acquire (/home/ranacode/Escritorio/meetup_server/node_modules/generic-pool/lib/Pool.js:400:35)
    at promise.then (/home/ranacode/Escritorio/meetup_server/node_modules/sequelize/lib/dialects/abstract/connection-manager.js:281:24)
    at tryCatcher (/home/ranacode/Escritorio/meetup_server/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/home/ranacode/Escritorio/meetup_server/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/home/ranacode/Escritorio/meetup_server/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (/home/ranacode/Escritorio/meetup_server/node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/home/ranacode/Escritorio/meetup_server/node_modules/bluebird/js/release/promise.js:693:18)
    at Async._drainQueue (/home/ranacode/Escritorio/meetup_server/node_modules/bluebird/js/release/async.js:133:16)
    at Async._drainQueues (/home/ranacode/Escritorio/meetup_server/node_modules/bluebird/js/release/async.js:143:10)
    at Immediate.Async.drainQueues (/home/ranacode/Escritorio/meetup_server/node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:785:20)
    at tryOnImmediate (timers.js:747:5)
    at processImmediate [as _immediateCallback] (timers.js:718:5)
import "babel-polyfill";
import chai from "chai";
import { expect } from "chai";
import chaiHttp from "chai-http";

chai.use(chaiHttp);

import database from "../../config/dbconnection";
import Models from "../index";

describe("User_model", function() {
  describe("#save_operations", function() {
    it("should gets the user correctly", function(done) {
      Models.User.sync({ force: true }).then(() => {
        return Models.User.create({
          username: "Daniel",
          email: "daniel@daniel.com",
          password: "daniel"
        });
        Models.User.findOne({ where: { username: "Daniel" } }).then(user => {
          expect(user).to.not.be(null);
        });
      });
      done();
    });
  });

  after(function(done) {
    database.close();
    return done();
  });
});