## Where communities thrive

• Join over 1.5M+ people
• Join over 100K+ communities
• Free without limits
##### Activity
• Aug 17 09:32
• Jun 20 21:19
@bjorno43 banned @shenerd140
• Apr 25 17:13
@mstellaluna banned @cmal
• Jan 08 22:05
@mstellaluna banned @dertiuss323
• Nov 07 2018 04:42
User @texas2010 unbanned @ows-ali
• Nov 02 2018 16:25
@texas2010 banned @ows-ali
• Oct 12 2018 05:50
@bjorno43 banned @NACH74
• Oct 05 2018 23:02
@mstellaluna banned @JomoPipi
• Sep 16 2018 12:21
@bjorno43 banned @yash-kedia
• Sep 16 2018 12:16
@bjorno43 banned @vnikifirov
• Sep 05 2018 08:12
User @bjorno43 unbanned @androuino
• Sep 05 2018 07:38
@bjorno43 banned @androuino
• Aug 23 2018 16:57
User @bjorno43 unbanned @rahuldkjain
• Aug 23 2018 16:23
@bjorno43 banned @rahuldkjain
• Jul 29 2018 14:13
User @bjorno43 unbanned @jkyereh
• Jul 29 2018 01:00
@bjorno43 banned @jkyereh
• Jul 10 2018 22:09
@bjorno43 banned @manafn
• Jul 06 2018 15:20
@texas2010 banned @imlegend19
• Jul 03 2018 12:28
@bjorno43 banned @vbvmatta
• Jun 29 2018 13:54
@bjorno43 banned @OGTechnoBoy
Stephen James
@sjames1958gm
@raynor84 What code do you have?
steve84
@raynor84
don't have any I'm stuck on how to approach the algorithm
Fernando
@lestairon
I guess the best option now is make 2 if statements that check if the temperature is being displayed in ºC or ºF, right?
Stephen James
@sjames1958gm
@raynor84
Maybe start with trying to find all the values in arr1 not in arr2, using the suggested links.
@lestairon You would have to do that anyway so that you calculated the correct temperature.
Stephen James
@sjames1958gm
@lestairon I would create a variable
var isCelsius = true;
Then use that in your click handler
if (isCelsius) {
// Change the display to F
isCelsius = false;
}
else
{
// Change the display to C
isCelsius = true;
}
steve84
@raynor84
still testing it though....

function diffArray(arr1, arr2) {
var newArr = [];
arr1.forEach(function(array, index) {
if(array[index]==arr2[index]) {
newArr = arr1.slice(index-1,index);
console.log(arr1.slice(index-1, index));
}
});

// Same, same; but different.
return newArr;
}

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

Stephen James
@sjames1958gm
@raynor84 The function called by forEach is not called with an array it is called with a value from the array
arr1.forEach(function(value, index) {})
So you should be testing if value is in arr2 (irrespective of index) (consider indexOf function
steve84
@raynor84
'''
CamperBot
@camperbot
:bulb: to format code use backticks!  more info
steve84
@raynor84

'''

function diffArray(arr1, arr2) {
var newArr = [];
arr1.forEach(function(value, index) {
if(value==arr2[index]) {
newArr = arr1.slice(arr1.indexOf(value),arr1.indexOf(value) - 1);
}
});

// Same, same; but different.
return newArr;
}

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

CamperBot
@camperbot
:bulb: to format code use backticks!  more info
steve84
@raynor84

function diffArray(arr1, arr2) {
var newArr = [];
arr1.forEach(function(value, index) {
if(value==arr2[index]) {
newArr = arr1.slice(arr1.indexOf(value),arr1.indexOf(value) - 1);
}
});

// Same, same; but different.
return newArr;
}

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
Stephen James
@sjames1958gm
@raynor84 Doesn't that just compare value to one value in arr2? You are resetting newArr with each iteration.
Consider arr2.indexOf(value) and how the value from this might be useful
steve84
@raynor84
the index should be index++ next iteration
Juli Cheng
@julicheng
hi guys would really appreciate if someone could help with my question. so im looking at a js file and this is at the top
import \$ from 'jquery' import TweenMax from 'gsap'
what does it mean and is it vanilla js? i know it says its importing jquery and tweenmax but ive never seen this syntax before. thanks!
Fernando
@lestairon
That looks like python lol, but yeah, i've never seen that syntax in js
Stephen James
@sjames1958gm
@julicheng
It is the new module syntax
@julicheng It doesn't have a lot of browser support yet, so you will see that code converted by tools like babel on the server before being
sent to the browser
@raynor84 I am going to say you don't really need the index at all, if a value from arr1 is not in arr2, simply add the value to newArr
Fernando
@lestairon
@sjames1958gm Why arent we using Ecmascript 6 instead of Js?
steve84
@raynor84
'''
CamperBot
@camperbot
:bulb: to format code use backticks!  more info
Stephen James
@sjames1958gm
@lestairon ecmascript is the specification, javascript is the implementation of ecmascript
Andrew Marushko
@andrewmarushko

Hi all.
I have array of objects for example

var someArray = [{name: ‘one'}, {name: ’two'}, {name: ’three’}]

how i can add for example field id: 1 for each of objects in array
output must be something like this

var someArray = [{name: ‘one’, id: 1}, {name: ’two’, id: 2}, {name: ’three’, id: 3}]

steve84
@raynor84

function diffArray(arr1, arr2) {
var newArr = [];
arr1.forEach(function(value, index) {
if(value!=arr2[index]) {
newArr.push(arr2[index]);
}
});

// Same, same; but different.
return newArr;
}

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
Fernando
@lestairon
Yeah, but i mean the newer vesion
Stephen James
@sjames1958gm
@lestairon If your browser supports it you can. Most modern ones support a lot of ES6
Fernando
@lestairon
Oh
I was curious, because most sites teach ES5.1 instead ES6
Stephen James
@sjames1958gm
@raynor84 That is closer, but you are still only comparing value to one element of arr2 and not searching all of arr2,
Then you would push value not arr2[index]
@lestairon I am seeing most of the newer courses on sites like Udemy using more of ES6
@andrewmarushko I would use array.map for that
@andrewmarushko
someArray.map((element, index) => {
element.id = index + 1;
return element;
}
Juli Cheng
@julicheng
hi @sjames1958gm thank you for your reply! what is a module ? is it an external file ? thanks
CamperBot
@camperbot
julicheng sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 8894 | @sjames1958gm |http://www.freecodecamp.org/sjames1958gm
Fernando
@lestairon
I guess ES6 doesn't have that many libraries tho
Stephen James
@sjames1958gm
@julicheng Yes, a module is another file.
steve84
@raynor84
this works:
´´´

function diffArray(arr1, arr2) {
var newArr = [];
arr1.forEach(function(value, index) {
if(arr2.indexOf(value)==-1) {
newArr.push(value);
}
});
arr2.forEach(function(value, index) {
if(arr1.indexOf(value)==-1) {
newArr.push(value);
}
});

// Same, same; but different.
return newArr;
}

diffArray(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]);
Stephen James
@sjames1958gm
@andrewmarushko You could even use the spread syntax
someArray.map((element, index) => ({...element, id: index+1})
@raynor84 :+1:
steve84
@raynor84
thx
Markus Kiili
@Masd925
@raynor84 You have essentially the same code there twice, so you could refactor that functionality into a function that you use twice.
Stephen James
@sjames1958gm
@raynor84 Your code in forEach is basically what filter does for you
this is the same using filter
newArr = arr1.filter(function(value) { return arr2.indexOf(value)==-1})
newArr.concat(arr2.filter(function(value) return arr1.indexOf(value) == -1})`
steve84
@raynor84
@james need to check it again... it didn't work