These are chat archives for FreeCodeCamp/HelpJavaScript

7th
Jan 2018
aRtoo
@artoodeeto
Jan 07 2018 02:21
s
hello fam
i have this code for my pomodoro timer but at the end it shows up like this. 0:1`` not0:0```. heres the code.
var min = 1;
var inSeconds = min * 60;


var interval = setInterval(timer , 10);



function timer() {

  inSeconds--;

  if(inSeconds === 0){
    clearInterval(interval);
    console.log(sdfasd);
  } else {

     if(inSeconds % 60 >= 10) {
    console.log(Math.floor(inSeconds / 60) , inSeconds % 60);
  } else {
    console.log(Math.floor(inSeconds / 60) , inSeconds % 60);
  }

  }

}
thanks much
Ken Haduch
@khaduch
Jan 07 2018 05:34

@artoodeeto - what are you seeing? Your code posting is messed up... but the basic thing that you have to go is generate a string output, because if you print a number that has the value 0001 as a number, it will only output the 1 and drop the insignificant leading zeroes. So you want to do something like this:

if ( seconds < 10) {
    console.log(`0${seconds}`); // or console.log("0" + seconds);
} else {
    console.log(seconds);
}

so you generate a string output with the leading zero there, which does look better in a time display. I hope that is what you were looking for?

Razvan Jackson
@RazvanJackson
Jan 07 2018 08:07
Good morning!
Sweet Coding :)
@SweetCodingInc
Jan 07 2018 08:29
@artoodeeto
var min = 0.2;
var inSeconds = min * 60;

var interval = setInterval(timer, 1000);

function timer() {
  if (--inSeconds === 0) {
    clearInterval(interval);
    console.log('sdfasd');
  } else {
    const mins = Math.floor(inSeconds / 60);
    const _seconds = inSeconds % 60;
    const seconds = _seconds > 9 ? seconds : `0${_seconds}`;
    const time = `${mins} ${seconds}`;
    console.log(time);
  }
}
Onome Sotu
@onomesotu
Jan 07 2018 15:15
Does anyone know how to make those small description videos /gifs used in README's?
Daniel Romero
@Ranacode
Jan 07 2018 15:17
markdowns README from github?
Onome Sotu
@onomesotu
Jan 07 2018 15:17
@Ranacode Yes
Daniel Romero
@Ranacode
Jan 07 2018 15:18
Can you give me a README example ?
just the url to the repository
Onome Sotu
@onomesotu
Jan 07 2018 15:18
Something that repeatedly shows how a website is used
@Ranacode one second
Daniel Romero
@Ranacode
Jan 07 2018 15:20
aaaaa
I usually use the licecap software
Razvan Jackson
@RazvanJackson
Jan 07 2018 15:20
 Bet(event){
            let choosenColor = event.target.value;
            console.log(choosenColor);
            if(app.timer>=0 && !isNaN(app.amount)){
                console.log('bet works');
                myRoulette.emit('bet', {choosenColor: choosenColor, amount:app.amount});
            }
        },

<button type="submit" name="bet-option" class="button is-black" value="black" :disabled="!activeBetting"  @click="Bet($event)"><span>Black</span></button>
Daniel Romero
@Ranacode
Jan 07 2018 15:21
Razvan Jackson
@RazvanJackson
Jan 07 2018 15:21
Do you have any idee why sometimes VueJS is getting the event.value, sometimes not?
Daniel Romero
@Ranacode
Jan 07 2018 15:22
What are the moments that event.target.value is undefined?
Onome Sotu
@onomesotu
Jan 07 2018 15:23
@Ranacode thanks, i will check it out
CamperBot
@camperbot
Jan 07 2018 15:23
onomesotu sends brownie points to @ranacode :sparkles: :thumbsup: :sparkles:
:cookie: 359 | @ranacode |http://www.freecodecamp.org/ranacode
Michael Grienauer
@mgrienauer
Jan 07 2018 16:30
i'm on arguments optional and am wondering, why isn't forEach returning properly?

function addTogether() {

  //create array of args, check function and add function
  var args = Array.from(arguments);
  var check = function(arg){
    if (typeof arg !== 'number'){
      return undefined;
    }
  };
  var add = function(x,y){
    return x+y;
  };

  //check each arg for typeof == 'number'
  args.forEach(check);

  //if only 1 arg, return that arg + 2
  //else, return the two args added together
  if (args.length == 1){
    return add(2);
  }
  else{
    return add(args[0],args[1]);
  }


}

addTogether("http://bit.ly/IqT6zt");
this example returns null, but when i get rid of the last if/else statement, it returns undefined properly
any ideas?
Razvan Jackson
@RazvanJackson
Jan 07 2018 16:34
var check = function(arg){
if (typeof arg !== 'number'){
return undefined;
}
else return 'foo';
};
try this
see if returns foo
because if typeof arg === number then it does nothing
Michael Grienauer
@mgrienauer
Jan 07 2018 16:37
its still returning null
yea it seems like my check function isn't doing anyting
no worries i have another idea
RecodeExistence
@RecodeExistence
Jan 07 2018 17:58
Hi guys, would anyone be able to point me in the right direction with this challenge please?

function findLongestWord(str) {
var splitStr = str.split(" ");    // Change string to elements in array.  
  var currentLongestString;    // Variable to push longer string length to.  
  for (var firstWordIndex = 0 ; firstWordIndex < splitStr.length ; firstWordIndex++){    // First string to compare
    for (var secondWordIndex = 1 ; secondWordIndex < splitStr.length - 1 ; secondWordIndex++) {    //Second string to compare
  return (splitStr[secondWordIndex].length > splitStr[firstWordIndex].length) ?
    currentLongestString = splitStr[secondWordIndex].length :
    currentLongestString = splitStr[firstWordIndex].length;
      }
  } 

}

findLongestWord("The quick brown fox jumped over the lazy dog");
Gersho
@Gersho
Jan 07 2018 18:01
i don't think you need the second loop
var splitStr = str.split(" ");    // Change string to elements in array.  
  var currentLongestString = 0; 
for (i=0; i<splitStr.length; i++){
if( splitStr[i].length > currentLongestString){
currrentLongestString = splitStr[i].length}
}
return currentLongestString,
or smth like that
string.length
Aditya
@ezioda004
Jan 07 2018 18:18
@RecodeExistence currentLongestWord is replaced every iteration regardless if its greater or not
@RecodeExistence You can do something like this
for (var firstWordIndex = 0 ; firstWordIndex < splitStr.length ; firstWordIndex++){    // First string to compare
    for (var secondWordIndex = 1 ; secondWordIndex < splitStr.length - 1 ; secondWordIndex++) {    //Second string to compare
   splitStr[secondWordIndex].length > splitStr[firstWordIndex].length ?
    currentLongestString >  splitStr[secondWordIndex].length?
      currentLongestString = currentLongestString: currentLongestString = splitStr[secondWordIndex].length:
    currentLongestString > splitStr[firstWordIndex].length?
  currentLongestString = currentLongestString: currentLongestString = splitStr[firstWordIndex].length;
      }
  }
 return currentLongestString;
But instead of making it so complicated you can maybe just use one loop and compare as mentioned above by @Gersho
RecodeExistence
@RecodeExistence
Jan 07 2018 18:33
@ezioda004 @Gersho thanks for the help, closed this down without thinking. eventually figured out that it was replacing regardless of comparisson and was scratching my head over how to fix that lol
CamperBot
@camperbot
Jan 07 2018 18:33
recodeexistence sends brownie points to @ezioda004 and @gersho :sparkles: :thumbsup: :sparkles:
:cookie: 353 | @ezioda004 |http://www.freecodecamp.org/ezioda004
:cookie: 564 | @gersho |http://www.freecodecamp.org/gersho
RecodeExistence
@RecodeExistence
Jan 07 2018 18:41
I seem to overcomplicate things a lot in my code lol.
Eric Hasegawa
@EricHasegawa
Jan 07 2018 21:05
can anyone tell me why on my 34th line of code in the JS file it says that it cannot join the array because it's undefined? I can console.log the array and it comes out fine. But when I try to .join() it the console states that it is undefined. I have no idea why this is.
Gersho
@Gersho
Jan 07 2018 21:10
console.log(userInput) does not come out fine, it is the undefined you see in your log
  function calculate(userInput) {
    console.log("a")
    console.log(userInput)
    console.log("b")
    totalStr = userInput.join("")
    $("#display").html(eval(totalStr));
    update();
  };


////////
["", "8", "7", "-", "2"]
// press =
"a"
undefined
"b"
Eric Hasegawa
@EricHasegawa
Jan 07 2018 21:12
I figured it out with some other help, I was calling calculate without a parameter
Thanks anyway though! @Gersho
CamperBot
@camperbot
Jan 07 2018 21:13
erichasegawa sends brownie points to @gersho :sparkles: :thumbsup: :sparkles:
:cookie: 565 | @gersho |http://www.freecodecamp.org/gersho
Eric Hasegawa
@EricHasegawa
Jan 07 2018 21:13
I will probably encounter more issues before I finish the project
Gersho
@Gersho
Jan 07 2018 21:14
you'll get to the good stuff when you hear about "floating point errors"
aka: the code works, but the maths comes out wrong
Eric Hasegawa
@EricHasegawa
Jan 07 2018 21:30
Yeah I have looots of errors to figure out still
At least it sort of works!
Gersho
@Gersho
Jan 07 2018 21:31
:)
Nerando Johnson
@Nerajno
Jan 07 2018 22:40
so i need a litte help
greetings all
here is the code
// $('#myCollapsible').collapse({
//   toggle: false
// })

var channels = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", "storbeck", "habathcx",
  "RobotCaleb", "noobs2ninjas", "brunofin", "comster404", "RealKraftyy?"
];

var apiSegmentOne = "https://wind-bow.glitch.me/twitch-api/streams/";

//This helped me to run the function channel by channel.
//https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example
function individualIteration(i) {
  return function() {
    console.log(channels[i]);
  }
}
$(document).ready(function() {
  // console.log(channels.length, channels);
  for (var i = 0; i < channels.length; i++) {
    var apiSegmentTwo = apiSegmentOne + channels[i];
    $.getJSON(apiSegmentTwo, function(data) {
      var intitalData = data.stream;
      var test = data._links.channel;
      var convertedUserName = test.split("/").pop();
      //$(".userName").append(convertedUserName+" is online.");
      // get it to write to html
      $.(".card").append("<div class='card'><div class='card-header' role="tab"><h4 class='mb-0' 'channelName' userName>"+
      convertedUserName+"<h4></div></div>");
      // console.log(convertedUserName);
      if (intitalData === null) {
        console.log(convertedUserName + " is offline.");
        // NOTE:  need to add condition, if channel was never present.
      } else {
        var user = data.stream.channel.display_name;
      //$(".userName").append(user+" is offline.");

        // $('#userName')
        var info = data.stream;
        var gameName = data.stream.channel.game;
        let status = data.stream.channel.status;
        let viewers = data.stream.viewers;
        let logo = data.stream.channel.logo;
        let channelAddress = data.stream.channel.url;
        console.log(user + " is online.");
        console.log(user+"'s Info", info, gameName, status, viewers, logo,
          channelAddress);
      }
    });
  }
});

// // Trying to write a promise for this
// let testPromise =function getJSON() {
//   return new Promise(function(resolve, reject) {
//     $.ajax({
//       url:apiSegmentOne+channels[1],
//       method: 'GET'
//     }).done((response)=>{
//       ////this means my api call suceeded, so I will call resolve on the response
//       console.log(url);
//       resolve()
//     }).fail((error)=>{
//       ////this means the api call failed, so I will call reject on the error
//       reject(console.log("api not present");)
//     });
//   });
//
// }
I am trying to get it to append
Nerando Johnson
@Nerajno
Jan 07 2018 22:46
i know i have errors so point them out please
Gersho
@Gersho
Jan 07 2018 22:51
haven't gotten into the code part, but about the commented part, getJSON has built in come back so you shouldn't mix it with promise (maybe it works i don't know, but you're asking for trouble)
Stephen James
@sjames1958gm
Jan 07 2018 22:52
@Nerajno Your resolve call should be passing the data that you received.
The value you call resolve with is the value passed to the .then function callback
In my opinion your URL should come as a parameter to the function.
You have testPromise and getJSON as the same function (they both refer to the function)
Gersho
@Gersho
Jan 07 2018 22:56
i don't really get it, if you plan to use promises wouldn't it be more appropriate to use fetch ? ajax already has success/error handling
Stephen James
@sjames1958gm
Jan 07 2018 22:57
Maybe they are trying to learn about promises? If not, then fetch would be better as it is already promise based.
Eunice Park
@eunicode
Jan 07 2018 23:47

I have a quick question about dot notation and bracket notation. Why does the code using bracket notation work, but not the code using dot notation?

Code with dots (doesn't work)

let checkObj = {
  oddNum: 1,
  evenNum: 2,
  foundNum: 5,
  randomNum: 18
};

let objToArray = [];

for (const prop in checkObj) {
  if (checkObj.prop > 2) {
    objToArray.push(checkObj.prop);
  }
}

console.log(objToArray); // undefined

Code with brackets (works)

for (const prop in checkObj) {
  if (checkObj[prop] > 2) {
    objToArray.push(checkObj[prop]); 
  }
}

console.log(objToArray); // [5, 18]
Aditya
@ezioda004
Jan 07 2018 23:51
@eunipa Because bracket notation is used to access objects via variables, dot notation is only used for strings.
Gersho
@Gersho
Jan 07 2018 23:52
.prop doesn't check the variable prop, it looks in the object for a property named prop
too slow
Eunice Park
@eunicode
Jan 07 2018 23:53
@ezioda004 Thank you
CamperBot
@camperbot
Jan 07 2018 23:53
eunipa sends brownie points to @ezioda004 :sparkles: :thumbsup: :sparkles:
:cookie: 354 | @ezioda004 |http://www.freecodecamp.org/ezioda004
Eunice Park
@eunicode
Jan 07 2018 23:55
@Gersho Interesting, thanks!
CamperBot
@camperbot
Jan 07 2018 23:55
eunipa sends brownie points to @gersho :sparkles: :thumbsup: :sparkles:
:cookie: 566 | @gersho |http://www.freecodecamp.org/gersho
Aditya
@ezioda004
Jan 07 2018 23:56
@eunipa :thumbsup: If you want a quick read on which to use when, check this out
Eunice Park
@eunicode
Jan 07 2018 23:57
@ezioda004 great article, thanks for the rec :)
CamperBot
@camperbot
Jan 07 2018 23:57
eunipa sends brownie points to @ezioda004 :sparkles: :thumbsup: :sparkles:
api offline
Aditya
@ezioda004
Jan 07 2018 23:57
@eunipa Happy to help.