These are chat archives for FreeCodeCamp/HelpJavaScript

23rd
May 2018
Keggatron
@Keggatron
May 23 2018 00:00
wehn I run this code it logs back just
body { date: '20180511',
 booking: 
   [ { booked: true },
    { booker: 'Keggatron' },
    { phone: '555555555' } ] }
I want it to log this:
body { date: '20180511',
"10:30": 
     [ { booked: true },
       { booker: 'Keggatron' },
       { phone: '555555555' } ] }
and 11:00 and 11:30 depending on how many values I have in values.booking
does that make sense?
Moisés Man
@moigithub
May 23 2018 00:42
ok ... yep
wrap booking variable on []
like this [booking] <-- Computed property names an es6 feature
Anne L
@mathisme
May 23 2018 01:49
Is there a reason 10:30 and such is an array of objects and not just an object?
ehutchllew
@ehutchllew
May 23 2018 02:24
@mathisme It should be an object, or even better - a Map.
cjm
@mrscjm
May 23 2018 02:39

function destroyer(arr) {

 return arguments[0].filter(function(val) {

    for (var i = 1; i < arguments.length - 1; i++) {
    return val !== arguments[2];
}

});

}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);

Why not?
Claudio Restifo
@Marmiz
May 23 2018 02:57
@mrscjm arguments inside the filter function is a different object that the one used for destroyer.
Moisés Man
@moigithub
May 23 2018 03:26
every function have its OWN arguments object.. except arrow function (if i recall) @mrscjm
Henry
@GitHub-Henry
May 23 2018 04:50
image.png
Claudio Restifo
@Marmiz
May 23 2018 04:51
:scream: that color palette!
Henry
@GitHub-Henry
May 23 2018 05:00
it's eclectic
notice the wide range of colors
Claudio Restifo
@Marmiz
May 23 2018 05:07
looks like a crayon box :)
cjm
@mrscjm
May 23 2018 05:31
thanks
KingKongg
@KingKongg
May 23 2018 07:39
Hi
Can anyone help me with my code.
function findLongestWord(str) {
var x=str.split("");// Convert the string to separate words
var y=x[0];
for(i=0;i<x.length;i++);{ if(x[i].length>=x.length)
y=x[i];
}
return y.length;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
Markus Kiili
@Masd925
May 23 2018 07:42
@KingKongg You are splitting into an array of characters there.
KingKongg
@KingKongg
May 23 2018 07:45
@Masd925 Got it! Thank you!!
CamperBot
@camperbot
May 23 2018 07:45
kingkongg sends brownie points to @masd925 :sparkles: :thumbsup: :sparkles:
:star2: 4802 | @masd925 |http://www.freecodecamp.org/masd925
Jen
@jenniredfield
May 23 2018 07:52
Hi guys
say you are have a React app with the state
   state = {
        ingredients: {
            salad: 3,
            bacon: 2,
            cheese: 1,
            meat: 1
        }
    }
and you want to output an ingredient component for each ingredient dynamically.. how would you do it?
Claudio Restifo
@Marmiz
May 23 2018 08:32

@jenniredfield

{Object.keys(this.state.ingredeints)
  .map(ingredient => <p>{ingredient}  {this.state.ingredients[ingredient] }</p>}

will print a p with name and value.
There are many other ways as well... it's just javascript :)

Greg Duncan
@GregatGit
May 23 2018 08:32
@jenniredfield i think you would use Object.keys to make an array from the ingredients.
Is there a good reason that you made the state with numbers?
Why not just have ingredients and have it as an array
Henry
@GitHub-Henry
May 23 2018 08:59
@KingKongg
image.png
Anabel
@AnabelSalomone
May 23 2018 09:15
Can anyone help me configure eslint for vscode? :)
I want it to tell me when a rule is not respected but it doesn't seem to work :(
Claudio Restifo
@Marmiz
May 23 2018 09:18
do you have an eslintrc file at the root of your project folder?
or a eslintConfig in package.json
(and the plugin installed for VSCode)
Anabel
@AnabelSalomone
May 23 2018 09:31
Yes to both questions :) @Marmiz
Jen
@jenniredfield
May 23 2018 09:38
@Marmiz and @GregatGit you would want the same number of components as the same number of the value on the object
like salad: 2 would output <p>salad</p><p>salad</p>
This is just from a tutorial I was following, the guy gave an answer but I was curious of other ways of solving this. as I think he overcomplicated it.
Claudio Restifo
@Marmiz
May 23 2018 09:59
@jenniredfield I'd just use createElement() or cloneElement()
Stephen James
@sjames1958gm
May 23 2018 10:17
@jenniredfield - this might work (caveat - not debugged
// here is a function that runs a certain function n times
let times = x => f => (
    x > 0 ? 
    f().concat(ts(x-1)(f)) :
    []
    )
    // Have this utility function and call it in render
   {Object.keys(this.state.ingredients)
  .map(ingredient => times(this.state.ingredients[ingredient]) (() => ([<p>{ingredient}</p>]))}
Claudio Restifo
@Marmiz
May 23 2018 10:37
@sjames1958gm I would have never thought of that.
I mean using concat.
I would have thought of f.call() with a while loop.
:clap: :clap:
Stephen James
@sjames1958gm
May 23 2018 10:39
@Marmiz thanks - was trying to stay functional without mutating any variables :tired_face:
CamperBot
@camperbot
May 23 2018 10:39
sjames1958gm sends brownie points to @marmiz :sparkles: :thumbsup: :sparkles:
:star2: 1236 | @marmiz |http://www.freecodecamp.org/marmiz
Claudio Restifo
@Marmiz
May 23 2018 10:40
@sjames1958gm I only fear for the context and this. do you know how it behave with that concat?
Stephen James
@sjames1958gm
May 23 2018 10:41
@Marmiz Not sure what happens with this in the arrow function recursion, don't think the concat itself would be an issue.
Claudio Restifo
@Marmiz
May 23 2018 10:58
@sjames1958gm my pale attempt at it. Not as fp :)
let times = (n, fn, context = undefined) => {
  let i = 0;
  while(fn.call(context, i) !== false && ++i < n) {}
}


times(2, i => console.log(i)) // 0 1
i peeked at lodash tho
Jen
@jenniredfield
May 23 2018 11:48
Hi @sjames1958gm that seems cool but I dont get it ?
@sjames1958gm whats ts?
Stephen James
@sjames1958gm
May 23 2018 12:12
@jenniredfield ts should be times it is the recursive call
My bad here is an update
// here is a function that runs a certain function n times
let times = x => f => (
    x > 0 ? 
    f().concat(times(x-1)(f)) :
    []
    )
Jen
@jenniredfield
May 23 2018 12:27
@sjames1958gm ah better now. Still how would you write this in ES5 syntax, without arrows?

```
var times = function x (f) {

}


var times = function x (f) {


    return function f() {
        x > 0 ? :
        f().concat(times(x-1)(f)) :
        []
    }
}
??
Stephen James
@sjames1958gm
May 23 2018 12:38
@jenniredfield You need another return inside of f()
Jen
@jenniredfield
May 23 2018 12:41

var times = function x (f) {


    return function f() {
     return    x > 0 ? :
        f().concat(times(x-1)(f)) :
        []
    }
}
hang on...
how do you even invoke this?
also the x should be an argument right
Stephen James
@sjames1958gm
May 23 2018 12:45
@jenniredfield function(x)
@jenniredfield Yes that is right
Jen
@jenniredfield
May 23 2018 12:45
I dont need x or f functions?
Stephen James
@sjames1958gm
May 23 2018 12:46
@jenniredfield You don't need to name them
Jen
@jenniredfield
May 23 2018 12:46

var times = function (x) {
    return function (f) {
     return    x > 0 ? :
        f().concat(times(x-1)(f)) :
        []
    }
}
baaa
Stephen James
@sjames1958gm
May 23 2018 12:47
// here is a function that runs a certain function n times
let times = function(x) {
   return function(f) {
     return  x > 0 ? 
          f().concat(times(x-1)(f)) :
          []
    }
}
Jen
@jenniredfield
May 23 2018 12:48
how do you invoke it.
can you pass console.log to it to print 'hello' 4 times?
or the function you call has to return an array?
Stephen James
@sjames1958gm
May 23 2018 12:57
The function you call can return an array or a string (something that supports concat)
times(3)(() => "test")
Jen
@jenniredfield
May 23 2018 12:59
doesnt work on my console
actually, now it did
how would you call any function a number of times?
just with loops? say console.log
actually that does not seem complicated
Stephen James
@sjames1958gm
May 23 2018 13:09
Well you could of course
for (let i = 0; i < count; i++) f();
Jen
@jenniredfield
May 23 2018 13:18
that's what I thought haha
Chris
@ConnectCodes
May 23 2018 13:27
Is there a way to indefinatly repeat var pomodoro = setInterval(timer,1000)in pomodoro project? I tried creating a infinite loop with var i = 1 while(i ==1){} but not suprisingly it crashes the page. Also I added else if in the end and called var pomodoro = setInterval(timer,1000); but again it crashed. Any ideas thanks
https://codepen.io/Codeconnect/pen/wjYGZQ?editors=0010
Nemanja Simeunovic
@Saymon85
May 23 2018 14:02
@ConnectCodes maybe some clearInterval() may help... just as first tought.... ;)
Chris
@ConnectCodes
May 23 2018 14:08
@Saymon85 hvala brate :smile: thanks
CamperBot
@camperbot
May 23 2018 14:08
connectcodes sends brownie points to @saymon85 :sparkles: :thumbsup: :sparkles:
:cookie: 362 | @saymon85 |http://www.freecodecamp.org/saymon85
Pieter Stokkink
@forkerino
May 23 2018 14:08
@ConnectCodes setInterval already repeats the callback indefinitely.
Jason Luboff
@JLuboff
May 23 2018 15:12
:wave:
OGTechnoBoy
@OGTechnoBoy
May 23 2018 15:56
Hi!
How are you guys?
I'm new here.
Keggatron
@Keggatron
May 23 2018 15:56

Hi guys, I'm having some issues with MongoDB. Anyway when I put this in:

Appointment.create(
    {
      date: date,
      "10:30": [{'booked': true}, {'booker': 'John Doe'}, {'phone':'5555555'}]
    }
  )

this returns

"date" : "20180510", "10:30" : [ { "_id" : ObjectId("5b057824d5eee629f0dfa763"), "booked" : true }, { "_id" : ObjectId("5b057824d5eee629f0dfa762") }, { "_id" : ObjectId("5b057824d5eee629f0dfa761") } ]

Why am I not getting my booker and phone entries?

OGTechnoBoy
@OGTechnoBoy
May 23 2018 15:57
Ah, I see.
This is not for chatting between developers.
It's for help.
Jason Luboff
@JLuboff
May 23 2018 15:58
@Keggatron I'[m assuming you're using mongoose?
@OGTechnoBoy It is mainly for help, but lots of other chat occurs as well :D
OGTechnoBoy
@OGTechnoBoy
May 23 2018 15:58
Oh, I see.
Keggatron
@Keggatron
May 23 2018 16:01
yeah
@JLuboff
OGTechnoBoy
@OGTechnoBoy
May 23 2018 16:02
It might be a weird question but is there any arabic people here?
Jason Luboff
@JLuboff
May 23 2018 16:03
@Keggatron I don't really have much experience with mongoose (I'm a vanilla mongo kind of guy) but what does your schema look like?
Keggatron
@Keggatron
May 23 2018 16:04
const appointmentSchema = new Schema ({
  date: String,
  "8:00": [{booked: Boolean}, {booker: String}, {phone: String}],
  "8:30": [{booked: Boolean}, {booker: String}, {phone: String}],
  "9:00": [{booked: Boolean}, {booker: String}, {phone: String}],
  "9:30": [{booked: Boolean}, {booker: String}, {phone: String}],
  "10:00": [{booked: Boolean}, {booker: String}, {phone: String}],
  "10:30": [{booked: Boolean}, {booker: String}, {phone: String}],
  "11:00": [{booked: Boolean}, {booker: String}, {phone: String}],
  "11:30": [{booked: Boolean}, {booker: String}, {phone: String}],
  "12:00": [{booked: Boolean}, {booker: String}, {phone: String}],
  "12:30": [{booked: Boolean}, {booker: String}, {phone: String}],
  "13:00": [{booked: Boolean}, {booker: String}, {phone: String}],
  "13:30": [{booked: Boolean}, {booker: String}, {phone: String}],
  "14:00": [{booked: Boolean}, {booker: String}, {phone: String}],
  "14:30": [{booked: Boolean}, {booker: String}, {phone: String}],
  "15:00": [{booked: Boolean}, {booker: String}, {phone: String}],
  "15:30": [{booked: Boolean}, {booker: String}, {phone: String}],
  "16:00": [{booked: Boolean}, {booker: String}, {phone: String}],
  "16:30": [{booked: Boolean}, {booker: String}, {phone: String}]
})
Jason Luboff
@JLuboff
May 23 2018 16:05
@Keggatron Hmmm.. and what if you query your data directly from the mongo shell?
Keggatron
@Keggatron
May 23 2018 16:15
I'm unable to
it's odd
here is the result of db.appointments.find()
{ "_id" : ObjectId("5b057824d5eee629f0dfa760"), "date" : "20180510", "10:30" : [ { "_id" : ObjectId("5b057824d5eee629f0dfa763"), "booked" : true }, { "_id" : ObjectId("5b057824d5eee629f0dfa762") }, { "_id" : ObjectId("5b057824d5eee629f0dfa761") } ], "8:00" : [ ], "8:30" : [ ], "9:00" : [ ], "9:30" : [ ], "10:00" : [ ], "11:00" : [ ], "11:30" : [ ], "12:00" : [ ], "12:30" : [ ], "13:00" : [ ], "13:30" : [ ], "14:00" : [ ], "14:30" : [ ], "15:00" : [ ], "15:30" : [ ], "16:00" : [ ], "16:30" : [ ], "__v" : 0 }
but when I use the find call, and plug in the objectIds, the only thing that returns anything of value is the first objectId. The other three return nothing
ArchivalBoat50
@ArchivalBoat50
May 23 2018 16:22
Hey guys I am the doing the Mutations algorithm and I'm having difficulty trying to figure out how I can iterate through each letter of the two words in the array and compare them to each other. Here is my code so far:
function mutation(arr) {
  arr = arr.join(" ").toLowerCase().split(" ");
  if (arr[1].indexOf(arr[0]) === -1) {
      return false;
  }
  else {
    return true;
  }

}


mutation(["Alien", "line"]);
Bjorn van de Peut
@bjorno43
May 23 2018 16:26
@ArchivalBoat50 Can you please link the challenge with your question? There are many challenges on FCC, it's hard to remember the exact requirements for each of them
ArchivalBoat50
@ArchivalBoat50
May 23 2018 16:28
Nate Mallison
@NJM8
May 23 2018 16:31
@ArchivalBoat50 You don't want to join them, you want to check if arr[0] contains all the letters of arr[1], so loop through arr[1], seeing if each letter exists in arr[0], if they all exist return true, if not return false
ArchivalBoat50
@ArchivalBoat50
May 23 2018 16:40
@NJM8 How would you compare them
Bjorn van de Peut
@bjorno43
May 23 2018 17:04
@ArchivalBoat50 This is how I solved it. Explanation in comments:
function mutation(arr) {
  // Turn both strings into lower case
  arr = arr.join(" ").toLowerCase().split(" ");

  // Split second array on each character
  var letters = arr[1].split('');

  // Loop through each character
  for(var i=0; i<letters.length; i++){
    // Verify if character exists in first string
    if(arr[0].indexOf(letters[i]) === -1){
      // Character not found, return false
      return false;
    }
  }

  // All found, return true
  return true;
}

mutation(["hello", "hey"]);
Nate Mallison
@NJM8
May 23 2018 17:07
@ArchivalBoat50 I did much the same, loop through arr[1] and use indexOf to see if arr[0] contains the letter, you can also convert arr[0] to an array and use arr.includes(letter) to see if it contains it
ArchivalBoat50
@ArchivalBoat50
May 23 2018 17:11
@bjorno43 @NJM8 Thanks for your help I figured it out
CamperBot
@camperbot
May 23 2018 17:11
archivalboat50 sends brownie points to @bjorno43 and @njm8 :sparkles: :thumbsup: :sparkles:
:cookie: 449 | @njm8 |http://www.freecodecamp.org/njm8
:cookie: 303 | @bjorno43 |http://www.freecodecamp.org/bjorno43
Gulsvi
@gulsvi
May 23 2018 18:59
Fun, easy problem :) Find the index of the lowest number in a sorted array. For example:
[15, 16, 23, 42, 4, 8]
circularly sorted, I guess I should say
Moisés Man
@moigithub
May 23 2018 19:11
let arr=[15, 16, 23, 42, 4, 8];
let min = Math.min(...arr);
let lowestIndex = arr.indexOf(min); //// <-- result
richRocksCodeBlocks
@richRocksCodeBlocks
May 23 2018 19:12

@ArchivalBoat50 Here was my solution. I spent a lot of time on this challenge. ```function mutation(arr) {
// make each lowercase
var first = arr[0].toLowerCase();
var second = arr[1].toLowerCase();

    // loop through each letter in the first array
    for (var i = 0; i < first.length; i++){
        //compare the selected letter in the fist array against each letter in the second array
        for (var j = 0; j < second.length; j++){
            // if the selected letter from the first array is not found in the second array return false
            if (first.indexOf(second[j]) === -1){
                return false;
            } 

        }
    }

    return true;
}
mutation(["hello", "hey"]);

/
this was by far the most dificult so far.
/```

function mutation(arr) {
        // make each lowercase 
        var first = arr[0].toLowerCase();
        var second = arr[1].toLowerCase();

        // loop through each letter in the first array
        for (var i = 0; i < first.length; i++){
            //compare the selected letter in the fist array against each letter in the second array
            for (var j = 0; j < second.length; j++){
                // if the selected letter from the first array is not found in the second array return false
                if (first.indexOf(second[j]) === -1){
                    return false;
                } 

            }
        }

        return true;
    }
    mutation(["hello", "hey"]);

/** 
this was by far the most dificult so far.
**/
i messed up on the first attempt to post code. please ignore the first markdown box.
Nate Mallison
@NJM8
May 23 2018 19:15

@gulsvi @moigithub

let arr=[15, 16, 23, 42, 4, 8];
let lowestIndex = arr.indexOf(Math.min(...arr));

:smile:

Gulsvi
@gulsvi
May 23 2018 19:19
@moigithub @NJM8 Now try to do it without visiting each element in the array
O(log(n))
Brad
@bradtaniguchi
May 23 2018 19:22
@gulsvi I mean you'd only skip the last guy right in some special case right? I don't think its possible to say search only "half" of the array or something
Gulsvi
@gulsvi
May 23 2018 19:22
Okay, a different test case: [15, 16, 23, 24, 25, 26, 27, 4, 8, 12, 13, 14];
But, yeah searching half the array is the right thought @bradtaniguchi
Moisés Man
@moigithub
May 23 2018 19:31
soo the array values will always be "sorted" like that ?
Gulsvi
@gulsvi
May 23 2018 19:32
Yes, always sorted ascending
Brad
@bradtaniguchi
May 23 2018 19:49
I don't wanna keep killing the fun, but that test case isn't sorting ascending haha, unless there's another special rule with how the array would come haha
Gulsvi
@gulsvi
May 23 2018 19:57
@bradtaniguchi sorted ascending like that :)
I think it's called a sorted circular array
Brad
@bradtaniguchi
May 23 2018 19:57
Ah
Gulsvi
@gulsvi
May 23 2018 20:01
Not to spoil the fun either, but I gotta run. Here's the answer: https://s.codepen.io/anon/pen/pVmjZL?editors=0012
One of those times when lots of lines of codes can be quicker
Leena
@leena-d
May 23 2018 20:29
can someone explain the solution of the "Counting Cards" javascript challenge? I got the solution, I'm just not sure why/how it works. i haven't done any coding in quite some time
Aditya
@ezioda004
May 23 2018 20:42
@leena-d You need to write a function which will have a parameter card and also a global variable say count. When the values passed in the function (given in table), the variable count should change accordingly. So say you have var count = 0; initially, if you invoke the function with 2 as argument cc(2); then according to the table provided, the count should increment by 1. Likewise do it for other values given in the table. Now finally at the end of the function, you need to return count + either "Bet" or "Hold" based on if count is positive or negative.
Henry
@GitHub-Henry
May 23 2018 20:42
@leena-d this will help you visualize your solution
Leena
@leena-d
May 23 2018 20:44
@GitHub-Henry @ezioda004 thank you
CamperBot
@camperbot
May 23 2018 20:44
leena-d sends brownie points to @github-henry and @ezioda004 :sparkles: :thumbsup: :sparkles:
:cookie: 556 | @github-henry |http://www.freecodecamp.org/github-henry
:cookie: 565 | @ezioda004 |http://www.freecodecamp.org/ezioda004
Nate Mallison
@NJM8
May 23 2018 21:20
@gulsvi Now do Dijkstra in JS :smile:
Gulsvi
@gulsvi
May 23 2018 21:39
@NJM8 DFS or BFS? :)
I actually have that coming up - have had to figure out minimum distance between two nodes of a binary search tree, graphs are next. This is when I wish there was a queue or stack class built in to javascript
Brad
@bradtaniguchi
May 23 2018 21:44
I mean, array got all that you need tho right?
Gulsvi
@gulsvi
May 23 2018 21:48
Unshifting and shifting from an array is linear. A good queue lets you enqueue/dequeue in constant time
To do it right in JS, we have to build a linkedlist
A stack though, I think you're right - that's just a pop() and push() which is all constant
Brad
@bradtaniguchi
May 23 2018 21:56
What about shift/push tho?
Gulsvi
@gulsvi
May 23 2018 22:10
.shift() and .unshift() are O(n) because all the indexes need to be recalculated
.push() and .pop() are O(1) because only the last index changes
I think .slice() may give slightly better performance compared to shift/unshift, but it seems a linked list is the ideal way to do it
Brad
@bradtaniguchi
May 23 2018 22:15
Wow so internally re-calculating all the indexes interesting
Bjorn van de Peut
@bjorno43
May 23 2018 22:36
Thought this was interesting (messing around with canvas and simulating object movement): https://codepen.io/bjorno43/pen/bMyppJ
Brad
@bradtaniguchi
May 23 2018 22:37
@bjorno43 pretty cool stuff there
Bjorn van de Peut
@bjorno43
May 23 2018 22:40
@bradtaniguchi Thanks. Someone in CodeReview was asking about it. After explaining that you can't "move" objects in canvas as it's essentially just a blank paper where you draw upon, I started messing around with it. Obviously nothing actually moves. Instead it erases the canvas on each mousemove event and re-draws it using the cursor's location
CamperBot
@camperbot
May 23 2018 22:40
bjorno43 sends brownie points to @bradtaniguchi :sparkles: :thumbsup: :sparkles:
:cookie: 488 | @bradtaniguchi |http://www.freecodecamp.org/bradtaniguchi
Nate Mallison
@NJM8
May 23 2018 22:52
@gulsvi both. Then one with a simple heuristic to check if you are going the right direction. I did it with a stack.
@gulsvi you do any C programming?