Skip to content

Instantly share code, notes, and snippets.

@Masheen88
Last active September 5, 2024 06:38
Show Gist options
  • Save Masheen88/4a2fbc40d9ed2c191485331a2ec0f71f to your computer and use it in GitHub Desktop.
Save Masheen88/4a2fbc40d9ed2c191485331a2ec0f71f to your computer and use it in GitHub Desktop.
Multiply numbers using the forEach method
console.log("this is my test page:");
//1. need a set of numbers to multiply
let myNumbers = [10, 15];
console.log("This is my original Array", myNumbers);
//2. done need a function to multiply things
// we need an parameter to pass into our function
function multiplyNumbers(someParameter) {
console.log("inside my function, This is my parameter", someParameter);
//declares a variable to multiply by
let multiplicationTotal = 1;
console.log(
"This is multiplicationTotal before for each:",
multiplicationTotal
);
// forEach method that goes through each element of the someParameter array and multiplies each element times itself.
someParameter.forEach((someParameterElement) => {
//logs each element of someParameterElement to the console
console.log("This is a cool element in my function:", someParameterElement);
//performs multiplication of each element of someParameterElement
multiplicationTotal *= someParameterElement;
});
// console.log("After the for each method", multiplicationTotal);
// we need a formula to multiply our numbers
return multiplicationTotal;
}
//3. done need to call the function
console.log(
"This is the call of my function and should return a value:",
multiplyNumbers(myNumbers)
); // This line calls our multiplyNumbers Function
@Masheen88
Copy link
Author

update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment