Get help on our basic JavaScript and Algorithms Challenges. If you are posting code that is large use Gist - https://gist.github.com/ paste the link here.
i need help with this challenge. I keep getting the original collection in return
function whatIsInAName(collection, source){
// What's in a name?
var arr = [];
// Only change code below this line
for(var key in source){
for(var x = 0; x < collection.length; x++){
if(collection[x].hasOwnProperty(key)){
arr.push(collection[x]);
}
}
}
// Only change code above this line
return arr;
}
what could be the problem?
for(var x = 0; x < collection.length; x++){
for(var key in source){
if(collection[x].hasOwnProperty(key)){
arr.push(collection[x]);
}
}
}
for each value in collection
keep that value if for every key in source
the value has the key and the property values match
var all = true;
for (var key in source) {
if key not in collection[x] or values don't match
all = false
}
if (all) push value
@sjames1958gm
for(var key in source){
for(var x = 0; x < collection.length; x++){
if( key !== collection[x]){
all = false;
}else{
arr.push(collection[x]);
}
}
}
got an empty array instead....i'm surely doing something the wrong way
for(var x = 0; x < collection.length; x++){
var all = true;
for(var i = 0; i < source.length; i++){
if( source[i] !== collection[x]){
all = false;
}else{
arr.push(collection[x]);
}
}
}
collection[x]
is the looped object and source[i]
is the looped source key.
!==
.
source[i] !== collection[x].hasOwnProperty()
obj.hasOwnProperty(key)
. What is the object and what is the key here?
collection[x]
is the object and source[i]
is the key.
source
an object, and you need to iterate its properties?
source[i]
isn't the key even then. Some source_props[i]
, with var source_props = Object.keys(source);
at the beginning would.
Object.keys(source)
, and not over source which is not an array.
for (key in source){...}
then key would be the looped source key. That is another way.
filter
and every
instead of for
loops. Many ways, you just have to pick one.
collection[x][key]!==source[key]
||
)
collection
, you test it for all of source
's properties.
padunk sends brownie points to @sjames1958gm :sparkles: :thumbsup: :sparkles:
:star2: 9235 | @sjames1958gm |http://www.freecodecamp.org/sjames1958gm
items
by date/name ? Found few articles but nothing that really works..
items
contain names/prices/etc
so have not started but just trying to understand the logic. its kind of online shopping website that has the options
to put them in those orders. Hope it makes sence
.sort()
with either name or date but you need to change date into a more useable format like UNIX time
moment.js
to convert a date into unix (or it may have been just regular ms, can't remember) and did a comparison in my sort
.sort()
an actual callback as let arr = [10, 5, 1, 20];
console.log(arr.sort()); // result: [1, 10, 20, 5]
let arr = [10, 5, 1, 20];
console.log(arr.sort((a,b) => a > b)); // result: [1, 5, 10, 20]
let arr = [10, 5, 1, 20];
let swap = true;
while (swap) {
swap = false;
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
[arr[i], arr[i + 1]] = [arr[i + 1], arr[i]];
swap = true;
}
}
}
console.log(arr);
let arr = [10, 5, 1, 20];
let swap = true;
let len = arr.length
while (swap) {
len--;
swap = false;
for (let i = 0; i < len; i++) {
if (arr[i] > arr[i + 1]) {
[arr[i], arr[i + 1]] = [arr[i + 1], arr[i]];
swap = true;
}
}
}
console.log(arr);
.sort()
, but I personally liked heap sort for no other reason than I studied it the most. But if you asked me to make it right now, Id be screwed
tiagocorreiaalmeida sends brownie points to @megma_jj_twitter :sparkles: :thumbsup: :sparkles:
Good evening,
I am trying to push all the items of the sorted 2d-array to arr1 if there are none of the same items in arr1.
The problem is that I get this weird result, when logging to the console, where "Hair-Pin" gets added but "Bowling Ball" doesn't. Can someone explain to me what's happening here?
for (let k = 0; k < arr1.length; k++){
for (let l = 0; l < sorted.length; l++){
if (arr1[k][1].includes(sorted[l][1]) == false){
arr1.push(sorted[l]);
}
}
break;
}
console.log(arr1[1].includes(sorted[2][1]));
bradley1492 sends brownie points to @tiagocorreiaalmeida :sparkles: :thumbsup: :sparkles:
:cookie: 525 | @tiagocorreiaalmeida |http://www.freecodecamp.org/tiagocorreiaalmeida
no reason...