These are chat archives for FreeCodeCamp/HelpJavaScript

27th
May 2018
Nate Mallison
@NJM8
May 27 2018 00:02
@ezioda004 I guess it's semantics at some point but I wouldn't consider JS OOP. It allows you to program in the style of OOP but few languages actually enforce the point of OOP. It doesn't really matter. I don't see why object wouldn't be able to access its own prototype. Isn't that the point of the prototype inheritance model?
ArchivalBoat50
@ArchivalBoat50
May 27 2018 00:48
I am doing the Where do I Belong algorithm challenge but I can't figure out why my loop is an infinite loop. Anyone mind explaining?
function getIndexToIns(arr, num) {
  arr = arr.sort();
  for (var i=0; i<arr.length; i++) {
    if (arr[i] > num) {
      arr.splice(i, 0, num);
    }
  }
  return arr;
}

getIndexToIns([40, 60], 50);
Mois├ęs Man
@moigithub
May 27 2018 01:26
cuz... u adding more values using splice @ArchivalBoat50
anthonygallina1
@anthonygallina1
May 27 2018 01:49
umm would it allways be larger than zero?
num
ArchivalBoat50
@ArchivalBoat50
May 27 2018 02:25

Hey guys, I don't understand why this code:

arr = [5,3,20,3];
arr = arr.sort();
console.log(arr);

Is returning this:

[ 20, 3, 3, 5 ]
Why is it returning, that? How do I get it to return the numbers in order?
Stephen James
@sjames1958gm
May 27 2018 02:31
@ArchivalBoat50 You have to provide a function to sort to sort numbers, the default is to convert values to strings and compare
arr.sort((a, b) => a-b);
ArchivalBoat50
@ArchivalBoat50
May 27 2018 02:38
@sjames1958gm I didn't quite catch what you said there.
abraham anak agung
@padunk
May 27 2018 02:55
@ArchivalBoat50 you have to provide a function to sort, either ascending or descending.
abraham anak agung
@padunk
May 27 2018 04:03
Sharing this link about JS and data structure. Check it out
Angela Sirls
@annDAcoder_gitlab
May 27 2018 04:10
I got the switch statement to run. I'm attaching, hopfully it will be helpful to someone. Thanks to everyone that responded
image.png
Shubham soni
@shubhamsoni2617
May 27 2018 11:45

Objective is to check if the number is square of any number, I did the the below code:

var isSquare = function(n){
if(n===0 || n===1){
return true;
}

for(var i=2;i<=n/2;i++){
if(n/i===i){
return true;
}
}

return false;

}

I am unable to understand the code within the brackets: Can anyone please elaborate return Math.sqrt(n) % 1 === 0;

"(n) % 1 === 0" What it is actually doing here?

Stephen James
@sjames1958gm
May 27 2018 11:48
@shubhamsoni2617 I think that code is testing if (n) is an integer
1.5 % 1
0.5
1 % 1
0
Shubham soni
@shubhamsoni2617
May 27 2018 12:02
@sjames1958gm Though, I have a got your point that what is happening here but out of curiosity doesn't it happen the way we do in Math as something divided by 1 always gives remainder 0 as we kinda ignore 1 from the denominator?
Stephen James
@sjames1958gm
May 27 2018 12:27
@shubhamsoni2617 Generally when you do that you are only dividing integers, not floating point
Shubham soni
@shubhamsoni2617
May 27 2018 12:30
@sjames1958gm Thanks a lot.
CamperBot
@camperbot
May 27 2018 12:30
shubhamsoni2617 sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 9258 | @sjames1958gm |http://www.freecodecamp.org/sjames1958gm
Stephen James
@sjames1958gm
May 27 2018 12:30
@shubhamsoni2617 :+1:
ArchivalBoat50
@ArchivalBoat50
May 27 2018 17:52

I am doing the Caesars Cipher Algorithm but I don't understand why my function is returning '`'. Here is my code:

function rot13(str) {
  for (i=0; i<str.length; i++) {
    var num = str.charCodeAt(i);
    if (num >= 110) {
      num -= 13;
      str = String.fromCharCode(num);
    }
    else {
      num += 13;
      str = String.fromCharCode(num);
    }
    return str;
  } 

}

rot13("SERR PBQR PNZC");

I don't see how it can return '~' unless it adds 13 to the decimal version of q. Anyone mind explaining?

Ion Varsescu
@Nei-V
May 27 2018 17:54
hello, I found this on MDN
function clean () { clearInterval(nIntervId); bTyping = false; bStart = true; oCurrent = null; aSheets.length = nIdx = 0; }
what does the last statement do? (aSheets.length = nIdx = 0;)
Pieter Stokkink
@forkerino
May 27 2018 18:36
@ArchivalBoat50
'S'.charCodeAt(0) // 83
String.fromCharCode(83 + 13)  // "`"
your return should also probably be outside the loop, and it would be wise to not change str inside it, as that is what you are looping over.
Where did you get 110?
ah, that's lower case n, the input is all uppercase
@Nei-V It assigns 0 to both aSheets.length and nIdx
Gulsvi
@gulsvi
May 27 2018 18:52
@ndburrus :point_up: The link you gave looks a bit cryptic. What is it for?
Here it is for reference - not really comfortable clicking on that. https://1drv.ms/u/s!ApKCx7vmc_mAhzTXFbIR7YYi0DjP
Ion Varsescu
@Nei-V
May 27 2018 19:13
thanks @ArchivalBoat50 , is this the same as "aSheets.length,nIdx = 0; "?
CamperBot
@camperbot
May 27 2018 19:13
nei-v sends brownie points to @archivalboat50 :sparkles: :thumbsup: :sparkles:
:cookie: 270 | @archivalboat50 |http://www.freecodecamp.org/archivalboat50
Aditya
@ezioda004
May 27 2018 19:20
@forkerino Thanks for the link, it makes more sense now!
@NJM8 The object isnt able to access its own prototype as far as I know. Any instances from the class/constructor is able to access it so bascally public. Thats why static properties/methods are different than prototype.
function Person() {
}
Person.prototype.greet = function() {console.log('hello');} // accessed by all instances and not the class itself
Person.sayName = function() {
    alert("I am a Person object ;)");  
}; //accessed by only class
Person.greet // undefined
Person.sayName // f()
CamperBot
@camperbot
May 27 2018 19:20
ezioda004 sends brownie points to @forkerino and @njm8 :sparkles: :thumbsup: :sparkles:
:cookie: 451 | @njm8 |http://www.freecodecamp.org/njm8
:star2: 2999 | @forkerino |http://www.freecodecamp.org/forkerino
Pieter Stokkink
@forkerino
May 27 2018 19:26
@ezioda004 that last comment gave me an idea...
Object instanceof Object  // true
Function instanceof Function // true
Jefferson
@jeffersonnnn
May 27 2018 19:31
So i am seeking a variety of opinions. What do you guys think about freelancing on Toptal vs getting a remote job. What do y'all think about their recruitment process
ArchivalBoat50
@ArchivalBoat50
May 27 2018 22:15
Hey guys I am doing Caesars Cipher and everything seems to be going right? However I can't figure how to get change every letter of the string. Anyone mind helping?
function rot13(str) {
  for (i=0; i<str.length; i++) {
    var num = str.charCodeAt(i);
    if (num >= 78) {
      num -= 13;
      str = String.fromCharCode(num);
    }
    else {
      num += 13;
      str = String.fromCharCode(num);
    }
  } 
  return str;
}

rot13("SERR PBQR PNZC");
Aditya
@ezioda004
May 27 2018 22:21
@forkerino Thats trippy, I wonder why..
@ArchivalBoat50 You're modifying the parameter passed str, you should initialize an empty string and concatenate the results from if, else and finally return that string.
function rot13(str){
  let result = "";
  for (){
    if (){
      result += String.fromCharCode(num);
    }
    else {
      result += ...
     }
   }
return result;
}
ArchivalBoat50
@ArchivalBoat50
May 27 2018 22:43

@ezioda004 Thanks that makes sense.
But now im struggling to keep the spaces.

function rot13(str) {
  let result =  "";
  for (i=0; i<str.length; i++) {
    var num = str.charCodeAt(i);
    if (num >= 78) {
      num -= 13;
      result += String.fromCharCode(num);
    }
    if (num === 32) {
      num += 0;
      result += String.fromCharCode(num);
    }
    else {
      num += 13;
      result += String.fromCharCode(num);
    }
  } 
  return result;
}

rot13("SERR PBQR PNZC");

I'm trying to have it return the spaces too.

CamperBot
@camperbot
May 27 2018 22:43
archivalboat50 sends brownie points to @ezioda004 :sparkles: :thumbsup: :sparkles:
:cookie: 569 | @ezioda004 |http://www.freecodecamp.org/ezioda004
Aditya
@ezioda004
May 27 2018 23:01
@ArchivalBoat50 You can instead have the conditions only check for alphabets. In this case they lie between [65 90] so that way you wont have to worry about non-alpha characters.
ArchivalBoat50
@ArchivalBoat50
May 27 2018 23:10

@ezioda004 It's still giving me "FREE-CODE-CAMP" instead of "FREE CODE CAMP

function rot13(str) {
  let result =  "";
  for (i=0; i<str.length; i++) {
    var num = str.charCodeAt(i);
    if (65 <= num <= 90) {
      if (num >= 78) {
        num -= 13;
        result += String.fromCharCode(num);
      }
      else {
       num += 13;
        result += String.fromCharCode(num);
     }
    }
  } 
  return result;
}

rot13("SERR PBQR PNZC");

I don't quite get why the dashes are there

Aditya
@ezioda004
May 27 2018 23:13
@ArchivalBoat50 Can you guess what the result of 1 < 2 < 3would be?
and 3 > 2 > 1
ArchivalBoat50
@ArchivalBoat50
May 27 2018 23:15
@ezioda004 1 is the lowest number
Aditya
@ezioda004
May 27 2018 23:18
Try both in console, its the same reason why 65 <= num <= 90 doesnt work. The condition starts checking for left to right. So 65 <= num returns true and then true <= 90 which will always return true because true is implicitly coerced to 1 so 1 <= 90 will return true.
You can use use logical operator AND && to rectify this.
65 <= num && num <= 90 would work. Remember you're skipping through the characters so you'd have to add them to the result somehow.
ArchivalBoat50
@ArchivalBoat50
May 27 2018 23:22
@ezioda004 Oh I get it now
But now it's returning ' '
Aditya
@ezioda004
May 27 2018 23:24
Seems to be returning the string for me.
function rot13(str) {
  let result =  "";
  for (i=0; i<str.length; i++) {
    var num = str.charCodeAt(i);
    if (65 <= num && num <= 90) {
      if (num >= 78) {
        num -= 13;
        result += String.fromCharCode(num);
      }
      else {
       num += 13;
        result += String.fromCharCode(num);
     }
    }
  } 
  return result;
}
rot13("SERR PBQR PNZC"); // "FREECODECAMP"
ArchivalBoat50
@ArchivalBoat50
May 27 2018 23:27
function rot13(str) {
  let result =  "";
  for (i=0; i<str.length; i++) {
    if (65 <= num && num < 90) {
      var num = str.charCodeAt(i);
      if (num >= 78) {
        num -= 13;
        result += String.fromCharCode(num);
      }
      else {
        num += 13;
        result += String.fromCharCode(num);
      }
    }
  } 
  return result;
}

rot13("SERR PBQR PNZC");
Oh i put declared num inside the if statement. How does that make the difference of returning ' ' ?
Oh i see
nvm
ArchivalBoat50
@ArchivalBoat50
May 27 2018 23:35
@ezioda004 The spaces are removed now though.
Aditya
@ezioda004
May 27 2018 23:48
@ArchivalBoat50 Yes, but you can add them simply by having an else statement and concatenating str[i] to result
ArchivalBoat50
@ArchivalBoat50
May 27 2018 23:56
@ezioda004 OK finally got it now. Thanks for all your help!
CamperBot
@camperbot
May 27 2018 23:56
archivalboat50 sends brownie points to @ezioda004 :sparkles: :thumbsup: :sparkles:
:cookie: 570 | @ezioda004 |http://www.freecodecamp.org/ezioda004