These are chat archives for FreeCodeCamp/HelpJavaScript

10th
Apr 2018
Nate Mallison
@NJM8
Apr 10 2018 01:45
Anyone on? Need a hand, setInterval driving me up the wall
Christopher McCormack
@cmccormack
Apr 10 2018 01:45
yup
Nate Mallison
@NJM8
Apr 10 2018 01:45
  function simonsTurn(){
    changeDisplayMessage('Simon\'s turn');
    simonSaying = true;
    if (!dontIncreasePlays) {
      const nextPlay = Math.floor(Math.random() * 4) + 1;
      simonPlays.push(nextPlay);
    }
    setTimeout(() => {
      simon = setInterval(simonSays, 1000);
    }, 500);
  }

  function simonSays(){
    simulateInput(simonPlays[count]);
    console.log(count);
    if (count === simonPlays.length) {
      simonSaying = false;
      dontIncreasePlays = false;
      count = 0;
      changeDisplayMessage('Your turn');
      clearInterval(simon);
      return;
    } 
    count++;
  }
it's not exiting the function when count === simonplays.length
but it's not running either, I'm getting an error from simulateInput every second, but no console.log(count)
Christopher McCormack
@cmccormack
Apr 10 2018 01:46
you probably want to clear interval
Nate Mallison
@NJM8
Apr 10 2018 01:46
it's in there
Christopher McCormack
@cmccormack
Apr 10 2018 01:46
clearInterval(simon)
ah
didn't see, did you make simon global?
Nate Mallison
@NJM8
Apr 10 2018 01:47
Screen Shot 2018-04-09 at 9.47.09 PM.png
I did
Christopher McCormack
@cmccormack
Apr 10 2018 01:48
what line is that referring to
Nate Mallison
@NJM8
Apr 10 2018 01:48
and I tried putting all the code that simulateInput calls into simonSays so there are no outside function calls, no change
  function simulateInput(id){
    let element = document.querySelector(`[data-id="${id}"`);
    let mousedown = new Event('mousedown');
    element.dispatchEvent(mousedown);
  }
element.dispatch event, it's just because it's being passed undefined from the setInterval
All that does is add the click styling, if I bypass that and add it manually it still fails
Christopher McCormack
@cmccormack
Apr 10 2018 01:50
Why don't you just run the function that mousedown is bound to
instead of simulating a click?
Nate Mallison
@NJM8
Apr 10 2018 01:50
it doesn't matter, it isn't what is wrong with it
Christopher McCormack
@cmccormack
Apr 10 2018 01:50
Can't really see all your code to help, you're just adding more details that are confusing out of context
Christopher McCormack
@cmccormack
Apr 10 2018 01:53
https://codepen.io/cmccormack/pen/geqMzP
Stephen James
@sjames1958gm
Apr 10 2018 01:54
@NJM8 Are you missing a ] in your selector?
Christopher McCormack
@cmccormack
Apr 10 2018 01:54
(`[data-id="${id}"`);
Nate Mallison
@NJM8
Apr 10 2018 01:55
huh, I am, but it still works the first time?
still doesn't work
Christopher McCormack
@cmccormack
Apr 10 2018 01:56
it's calling that function with a 0 at some point
which doesn't exist
Nate Mallison
@NJM8
Apr 10 2018 01:57
yeah, it seems like conditional to cancel the setInterval is not running but it should
Stephen James
@sjames1958gm
Apr 10 2018 01:58
@NJM8 With that querySelector error the condition is never hit
Christopher McCormack
@cmccormack
Apr 10 2018 01:59
and the querySelector is failing because count can sometimes be 0 even though the data-id are only 1-4
@NJM8 I would suggest trying to limit your use of globals, this is one of the reasons why
you have a mutable global variable being changed and used in several different places, very difficult to troubleshoot
Nate Mallison
@NJM8
Apr 10 2018 02:01
ahhh, duh. Just move the call to simulateInput after the if statement
globals are life
Thanks @cmccormack and @sjames1958gm
CamperBot
@camperbot
Apr 10 2018 02:02
njm8 sends brownie points to @cmccormack and @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 1402 | @cmccormack |http://www.freecodecamp.org/cmccormack
:star2: 9186 | @sjames1958gm |http://www.freecodecamp.org/sjames1958gm
Christopher McCormack
@cmccormack
Apr 10 2018 02:03
I challenge you to treat every variable as a const - you will come out of that exercise learning something new I promise!
Lance
@texas2010
Apr 10 2018 02:04
can we use let? :P
Christopher McCormack
@cmccormack
Apr 10 2018 02:04
nope!
Lance
@texas2010
Apr 10 2018 02:04
:O
Christopher McCormack
@cmccormack
Apr 10 2018 02:04
let is just a crutch
think about where you might use let
and I'll show you how not to use a for loop :)
Lance
@texas2010
Apr 10 2018 02:05
i know that. what if i need to use let for reassign. or else.
Christopher McCormack
@cmccormack
Apr 10 2018 02:05
That's the whole point!
Jacob French
@Wingcraftian
Apr 10 2018 02:05
Hey can anyone help me with the syntax on how to add a new obj with 3 parameters to a constructor function using a prototype method?
Christopher McCormack
@cmccormack
Apr 10 2018 02:05
don't reassign
@Wingcraftian 3 parameters?
or properties?
Jacob French
@Wingcraftian
Apr 10 2018 02:06
parameters
Lance
@texas2010
Apr 10 2018 02:06
@cmccormack okay.
Nate Mallison
@NJM8
Apr 10 2018 02:06
@cmccormack I know globals are "bad" but I don't really see an easier way to control the state of a simple game like simon says. I mean, do I really need to construct a simon class with all it's own methods and states? it's the same thing just all bound to one instance of a class.
Christopher McCormack
@cmccormack
Apr 10 2018 02:07
@texas2010 are you blowing me off or taking the challenge? :D
Lance
@texas2010
Apr 10 2018 02:07
nooo
i would accept that. for sure. but not today
Nate Mallison
@NJM8
Apr 10 2018 02:07
games have to have states, playing, not playing, who's turn it is
Christopher McCormack
@cmccormack
Apr 10 2018 02:07
@NJM8 a lot of learning how to not use globals comes from learning concepts of functional programming
@NJM8 it's a bit of a shift in thinking but it is doable
Lance
@texas2010
Apr 10 2018 02:08
@cmccormack other day, i found out about for of . I was like what?!
Christopher McCormack
@cmccormack
Apr 10 2018 02:08
are you also aware of for...in?
Lance
@texas2010
Apr 10 2018 02:09
yeah barely use it
Christopher McCormack
@cmccormack
Apr 10 2018 02:09
yeah it's not that useful lol
Lance
@texas2010
Apr 10 2018 02:09
often time, i use for loop and array map.
what about while loop? that need to have let. i think so
Nate Mallison
@NJM8
Apr 10 2018 02:10
for...in is for objects though, no?
Lance
@texas2010
Apr 10 2018 02:10
for increment
Christopher McCormack
@cmccormack
Apr 10 2018 02:12
correct for of is for iterables
for in is for objects
Jacob French
@Wingcraftian
Apr 10 2018 02:12
@cmccormack I know you're still helping this guy but just checking do you know how? I'm good with waiting just double checking.
Christopher McCormack
@cmccormack
Apr 10 2018 02:15
@Wingcraftian yeah sorry I wasn't aware the key value combo was referred to as parameters
Jacob French
@Wingcraftian
Apr 10 2018 02:16
Well it takes in 3 parameters the method
Christopher McCormack
@cmccormack
Apr 10 2018 02:16
oh this is for a constructor
Jacob French
@Wingcraftian
Apr 10 2018 02:16
user.prototype.name = function (id,title,rating){}
example
yeah
Christopher McCormack
@cmccormack
Apr 10 2018 02:17

@Wingcraftian

Hey can anyone help me with the syntax on how to add a new obj with 3 parameters to a constructor function using a prototype method?

sorry keep having to scroll up it's a pain
Jacob French
@Wingcraftian
Apr 10 2018 02:17
all good.
Christopher McCormack
@cmccormack
Apr 10 2018 02:17
@Wingcraftian do you have a link to the problem?
Lance
@texas2010
Apr 10 2018 02:19
@cmccormack somehow i love to use Object destructuring. it is so awesome!
Christopher McCormack
@cmccormack
Apr 10 2018 02:20
@texas2010 it is! I've gotten to the point I don't like to use anything but objects as arguments so I can deconstruct them later :)
In React it's destructuring all over the place
Jacob French
@Wingcraftian
Apr 10 2018 02:21
I was writing out the instructions but I noticed some wording I didn't notice before so I'ma try a few more things real quick but thank you @cmccormack , I probably will still need you lol.
CamperBot
@camperbot
Apr 10 2018 02:21
wingcraftian sends brownie points to @cmccormack :sparkles: :thumbsup: :sparkles:
:star2: 1403 | @cmccormack |http://www.freecodecamp.org/cmccormack
Christopher McCormack
@cmccormack
Apr 10 2018 02:22
@Wingcraftian np sorry the wording is a bit confusing so I wanted to see if there was context I was missing
Lance
@texas2010
Apr 10 2018 02:24
yeah haha. so we dont have to use Dot and bracket notation.
Brad
@bradtaniguchi
Apr 10 2018 04:20
Anyone use TDD? if so, is it reasonable to test if a function exists on my class?
Christopher McCormack
@cmccormack
Apr 10 2018 04:21
Doesn't TDD imply you would already have the test before you wrote the class?
You would want to test the function anyhow and if you're accessing it as a member of the class you should be good
Brad
@bradtaniguchi
Apr 10 2018 04:31
Okey, so the fact I'm testing the functions actual use is good enough. I mean if the function doesn't exist, then the test will fail
Christopher McCormack
@cmccormack
Apr 10 2018 04:31
provided the function is only attached to that class it should fail, right
Brad
@bradtaniguchi
Apr 10 2018 04:32
yea it would fail
Avinash Chaudhary
@imperishableavinash
Apr 10 2018 08:30
https://codepen.io/imperishableavinash/live/pLGrex Can some one tell me why input not toggling After clicking on fa icon plus The code clicking fa is on last line of js file
Ayush Bahuguna
@relentless-coder
Apr 10 2018 08:35
where do you guys store the jwt token on the client side? Localstorage, Session storage, or in app data store?
or something else?
Conor Murphy
@cmurphy580
Apr 10 2018 08:36
Does anyone have experience with Firebase
Ayush Bahuguna
@relentless-coder
Apr 10 2018 08:37
server side?
Conor Murphy
@cmurphy580
Apr 10 2018 08:38
@imperishableavinash are you targeting the input or the value inside of it?
not sure what youre trying to do
Avinash Chaudhary
@imperishableavinash
Apr 10 2018 08:40
I am targeting input
I want that when I click plus it fade out and if click again on plus it appears
Marat
@Marat89bluolyu
Apr 10 2018 10:41
hi all, how to get whitespace('_') near word? I need filter words. For example: in my input I typed ayload, but I need get payload. I want to slice from founded word and I wont get full this word (not a part)
Marianissimus
@Marianissimus
Apr 10 2018 10:43
@Marat89bluolyu do you really want a whitespace (' ') or an underscore ('_') ?
Marat
@Marat89bluolyu
Apr 10 2018 10:46
@Marianissimus whitespace(' ')
I need to find first whitespce in text near with searchword
Marianissimus
@Marianissimus
Apr 10 2018 10:48
do you have a link to the challenge / can you paste the instructions here?
Marat
@Marat89bluolyu
Apr 10 2018 10:49
getDescription = (key: string, searchString: string) => { const MAX_DESCRIPTION_LENGTH = 60; if (key.indexOf(searchString) > MAX_DESCRIPTION_LENGTH / 2) { return ( '...' + key .slice( key.indexOf(searchString)/6, key.indexOf(searchString) + MAX_DESCRIPTION_LENGTH ) .slice(key.indexOf(' ') , key.indexOf(searchString)) ); } else { return key.slice(0, MAX_DESCRIPTION_LENGTH); } };
Marianissimus
@Marianissimus
Apr 10 2018 10:49
anyway, you can do it with regex - this is clearly the recommended way; however, since I don't know regex so well, I can do it in another way
oh, not the code, the problem itself
Marat
@Marat89bluolyu
Apr 10 2018 10:51
I haven't link
Marianissimus
@Marianissimus
Apr 10 2018 10:51
so you have a function, say splitwords(keyword) that should return the text before / after the first space before / after the keyword?
tell me exactly what you need to do
Marat
@Marat89bluolyu
Apr 10 2018 10:54
this function filter string, if I write word this function remove words from begin to search word, if I wrote part of this word I need to get full this word and string after search word
because I try search first whitespcace near word
maybe I need split string?
Marianissimus
@Marianissimus
Apr 10 2018 10:57
I dont really understand it; let's try again: function filterstring(str, searchword){}, where, if you find the searchword inside the string, split the string and return only the text before the searchword or after it?
yes, you can split the string at searchword, for instance
Marat
@Marat89bluolyu
Apr 10 2018 11:00
first word will be full search word and text after it
Marianissimus
@Marianissimus
Apr 10 2018 11:02
here, you can try something like:
function splitText(str, searchword){
 return str.split(searchword)[1];
}
this says: split the string at searchword and return the text after the split (after the searchword)
Marat
@Marat89bluolyu
Apr 10 2018 11:12
if i type "pay" i get 'load' but not 'payload'
key.split(searchString)[1] this my func
Marianissimus
@Marianissimus
Apr 10 2018 11:13
why don't you paste here the text, the keyword, and the output that you want; i still don't know exactly what you want
Marat
@Marat89bluolyu
Apr 10 2018 11:16
@Marianissimus text in sting: why don't you paste here the text, the keyword, and the output that you want
In search i wrote: keyword
Marianissimus
@Marianissimus
Apr 10 2018 11:17
are you kidding me?
Marat
@Marat89bluolyu
Apr 10 2018 11:17
and i wont to get: keyword, and the output that you want
@Marianissimus no man, sorry for my English, i just wont make this task =)
Marianissimus
@Marianissimus
Apr 10 2018 11:20
try this:
function splitText(str, searchword){
 return searchword + str.split(searchword)[1];
}
Marat
@Marat89bluolyu
Apr 10 2018 11:20
now if i wrote key i getting word, and the output that you want
@Marianissimus thank you! its works!
CamperBot
@camperbot
Apr 10 2018 11:23
marat89bluolyu sends brownie points to @marianissimus :sparkles: :thumbsup: :sparkles:
:cookie: 488 | @marianissimus |http://www.freecodecamp.org/marianissimus
Marat
@Marat89bluolyu
Apr 10 2018 11:25
realy complete this task with slice?
just intresting
Marat
@Marat89bluolyu
Apr 10 2018 11:31
if i write eyword i need to get keyword, and the output that you want
now i got eyword, and the output that you want
Marat
@Marat89bluolyu
Apr 10 2018 11:51
can you help?
Elia
@elia-russad
Apr 10 2018 12:23
you will have to use regexp to find the whole word first. something like /([^\s]*searchword)/g
@Marat89bluolyu
Leigh Hobson
@leighhobson89
Apr 10 2018 12:30

hi guys can anyone help me with something? I have an if statement that never seems to be triggering. I'll paste the code around it, and include console output to show my variables are set right for it to trigger:

  var denominations = [
  { name: 'ONE HUNDRED', value: 100.00},
  { name: 'TWENTY', value: 20.00},
  { name: 'TEN', value: 10.00},
  { name: 'FIVE', value: 5.00},
  { name: 'ONE', value: 1.00},
  { name: 'QUARTER', value: 0.25},
  { name: 'DIME', value: 0.10},
  { name: 'NICKEL', value: 0.05},
  { name: 'PENNY', value: 0.01}
];

...

  for (var i=0;i<cid.length;i++) {
    notesGiven = 0;
        if (numberOfDenominations[i] > 0 && denominations[i].value < changeDue) {
          cashInTill -= denominations[i].value;
          numberOfDenominations[i]--;
          changeDue -= denominations[i].value;
          notesGiven++;
        }
      else {
        break;
      }
    if (notesGiven > 0) { //if we have used this denomination
      returnArray[i][0] = denominations[i].name;
      returnArray[i][1] = (denominations[i].value * notesGiven);
    }
  }

as you can see this loop is designed to iterate through the object and check a few things. the if statement if (numberOfDenominations[i] > 0 && denominations[i].value < changeDue) always executes the else in every iteration. Some console output is below:

image.png
i should mention that cid is an array with the same number of elements (each with an array of two elements) as the object denominations
Markus Kiili
@Masd925
Apr 10 2018 12:33
@leighhobson89 If you use float values for storing the money amounts, you are certain to face float precision problems with comparisons.
Leigh Hobson
@leighhobson89
Apr 10 2018 12:34
ok thats worth knowing, but its not going to matter in the case of this, as at least one of them should trigger surely
i would iron out the creases afterwards, but something is stopping it triggering at all? no?
i had wondered if there was something syntactically wrong with it - i tried sperating out the statement in to two if's and its the second half that isnt firing. is there anything wrong with it
?
Marat
@Marat89bluolyu
Apr 10 2018 12:36
@elia-russad with witch method I should use regexp?
Elia
@elia-russad
Apr 10 2018 12:38
@Marat89bluolyu str.match(regexp)
or search()
and you should use regexp constructor to put your variable in regexp
Leigh Hobson
@leighhobson89
Apr 10 2018 12:44
anyone?
Nate Mallison
@NJM8
Apr 10 2018 12:49
@leighhobson89 Can you post all the code? Or put it in a pastebin with a link?
Hey all, can I get some reviews of my Simon game? Thanks! It is currently set to only go 5 rounds so it's easy to test the ending functions. https://www.natethedev.com/FreeCodeCampFrontEndProjects/simonSays/
Leigh Hobson
@leighhobson89
Apr 10 2018 12:52
ok @NJM8
its not expected to work yet, but this paticular issue is bugging me
function checkCashRegister(price, cash, cid) {

var returnArray = []; //array to send back

function sumInTill(sum) { //add up the register contents
  var newArr = [];
  for (var i=0;i<sum.length;i++) {
  newArr.push(sum[i][1]); 
  }  
  return newArr.reduce(function(a,b) { return a+b; }).toFixed(2);
}

function amountOfEachCoinOrBill(arr,denominations) { //see how many of each denomiation we have
  var newArr = [];
  for (var i=0;i<arr.length;i++) {
    var result = arr[i][1] / denominations[i].value;
    result = result.toFixed();
    newArr.push(result);/////
    newArr[i] = parseFloat(newArr[i]);
  }
  return newArr;
}
 // -------------------------------------------------------------------
  cid.reverse(); //reverse array to make it easier to match up with object
  //console.log(cid.length);
  var denominations = [
  { name: 'ONE HUNDRED', value: 100.00},
  { name: 'TWENTY', value: 20.00},
  { name: 'TEN', value: 10.00},
  { name: 'FIVE', value: 5.00},
  { name: 'ONE', value: 1.00},
  { name: 'QUARTER', value: 0.25},
  { name: 'DIME', value: 0.10},
  { name: 'NICKEL', value: 0.05},
  { name: 'PENNY', value: 0.01}
];
  //numbers to work with
  var changeDue = cash-price;
  console.log("Change Due: " + changeDue);
  var numberOfDenominations = amountOfEachCoinOrBill(cid,denominations);
  var cashInTill = parseFloat(sumInTill(cid));
  var notesGiven; //counter for working out change
  console.log("#denoms : " + numberOfDenominations);
  for (var i=0;i<cid.length;i++) {
    notesGiven = 0;
        if (numberOfDenominations[i] > 0 && denominations[i].value < changeDue) {
          cashInTill -= denominations[i].value;
          numberOfDenominations[i]--;
          changeDue -= denominations[i].value;
          notesGiven++;
        }
      else {
        break;
      }
    if (notesGiven > 0) { //if we have used this denomination
      returnArray[i][0] = denominations[i].name;
      returnArray[i][1] = (denominations[i].value * notesGiven);
    }
  }

  if (changeDue > cashInTill) { //gets the insufficient funds part out the way
    return "Insufficient Funds";
  }
  else if (changeDue === cashInTill) {
    return "Closed";
  }
  else {
    //console.log(returnArray);
    return returnArray;
  }
}

checkCashRegister(19.50, 20.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]);
Nate Mallison
@NJM8
Apr 10 2018 13:00
so first I would make sure you format your code, so it is easy to read and reason about, small thing that helps
    if (numberOfDenominations[i] > 0 && denominations[i].value < changeDue) {
      cashInTill -= denominations[i].value;
      numberOfDenominations[i]--;
      changeDue -= denominations[i].value;
      notesGiven++;
      console.log(notesGiven);
    } else {
      break;
    }
Leigh Hobson
@leighhobson89
Apr 10 2018 13:01
ok
Nate Mallison
@NJM8
Apr 10 2018 13:01
console.log(notesGiven); never runs, I suspect this is because you are getting some small rounding errors when doing math on floats, so this if statement always fails, which means it always breaks.
so the next one, if (notesGiven > 0) will never run because the loop always exits
maybe you want continue there?
Leigh Hobson
@leighhobson89
Apr 10 2018 13:03
ok, so i should multiply them by 100 and then divide by 100 after doing the math so its performed on an integer?
Nate Mallison
@NJM8
Apr 10 2018 13:03
yes
start there. My solution to this did all kinds of weird stuff until I fixed that, fixed like 4 bugs. haha
Leigh Hobson
@leighhobson89
Apr 10 2018 13:04
continue should be there, apologies, i had a nested loop there originally and that is from then, i will change that part, and try messing with the numbers
does it look like im sort of on the right track though in terms of the idea and the logic?
Nate Mallison
@NJM8
Apr 10 2018 13:07
Yes. Maybe doing a little more work than is needed but that is ok. :smile:
Make it work your way then refactor
Leigh Hobson
@leighhobson89
Apr 10 2018 14:01
cheers
NikolaNbgd
@NikolaNbgd
Apr 10 2018 14:21
Hello folks. I have a form and I want to save my data in google sheets on submit. Form is integrated, I'm receiving data on email, but google sheets are empty. Does anyone know how can I do this? Thanks in advance.
Marianissimus
@Marianissimus
Apr 10 2018 14:29
I suppose you can export your data in JSON; all you need to do then is follow this tutorial: https://medium.com/@paulgambill/how-to-import-json-data-into-google-spreadsheets-in-less-than-5-minutes-a3fede1a014a
However, I dont know how you could do that automatically - say, use a setInterval function that would refresh your data at a given time
NikolaNbgd
@NikolaNbgd
Apr 10 2018 14:33
@Marianissimus look at this, https://github.com/dwyl/learn-to-send-email-via-google-script-html-no-server, I followed up, step by step, but at the end, sheets are still empty
Marianissimus
@Marianissimus
Apr 10 2018 14:33
Wait, there's this: https://developers.google.com/apps-script/guides/triggers/installable#time-driven_triggers. So, yeah, it can be done; Create a function that import the data, then a trigger that runs that function. here's the code from google:
 // Trigger every Monday at 09:00.
  ScriptApp.newTrigger('myFunction')
      .timeBased()
      .onWeekDay(ScriptApp.WeekDay.MONDAY)
      .atHour(9)
      .create();
}
NikolaNbgd
@NikolaNbgd
Apr 10 2018 14:37
@Marianissimus still is triggered every Monday, right? I need to be triggered every time when user contact me, different user of course
Marianissimus
@Marianissimus
Apr 10 2018 14:38
I wonder if you could skip the email step from this process; so, it would be something like: 1. on form submit, update a json file. 2. run the function and update the google sheet
Ken Haduch
@khaduch
Apr 10 2018 14:39
@NJM8 - the Simon game looks good, but the sounds are not working (other than a little "beep" the first time, at least for some of the sounds.) Are you hearing them when you play? I think that there is a problem with the sound files that they provide. Someone pointed out that there is an error when you play them and they only sound one time, and it's a chopped sound.
Marianissimus
@Marianissimus
Apr 10 2018 14:40
I am really just guessing here, but it seems there's a way for that, too: // doGet(e) runs when a user visits a web app or a program sends an HTTP GET request to a web app. //so you can run the function on GET or POST requests; you would probably use POST to get the JSON when user submits the form
NikolaNbgd
@NikolaNbgd
Apr 10 2018 14:42

@Marianissimus First step is done, I'm getting user's data on the email in sheets order, name, lastname, etc, this is my script // spit out all the keys/values from the form in HTML for email function formatMailBody(obj, order) { var result = ""; // loop over all keys in the ordered form data for (var idx in order) { var key = order[idx]; result += "<h4 style='text-transform: capitalize; margin-bottom: 0'>" + key + "</h4><div>" + sanitizeInput(obj[key]) + "</div>"; // for every key, concatenate an<h4 />/<div />pairing of the key name and its value, // and append it to theresultstring created at the start. } return result; // once the looping is done,result` will be one long string to put in the email body
}

// sanitize content from the user - trust no one
// ref: https://developers.google.com/apps-script/reference/html/html-output#appendUntrusted(String)
function sanitizeInput(rawInput) {
var placeholder = HtmlService.createHtmlOutput(" ");
placeholder.appendUntrusted(rawInput);

return placeholder.getContent();
}

function doPost(e) {

try {
Logger.log(e); // the Google Script version of console.log see: Class Logger
record_data(e);

// shorter name for form data
var mailData = e.parameters;

// names and order of form elements
var dataOrder = JSON.parse(e.parameters.formDataNameOrder);

// determine recepient of the email
// if you have your email uncommented above, it uses that `TO_ADDRESS`
// otherwise, it defaults to the email provided by the form's data attribute
var sendEmailTo = (typeof TO_ADDRESS !== "undefined") ? TO_ADDRESS : mailData.formGoogleSendEmail;

MailApp.sendEmail({
  to: String(sendEmailTo),
  subject: "Contact form submitted",
  // replyTo: String(mailData.email), // This is optional and reliant on your form actually collecting a field named `email`
  htmlBody: formatMailBody(mailData, dataOrder)
});

return ContentService    // return json success results
      .createTextOutput(
        JSON.stringify({"result":"success",
                        "data": JSON.stringify(e.parameters) }))
      .setMimeType(ContentService.MimeType.JSON);

} catch(error) { // if error return this
Logger.log(error);
return ContentService
.createTextOutput(JSON.stringify({"result":"error", "error": e}))
.setMimeType(ContentService.MimeType.JSON);
}
}

/**

  • record_data inserts the data received from the html form submission
  • e is the data received from the POST
    */
    function record_data(e) {
    Logger.log(JSON.stringify(e)); // log the POST data in case we need to debug it
    try {
    var doc = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = doc.getSheetByName('responses'); // select the 'responses' sheet by default
    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
    var nextRow = sheet.getLastRow()+1; // get next row
    var row = [ new Date() ]; // first element in the row should always be a timestamp
    // loop through the header columns
    for (var i = 1; i < headers.length; i++) { // start at 1 to avoid Timestamp column
    if(headers[i].length > 0) {
     row.push(e.parameter[headers[i]]); // add data to row
    }
    }
    sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
    }
    catch(error) {
    Logger.log(e);
    }
    finally {
    return;
    }

}`

my bad, this is script ```
var TO_ADDRESS = "suzana.tasic@ringmaster.co";

// spit out all the keys/values from the form in HTML for email
function formatMailBody(obj, order) {
var result = "";
// loop over all keys in the ordered form data
for (var idx in order) {
var key = order[idx];
result += "<h4 style='text-transform: capitalize; margin-bottom: 0'>" + key + "</h4><div>" + sanitizeInput(obj[key]) + "</div>";
// for every key, concatenate an <h4 />/<div /> pairing of the key name and its value,
// and append it to the result string created at the start.
}
return result; // once the looping is done, result will be one long string to put in the email body
}

// sanitize content from the user - trust no one
// ref: https://developers.google.com/apps-script/reference/html/html-output#appendUntrusted(String)
function sanitizeInput(rawInput) {
var placeholder = HtmlService.createHtmlOutput(" ");
placeholder.appendUntrusted(rawInput);

return placeholder.getContent();
}

function doPost(e) {

try {
Logger.log(e); // the Google Script version of console.log see: Class Logger
record_data(e);

// shorter name for form data
var mailData = e.parameters;

// names and order of form elements
var dataOrder = JSON.parse(e.parameters.formDataNameOrder);

// determine recepient of the email
// if you have your email uncommented above, it uses that `TO_ADDRESS`
// otherwise, it defaults to the email provided by the form's data attribute
var sendEmailTo = (typeof TO_ADDRESS !== "undefined") ? TO_ADDRESS : mailData.formGoogleSendEmail;

MailApp.sendEmail({
  to: String(sendEmailTo),
  subject: "Contact form submitted",
  // replyTo: String(mailData.email), // This is optional and reliant on your form actually collecting a field named `email`
  htmlBody: formatMailBody(mailData, dataOrder)
});

return ContentService    // return json success results
      .createTextOutput(
        JSON.stringify({"result":"success",
                        "data": JSON.stringify(e.parameters) }))
      .setMimeType(ContentService.MimeType.JSON);

} catch(error) { // if error return this
Logger.log(error);
return ContentService
.createTextOutput(JSON.stringify({"result":"error", "error": e}))
.setMimeType(ContentService.MimeType.JSON);
}
}

/**

  • record_data inserts the data received from the html form submission
  • e is the data received from the POST
    */
    function record_data(e) {
    Logger.log(JSON.stringify(e)); // log the POST data in case we need to debug it
    try {
    var doc = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = doc.getSheetByName('responses'); // select the 'responses' sheet by default
    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
    var nextRow = sheet.getLastRow()+1; // get next row
    var row = [ new Date() ]; // first element in the row should always be a timestamp
    // loop through the header columns
    for (var i = 1; i < headers.length; i++) { // start at 1 to avoid Timestamp column
    if(headers[i].length > 0) {
     row.push(e.parameter[headers[i]]); // add data to row
    }
    }
    sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
    }
    catch(error) {
    Logger.log(e);
    }
    finally {
    return;
    }

}```

Marianissimus
@Marianissimus
Apr 10 2018 14:46
oh, no, no back-end for me, sorry:( In this area, I can mostly theoretize; So, yeah, what I'm saying is that you could maybe try to do without the e-mail step of the process, OR to do both - the e-mail, as a back-up, and the JSON update; I think it's much easier to get the data from a JSON to the Google sheet then from an e-mail;
NikolaNbgd
@NikolaNbgd
Apr 10 2018 14:48
there is no backend, just js, I followed up this example, https://github.com/dwyl/learn-to-send-email-via-google-script-html-no-server#how
Nate Mallison
@NJM8
Apr 10 2018 15:31
thanks @khaduch I heard that, only seeing problems in Chrome, I may just find and host my own
CamperBot
@camperbot
Apr 10 2018 15:31
njm8 sends brownie points to @khaduch :sparkles: :thumbsup: :sparkles:
:star2: 3866 | @khaduch |http://www.freecodecamp.org/khaduch
Jason Luboff
@JLuboff
Apr 10 2018 15:41
:fire:
Nate Mallison
@NJM8
Apr 10 2018 15:42
:sparkles:
Jason Luboff
@JLuboff
Apr 10 2018 15:44
Whats up
Nate Mallison
@NJM8
Apr 10 2018 15:44
simon says
Christopher McCormack
@cmccormack
Apr 10 2018 15:44
@JLuboff :wave:
Nate Mallison
@NJM8
Apr 10 2018 15:45
@cmccormack I doubled my global vars count.
I set a minimum 25 per app
lol
Christopher McCormack
@cmccormack
Apr 10 2018 15:45
@NJM8 That's crazy because I just converted all of my variables to quantum variables
Now I don't even have to create them, they are either there or not, in a state of variable uncertainty
Jason Luboff
@JLuboff
Apr 10 2018 15:45
@cmccormack :wave:
@NJM8 I won after only 5???
Nate Mallison
@NJM8
Apr 10 2018 15:50
lol
it's set to 5 for testing
faster
Jason Luboff
@JLuboff
Apr 10 2018 15:51
faster...surrree
Jason Luboff
@JLuboff
Apr 10 2018 16:01
End user's can be so frustrating sometimes..
sabahatullah
@sabahatullah
Apr 10 2018 16:07
could someone tell me, in the browser console, how can I access the objects displayed under Network Responses
like for example I want to get this session Id? How would I go about doing that. which object its saved under?
Christopher McCormack
@cmccormack
Apr 10 2018 16:09
@sabahatullah which tool are you using in your request?
or is this just a page you were browsing?
sabahatullah
@sabahatullah
Apr 10 2018 16:10
yes just a page .. no tool
@cmccormack
Christopher McCormack
@cmccormack
Apr 10 2018 16:11
@sabahatullah try making a fetch request from within your javascript for the URL, and you should be able to access the headers from the response
Jason Luboff
@JLuboff
Apr 10 2018 16:15
@cmccormack What're you working on today?
Christopher McCormack
@cmccormack
Apr 10 2018 16:15
Trying to get a couple project for work back on the right track, seems nobody wants to respond to my emails :anger:
sabahatullah
@sabahatullah
Apr 10 2018 16:16
@cmccormack thats right. I get it to work that way. does it mean thats the only way we can access that information. Isn't that something stored in any object which we can straight away access through browser console?
Christopher McCormack
@cmccormack
Apr 10 2018 16:16
And then when I'm free maybe back to voting app, almost done!
Jason Luboff
@JLuboff
Apr 10 2018 16:16
I hate when peoples don't respond to emails. I have a couple individuals here who are notourious for never responding
Christopher McCormack
@cmccormack
Apr 10 2018 16:16
@sabahatullah not sure I follow what exactly you're trying to do
@JLuboff when I'm in the office i can just go to their desk and throw a nerf ball at them but I"m not there very often anymore and it's more of a struggle
alpox
@alpox
Apr 10 2018 16:17
@JLuboff I'm just waiting for a week for responses from two assistants at my university
Thats so disturbing
Jason Luboff
@JLuboff
Apr 10 2018 16:17
@cmccormack I mean...I could walk over...but.. that means I ahve to walk...
@alpox That's shitty lol
Christopher McCormack
@cmccormack
Apr 10 2018 16:18
haha
sabahatullah
@sabahatullah
Apr 10 2018 16:18
you know like when you can just access the data through console. like window.ObjectName
can we not access Network tab Response the same way?
Christopher McCormack
@cmccormack
Apr 10 2018 16:18
@sabahatullah not sure if headers are actually stored as a property of window
I'm also not certain how that would be useful
The browser is what is sending and receiving the response to the http request, the page is presenting the response
alpox
@alpox
Apr 10 2018 16:19
@sabahatullah Btw. with what code did you make the request? You can get the data where you made the request.
Ronique Ricketts
@RoniqueRicketts
Apr 10 2018 16:20
@JLuboff lazy
sabahatullah
@sabahatullah
Apr 10 2018 16:21
@cmccormack for example if that response is from analytics it contains lot of user information that can be used to personalize content.
@alpox yes, it works with get request.
Christopher McCormack
@cmccormack
Apr 10 2018 16:22
@sabahatullah why would you ever need to access it from the window object?
if you can get it from a get request why not do that?
sabahatullah
@sabahatullah
Apr 10 2018 16:22
@cmccormack because that url is dyanmic
Jason Luboff
@JLuboff
Apr 10 2018 16:23
@alpox Hey ...since you're here.. Question in regards to best practice.. I have seperated out my queries from my routes file.. but I still have a lot of logic/processing in my routes file (i.e process data to send to queries, process data returned from queries). Is that okay to keep in my routes file or should that processing be in the query files?
sabahatullah
@sabahatullah
Apr 10 2018 16:23
I guess where it stores all the information would probably bit consistent object
Jason Luboff
@JLuboff
Apr 10 2018 16:23
@RoniqueRicketts I do enough walking outside of work :P
Christopher McCormack
@cmccormack
Apr 10 2018 16:23
So how do you expect to grab this information?
Ronique Ricketts
@RoniqueRicketts
Apr 10 2018 16:25
@JLuboff lies
Ronique Ricketts
@RoniqueRicketts
Apr 10 2018 16:25
You should be happy to walk
sabahatullah
@sabahatullah
Apr 10 2018 16:25
@cmccormack Actually what I am trying to figure out is that where does the browser store that information that returns as Network Response. I am under impression its probably under some object.
alpox
@alpox
Apr 10 2018 16:27
@JLuboff I generally always recommend to keep logic where it logically belongs. So this sounds perfectly fine to me as route data processing before passing it on sound like a logical part of request/response handling. Just go sure to not do the same thing 100 times in every route.
sabahatullah
@sabahatullah
Apr 10 2018 16:27
@alpox
Thanks but I am able to make get request successfully but that is not something I can use in my web application as the URL for fetch request is dynamic and changes.
CamperBot
@camperbot
Apr 10 2018 16:27
sorry sabahatullah, you can't send brownie points to yourself! :sparkles: :sparkles:
Jason Luboff
@JLuboff
Apr 10 2018 16:28
@alpox Awesome, thanks! I'll have to review my code (probably needs some refactoring at the end) but I don't think I reuse too much besides maybe checking the user info
Christopher McCormack
@cmccormack
Apr 10 2018 16:28
@sabahatullah Ah I see. I'm not sure if it actually stores this information outside of memory.
alpox
@alpox
Apr 10 2018 16:28
@sabahatullah You maybe misunderstood. This captures all requests done from your page and you can intercept all the responses done through ajax.
Christopher McCormack
@cmccormack
Apr 10 2018 16:29
@sabahatullah can you clarify why the url is changing? Is it a redirect every time you access a specific URL?
sabahatullah
@sabahatullah
Apr 10 2018 16:40
@cmccormack yes sometimes it has some additional parameters. and also in most cases on some pages I won't know this URL before hand. so its not feasible for me to hard the URL into the ajax request
Christopher McCormack
@cmccormack
Apr 10 2018 16:41
How are you getting it in the browser then? I believe a fetch request to a URL will still go through the redirect process
Jason Luboff
@JLuboff
Apr 10 2018 19:02
@cmccormack around?
Markus Kiili
@Masd925
Apr 10 2018 19:04
@JLuboff No, but I am ready for some php questions :imp:
Jason Luboff
@JLuboff
Apr 10 2018 19:05
@Masd925 :joy: No PHP question here! I was going to ask if he had a good write up about fetch, but I just saw that fetch isn't supported in IE (unless you use polyfill?) so just going to stick to ajax
:fire: :fire:
Jason Luboff
@JLuboff
Apr 10 2018 19:07
@NJM8 Conmgrats
Markus Kiili
@Masd925
Apr 10 2018 19:07
@NJM8 :sparkles:
Jason Luboff
@JLuboff
Apr 10 2018 19:07
You're way ahead of @Masd925 now!
Markus Kiili
@Masd925
Apr 10 2018 19:07
@JLuboff I will catch him in a couple of decades.
I mean catch where he is now, to be precise.
Nate Mallison
@NJM8
Apr 10 2018 19:08
Thanks, lol, I'm pretty sure @Masd925 could do it in no time
CamperBot
@camperbot
Apr 10 2018 19:08
njm8 sends brownie points to @masd925 :sparkles: :thumbsup: :sparkles:
:star2: 4772 | @masd925 |http://www.freecodecamp.org/masd925
Nate Mallison
@NJM8
Apr 10 2018 19:08
thanks @JLuboff
CamperBot
@camperbot
Apr 10 2018 19:08
njm8 sends brownie points to @jluboff :sparkles: :thumbsup: :sparkles:
:star2: 2396 | @jluboff |http://www.freecodecamp.org/jluboff
Nate Mallison
@NJM8
Apr 10 2018 19:08
now to pick a framework and do the back end stuff
leaning towards Vue
maybe angular
Markus Kiili
@Masd925
Apr 10 2018 19:10
@NJM8 I hate authorities so much that I can't really do any fcc projects (feels like someone is telling me to do them). I have done some similar projects on my own without any problems.
Jason Luboff
@JLuboff
Apr 10 2018 19:10
@Masd925 You're not really to into front end stuff either, are you?
That is, the design side of it
Nate Mallison
@NJM8
Apr 10 2018 19:11
nice, yeah I didn't think it was because you couldn't do them. I just want to finish something. I have about 12 half finished coding books and as many unfinished tutorials.
Markus Kiili
@Masd925
Apr 10 2018 19:11
@JLuboff Yeah, maybe. More mathematical and theoretical in nature. I work with front-end so that is good way to learn something that you don't like too much.
Nate Mallison
@NJM8
Apr 10 2018 19:12
TBH the last 3 are all the same, Simon, Tic Tac Toe, Pomodoro timer, all the same just different logic
Jason Luboff
@JLuboff
Apr 10 2018 19:12
I hated Simon and Pomodoro.
Markus Kiili
@Masd925
Apr 10 2018 19:13
@NJM8 Also my girlfriend said that I cannot get any more degrees.
Nate Mallison
@NJM8
Apr 10 2018 19:13
lol
degree envy?
Jason Luboff
@JLuboff
Apr 10 2018 19:13
TTT was fun. Also I found it to be the most challenging.
The logic behind Simon was actually not that difficult. The logic behind TTT was more involved
Nate Mallison
@NJM8
Apr 10 2018 19:13
calculator was most challenging for me, all those stupid input cases
Jason Luboff
@JLuboff
Apr 10 2018 19:13
Anyhow.. lunch time which means gym time...see ya in a bit
Nate Mallison
@NJM8
Apr 10 2018 19:14
see ya
Markus Kiili
@Masd925
Apr 10 2018 19:15
@NJM8 I did an expression parser for the calculator project, but the calculator is of course pending.