Barny
1
Hi! I need help, I’m new to js and have to do this task. I’m confused what I’m doing wrong…
var products = [
(product = {
id: 1,
name: "green",
}),
(product = {
id: 2,
name: "red",
}),
(product = {
id: 3,
name: "black",
}),
];
var myWishlist = [
(wishlist = {
id: 1,
productId: 4,
}),
(wishlist = {
id: 2,
productId: 2,
}),
(wishlist = {
id: 3,
productId: 2,
}),
];
for (var i = 0; i < myWishlist.length; ++i) {
var currentWishlistProductId = myWishlist[i].productId;
for (var j = 0; j < products.length; ++j) {
var currentProductId = products[i].id;
if (currentWishlistProductId == currentProductId) {
console.log(
"Whishlist id: " +
myWishlist[i].id +
"Product name: " +
products[i].name,
);
}
}
}
// Now output is: Whishlist id: 2 Product name: red
// Output should look like this:
// Whishlist id: 2 Product name: red
// Whishlist id: 3 Product name: red
nibble
2
Hi there welcome to FCC. Your code would be more readable if you formatted it properly. Kindly edit it.
ILM
3
what should your code do?
just giving a piece of code like that, at most we can point out syntax errors.
if you want help with what your code should do you need to explain what it should do, and what it does instead
nibble
4
I am not sure about the purpose of your code but by looking at the output try the code below and compare with yours:
var products = [
{
id: 1,
name: "green",
},
{
id: 2,
name: "red",
},
{
id: 3,
name: "black",
},
];
var myWishlist = [
{
id: 1,
productId: 4,
},
{
id: 2,
productId: 2,
},
{
id: 3,
productId: 2,
},
];
for (var i = 0; i < myWishlist.length; ++i) {
var currentWishlistProductId = myWishlist[i].productId;
for (var j = 0; j < products.length; ++j) {
var currentProductId = products[j].id;
if (currentWishlistProductId == currentProductId) {
console.log(
"Whishlist id: " +
myWishlist[i].id +
"Product name: " +
products[j].name,
);
}
}
}
/* Expected output
Whishlist id: 2Product name: red
Whishlist id: 3Product name: red
*/