17th
Feb 2018
Thomas Faller
@thomasfaller
Feb 17 2018 10:17
Hey guys,
I’m working on algorithms and creating a sorting function that sorts an array. It’s working great but I was wondering if there was a way to improve and reduce it with ES6 descructuring assignments such as [arr[i]] = [arr[i-1] to avoid the whole “saving that into a temp variable before reassignment.
What do you guys think?
const sortElements = (arr) => {
let Num = arr.length;
while ( Num !== 0 ) {
let newNum = 0;
for ( let i = 1; i < arr.length; i++ ) {
if ( arr[i] < arr[i - 1] ) {
let savedValue = arr[i];
arr[i] = arr[i - 1];
arr[i - 1] = savedValue;
newNum = i;
}
}
Num = newNum;
}
return arr;
}
The idea would be to reduce that part…
let savedValue = arr[i];
arr[i] = arr[i - 1];
arr[i - 1] = savedValue;
Thomas Faller
@thomasfaller
Feb 17 2018 10:26
I think I found it
const arr = [1,2,3,4];

const [a,b] = [arr[1],arr[0]];
[arr[0],arr[1]] = [a,b];

console.log(arr) //  -> [2, 1, 3, 4]
well I was hoping something like 1 line but 2 is better than 3 :D
Tai Jones
@taiJones00
Feb 17 2018 10:38
So here's the problem on mobile
The elements with the class of hideme aren't becoming opaque
It's like \$(window).scroll( function(){ isn't being registered on mobile
Tai Jones
@taiJones00
Feb 17 2018 10:46
Okay it looks like the text is now showing up on mobile but it's not animating like it is on desktop
Feb 17 2018 11:48
anyone done or doing nighlife cordination backend project?
anyone done or doing nighlife cordination backend project?
Tiago Correia
@tiagocorreiaalmeida
Feb 17 2018 16:21
Quantomistro3178
@Quantomistro3178
Feb 17 2018 16:57
@taiJones00 Is that information on that codepen true?
If so, wow, i didnt know quincy could speak chinese
Pieter Stokkink
@forkerino
Feb 17 2018 18:08
@thomasfaller you can do it like so:
let a = [1,2,3,4]
[a[1], a[0]] = [a[0], a[1]]
a // [2, 1, 3, 4]
little late to the party, but you might enjoy it anyway
Tiago Correia
@tiagocorreiaalmeida
Feb 17 2018 20:58
anyone around?
AbrisM
@AbrisM
Feb 17 2018 21:33
Hello
Amit Patel
@AmitP88
Feb 17 2018 21:40

hey guys, I'm stuck on the Diff Two Arrays int. algorithm and I need some help understanding why my solution doesn't work:

function diffArray(arr1, arr2) {
var newArr = [];
// Same, same; but different.

// merge arr1 and arr2 into one array (contains duplicates) & sort (this will put duplicates next to each other)
var duplicateArr = arr1.concat(arr2).sort();

// iterate through duplicateArr and filter duplicate PAIRS
for(var i = 0; i < duplicateArr.length; i++) {

// if the current index is equal to the next index, slice out both current and next index
if(duplicateArr.indexOf(i) === duplicateArr.indexOf(i++)){
duplicateArr.slice(duplicateArr.indexOf(i));
duplicateArr.slice(duplicateArr.indexOf(i++));
}

}

return duplicateArr;
}

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);

The output I get is:

[1,1,2,2,3,3,4,5,5]
Feb 17 2018 21:42
@AmitP88 your slice calls aren't finished, slice takes two arguments, the first is the index, the second is the length to remove. In this case (and most cases) its just 1
Amit Patel
@AmitP88
Feb 17 2018 21:43
@bradtaniguchi ahhhh I see. ok, let me fix it real quick
Feb 17 2018 21:43
Now idk if that will fix the program, but it should help haha
Moisés Man
@moigithub
Feb 17 2018 21:44
what u trying to do with indexOf ? @AmitP88
Amit Patel
@AmitP88
Feb 17 2018 21:45
@moigithub I'm trying to compare the indexOf the current element with the next one to see if they match (I wrote comments in my code explaining what I'm trying to do)
@ezioda004
Feb 17 2018 21:47
@bradtaniguchi .slice() second argument is optional (till which length the slice needs to be performed), it assumes the length of the array if you dont give one, I think you're referring to splice() :P
Feb 17 2018 21:47
@AmitP88 the other thing Id be careful of is when you slice, your array gets shorter. But your duplicateArr.length doesn't work correctly
Amit Patel
@AmitP88
Feb 17 2018 21:47
hmmmm
Feb 17 2018 21:47
@ezioda004 Yes, I use '1' so much I forgot the original specification
Amit Patel
@AmitP88
Feb 17 2018 21:47
this is making me pull my hair out lol
Moisés Man
@moigithub
Feb 17 2018 21:49
when use indexOf usually u try to find an X element inside the array...
ie: [9,4,6,1].indexOf(4) will return 1 <-- second position (starting on 0)
but on ur code.. instead of 4 (the element) u using an index (position)
probably easier if use a string example??
ie ["a", "b", "c"].indexOf("b") <-- will return 1
soo u pass the "element" inside the parenthesis
on ur code.. u passing i (which represent an index, not the real element)
(should be duplicateArr[i] instead)
Amit Patel
@AmitP88
Feb 17 2018 21:52

@moigithub hmmm I see. I mean, I also tried doing this:

for(var i = 0; i < duplicateArr.length; i++) {

// if the current index is equal to the next index, slice out both current and next index
if(duplicateArr[i] === duplicateArr[i++]){
duplicateArr.slice(i, i++);
}

}

return duplicateArr;

but I still get the same result

maybe I need to start all over and try a different strategy?
Moisés Man
@moigithub
Feb 17 2018 21:56

a quick test on console..

a=[1,2,3]
a.slice(0,1)
// shows [1]
a
//shows (3) [1, 2, 3]

soo slice dont mutate the original array

Feb 17 2018 21:56
@AmitP88 I just noticed the i++ increase stuff to much
@moigithub wut
Oh crap its splice, not slice they are totally different
Amit Patel
@AmitP88
Feb 17 2018 21:57
@moigithub hmmmm, would .delete() work?
Gersho
@Gersho
Feb 17 2018 21:57
The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). The original array will not be modified.
@ezioda004
Feb 17 2018 21:58
@AmitP88 Maybe you should look into filter() method, as its suggested in the challenge too.
Amit Patel
@AmitP88
Feb 17 2018 21:58
@ezioda004 yeah, I was thinking about that too
Feb 17 2018 21:58
@AmitP88 Don't use delete on arrays, and yes I agree with aditya, taking a different approach would be the best at this point
Amit Patel
@AmitP88
Feb 17 2018 21:59
ok, I think what I'm going to do is reset and try again. only this time i'm going to map out my strategy in more detail
@bradtaniguchi thanks for the tip
CamperBot
@camperbot
Feb 17 2018 21:59
@ezioda004
Feb 17 2018 21:59
Yeah, delete operator should be used carefully, it leaves undefined in its awakening
CamperBot
@camperbot
Feb 17 2018 21:59
amitp88 sends brownie points to @bradtaniguchi :sparkles: :thumbsup: :sparkles:
Gersho
@Gersho
Feb 17 2018 21:59
write some pseudo code first !
@ezioda004
Feb 17 2018 21:59
Wow I made that sound epic
Amit Patel
@AmitP88
Feb 17 2018 21:59
@Gersho will do
Gersho
@Gersho
Feb 17 2018 21:59
awakening sounded a bit over the top yeah xD
Amit Patel
@AmitP88
Feb 17 2018 22:00
@ezioda004 @Gersho @moigithub thanks guys for your help, I really appreciate it! :)
CamperBot
@camperbot
Feb 17 2018 22:00
amitp88 sends brownie points to @ezioda004 and @gersho and @moigithub :sparkles: :thumbsup: :sparkles:
:cookie: 462 | @ezioda004 |http://www.freecodecamp.org/ezioda004
:cookie: 601 | @gersho |http://www.freecodecamp.org/gersho
:star2: 3731 | @moigithub |http://www.freecodecamp.org/moigithub
Feb 17 2018 22:00
haha awakening XD
Gersho
@Gersho
Feb 17 2018 22:01
only copy pasted stuff from zeal by hey free cookies !
Amit Patel
@AmitP88
Feb 17 2018 22:01
lol
alright, I g2get back to it. :)
Gersho
@Gersho
Feb 17 2018 22:02
i'm waiting for my code to be done running so i checked on the chat :p
Feb 17 2018 22:02
@AmitP88 Goodluck :D