This community-built FAQ covers the “Product of Everything Else” code challenge in JavaScript. You can find that challenge here, or pick any challenge you like from our list.
Top Discussions on the JavaScript challenge Product of Everything Else
There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
The following works by iterating the array, making a shallow copy and then setting the current element to 1, then computing the product. No elements are ever removed or replaced in the initial array.
function productOfTheOthers(array) {
const mul = (a, b) => a * b
const arr = []
array.forEach((x, i) => {
const u = array.slice()
u[i] = 1
arr.push(u.reduce(mul))
})
return arr
}
console.log(productOfTheOthers([1, 2, 3, 4, 5]))
// Leave this so we can test your code:
module.exports = productOfTheOthers;
function productOfTheOthers(array) {
let currentIndex = 0;
let productArray = [];
while(currentIndex !== array.length) {
let productSum = 1;
let arrayOfProducts = array.filter((_, index) => index !== currentIndex);
for (let num of arrayOfProducts) productSum *= num;
productArray.push(productSum);
currentIndex += 1;
}
return productArray;
}
console.log(productOfTheOthers([1, 2, 3, 4]))
console.log(productOfTheOthers([5, 5, 5]))
// Leave this so we can test your code:
module.exports = productOfTheOthers;
This function has (I think - correct me if I am wrong) a time complexity of O(N), linear time, due to no nested loops. It calculates cumulative products in both directions for the array. Then these 2 cumulative products are used to calculate the product of missing numbers:
function productOfTheOthers(array) {
// Calculate forward and reverse cumulative products:
const fwdProd = [array[0]];
const revProd = [array[array.length - 1]];
for (let i = 1; i < array.length; i++) {
fwdProd.push(fwdProd[i - 1] * array[i]);
revProd.push(revProd[i - 1] * array[array.length - 1 - i]);
}
// Map over the array once and multiply from the cumulative product arrays:
return array.map((nr, idx) => {
const fact1 = idx >=1 ? fwdProd[idx - 1] : 1;
const fact2 = idx < revProd.length - 1 ? revProd[revProd.length - idx - 2] : 1;
return fact1 * fact2;
});
}
And that is my code:
function productOfTheOthers(array) {
// Write your code here
let result = ;
for (let i = 0; i < array.length; i++) {
let product = 1;
for (let j = 0; j < array.length; j++) {
if (i === j) continue;
else {
product *= array[j];
}
}
result.push(product);
}
return result;
}
console.log(productOfTheOthers([1, 2, 3, 4, 5]))
// Leave this so we can test your code:
module.exports = productOfTheOthers;
Probably because JS and large numbers can act a little strange sometimes. Try your code with an array such as [999999, 999999, 999999]. It will return a decimal output and not an integer. I usually try to avoid division for problems such as this one as the potential is there for a decimal output when it should be an integer.
function productOfTheOthers(array) {
let result = [];
array.forEach(function(el, index) {
let somme = 1;
array.forEach(function(elmt, ind) {
if (index !== ind) {
somme *= elmt
}
})
result.push(somme)
})
return result;
}
console.log(productOfTheOthers([5, 5, 5]))
// Leave this so we can test your code:
module.exports = productOfTheOthers;
You right. I rewrote the code and got rid of division operation. I have instead used outer and inner forEach loop and now it seems to work.
function productOfTheOthers(array) {
let result = [];
const reducer = (previousValue, currentValue) => previousValue * currentValue;
array.forEach((outVal, outInd) => {
// this line bellow may work also but does not pass the test
//result[index] = array.reduce(reducer) / value;
let product = 1; // initial value of product
array.forEach((inVal, inInd) => {
if(outInd !== inInd) {
product = product * inVal; // calculating products
}
});
result.push(product); // adding products
});
return result;
}
console.log(productOfTheOthers([1, 2, 3, 4, 5]))
// Leave this so we can test your code:
module.exports = productOfTheOthers;
Only if it cannot handle NaN (0 / 0) or Infinity (42 / 0). The code will run to completion but we may not like the result. Recall that NaN and Infinity are both Number in JS.
The code challenge will not accept this answer, but it works. And, I daresay, it’s quite readable.
function productOfTheOthers(array) {
// Write your code here
let product = BigInt(1);
const newArray = [];
for (let i of array) {
if (i != 0) {
product *= BigInt(i);
}
}
for (let i of array) {
if (i != 0) {
newArray.push(Number(BigInt(product) / BigInt(i)));
} else {
newArray.push(i);
}
}
return newArray;
}
console.log(productOfTheOthers([1, 2, 3, 4, 5]));
console.log(productOfTheOthers([5, 0, 5]));
console.log(productOfTheOthers([1, 0, 3, 4, 0]));
console.log(productOfTheOthers([999999, 999999, 999999]));
// Leave this so we can test your code:
module.exports = productOfTheOthers;
function productOfTheOthers(array) {
// Write your code here
let product = BigInt(1);
const newArray = [];
for (let i of array) {
if (i != 0) {
product *= BigInt(i);
}
}
for (let i of array) {
if (i != 0) {
newArray.push(BigInt(product) / BigInt(i));
} else {
newArray.push(i);
}
}
return newArray;
}
console.log(productOfTheOthers([1, 2, 3, 4, 5]));
console.log(productOfTheOthers([5, 0, 5]));
console.log(productOfTheOthers([1, 0, 3, 4, 0]));
console.log(productOfTheOthers([999999, 999999, 999999]));
// Leave this so we can test your code:
module.exports = productOfTheOthers;
My solution works by the simple logic of making an array of other numbers then get their product.
function productOfTheOthers(array) {
let result = [];
for (let i = 0; i < array.length; i++) {
let others = array.slice(0, i).concat(array.slice(i + 1));
let productOfOthers = others.reduce((acc, num) => num * acc);
result.push(productOfOthers);
}
return result;
};
console.log(productOfTheOthers([1, 2, 3, 4, 5]))
// Leave this so we can test your code:
module.exports = productOfTheOthers;
You define a result variable with an empty array, that will collect all calculated values.
The for in loop iterate through all indices of the elements in the given array.
At first we copy the given array and assign it to the arr variable.
Then we delete the element, with the actual index of the iteration, in the copied array.
Now we have a copied array with all the other elements, except the actual element of the iteration, stored in the arr variable.
Our next move is to multiply our whole copied array and add the calculated value to our result variable outside of our for in loop.
The calculated value is assigned to the val variable and get pushed in the result array on the next line.
After the for in loop we return the result variable and we accomplished our task.
function productOfTheOthers(array) {
// Write your code here
let result = [];
for(let i in array) {
let arr = new Array(...array);
arr.splice(i, 1);
let val = arr.reduce((a, b) => a * b );
result.push(val);
}
return result
}
console.log(productOfTheOthers([1, 2, 3, 4, 5]))
// Leave this so we can test your code:
module.exports = productOfTheOthers;