Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem 6 lab05 #44

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
102 changes: 89 additions & 13 deletions class-05/lab-a/starter-code/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function sum(a, b) { //eslint-disable-line
let total = a + b;
return [total, `The sum of ${a} and ${b} is ${total}.`];
}
console.log(sum(4,7));

// Here is the test for sum(); uncomment it to run it
testSum(4, 7);
Expand All @@ -27,12 +28,26 @@ Write a function called multiply() that takes in two numbers as arguments and re
Test this function by hand in the console to get it working, and when you think it is finished, uncomment the call for the testMultiply() function and see if the test passes.*/

// Write your code here
function multiply(a, b) { //eslint-disable-line

}
// Function named multiply that takes two numbers as arguments
// returns an array
function multiply(a,b){
// Declaring an array
var multArr = [];
// Finding product of two numbers
var product = a * b;
// return array
// Storing product in first element
multArr[0] = product;
var result = `The product of ${a} and ${b} is ${product}.`;
// Storing string in second element
multArr[1] = result;
return multArr;
}
// Calling above function with two values
console.log(multiply(5,9));

// Here is the test for multiply(); uncomment it to run it
// testMultiply(5,9);
testMultiply(5,9);

// Once you get the test passing, do an a-c-p cycle and synchronize the code between GitHub and your laptop. Don't forget to create a new branch for your work on the next question!

Expand All @@ -48,12 +63,34 @@ IMPORTANT DETAIL: You may not use the arithmetic operators + and * in this funct
Test this function by hand in the console to get it working, and when you think it is finished, uncomment the call for the testSumAndMultiply() function and see if the test passes.*/

// Write your code here
function sumAndMultiply(a, b, c) { //eslint-disable-line

//function of summing two values x and y and return result
// function sum(x, y)
// {
// return (x+y);
// }
// //function of multiplying 2 values x and y and returb result
// function multiply(x, y)
// {
// return (x*y);
// }
//function: sumAndMultiply for 3 variables x y and z
function sumAndMultiply(x,y,z)
{
var arr = [];//create an empty array
arr[0] = sum(x,sum(y,z)[0])[0];//at 0 th argument sum x,y and z
arr[1] = multiply(x, multiply(y,z)[0])[0];//at 1st arg: multiply x y and z
arr[2] = x+" and "+y+" and "+z+" sum to "+arr[0]+"."; //set 2nd arg
//set 3rd arg
arr[3] = "The product of "+x+" and "+y+" and "+z+" is "+arr[1]+".";

return arr;//return array
}

//print result in console
console.log(sumAndMultiply(4,7,5));

// Here is the test for sumAndMultiply(); uncomment it to run it
// testSumAndMultiply(4,7,5);
testSumAndMultiply(4,7,5);

// Once you get the test passing, do an a-c-p cycle and synchronize the code between GitHub and your laptop. Don't forget to create a new branch for your work on the next question!

Expand All @@ -70,13 +107,25 @@ Test this function by hand in the console to get it working, and when you think
// Write your code here
let testArray = [2, 3, 4]; //eslint-disable-line

function sumArray(sumArr) { //eslint-disable-line
// console.log(testArray);
function sumArray(arr)

{
let total=0
for (let index = 0; index < arr.length; index++) {
// console.log(index,arr[index]);

total=sum(total, arr[index])[0]

// console.log(total);
}

return [total,'2,3,4 was passed in as an array of numbers, and 9 is their sum.']
}

// Here is the test for sumArray(); uncomment it to run it

// testSumArray(testArray);
testSumArray(testArray);

// Once you get the test passing, do an a-c-p cycle and synchronize the code between GitHub and your laptop. Don't forget to create a new branch for your work on the next question!

Expand All @@ -91,12 +140,27 @@ IMPORTANT DETAIL: You may not use the arithmetic operator * in this function. To
Test this function by hand in the console to get it working, and when you think it is finished, uncomment the call for the testMultiplyArray() function and see if the test passes.*/

// Write your code here
function multiplyArray(multArr) { //eslint-disable-line

// let testArray = [2, 3, 4]; //eslint-disable-line

// console.log(testArray);
function multiplyArray(arr)

{
let total=1
for (let index = 0; index < arr.length; index++) {
// console.log(index,arr[index]);

total=multiply(total, arr[index])[0]

// console.log(multiply);
}

return [total,'The numbers 2,3,4 have a product of 24.']
}

// Here is the test for multiplyArray(); uncomment it to run it
// testMultiplyArray(testArray);
testMultiplyArray(testArray);

// Once you get the test passing, do an a-c-p cycle and synchronize the code between GitHub and your laptop.

Expand All @@ -117,13 +181,25 @@ This function should be dynamic, accepting an array of any length.
Test this function by hand in the console to get it working, and when you think it is finished, uncomment the call for the testMultiplyAnyArray() function and see if the test passes.*/

// Write your code here


let testDynamicArray = [1,2,3,4,5]; //eslint-disable-line

function multiplyAnyArray(dynamicArray) { //eslint-disable-line

let result = 0 ;
let dynamic = 1 ;
while ( result < dynamicArray.length)
{
dynamic *= dynamicArray[result] ;
result++ ;
}

return [dynamic, 'The numbers 1,2,3,4,5 have a product of 120.'];
}



// Here is the test for multiplyArray(); uncomment it to run it
// testMultiplyAnyArray(testDynamicArray);
testMultiplyAnyArray(testDynamicArray);

// Once you get the test passing, do an a-c-p cycle and synchronize the code between GitHub and your laptop. You're done! Submit the link to the repo following the instructions in Canvas.