+1 678 648 4277 

Currying Function Assignment Solution in Javascript


Currying Function Assignment

1. Start with an array called inputtable. The array should have numbers between

                                                              1 and 10.

2. Use inputtable from step 1 to create the following: -

a)Set of multiples of 5 between 1 and 51. Name it fiveTable

b)Set of multiples of 13 between 1 and 131. Name it thirteenTable

c)Set of squares of the numbers in the inputtable. Name it squaresTable3. Get the odd multiples of 5 between 1 and 100. 5, 15, ...

4. Get the sum of even multiples of 7 between 1 and 100.

a. Example, find the multiples and then sum them: 14 + 28+...

5. Use currying to rewrite the function below: -

function cylinder_volume(r, h){ var volume = 0.0; volume = 3.14 * r * r * h; return volume; }
  1. Use r = 5 and h = 10 to call your curried function.
  2. Reuse the function from part ‘a’ but use h = 17
  3. Reuse the function from part ‘a’ but use h = 11

6. Use the following code to take advantage of closures to wrap content with HTML tags, specifically show an HTML table consisting of a table row that has at least one table cell/element. You can use the console to output your results.

makeTag = function(beginTag, endTag){ return function(textcontent){ return beginTag +textcontent +endTag; } }

Example output for #6. Note that the tag is optional. Please do not use this data, but substitute your own values for the contents of the cells. 

FirstnameLastnameAge
JillSmith50
EveJackson94

7. Following instructions

8. Do the ‘generic’ version of questions 3 and 4, meaning the target multiple must not be hardcoded. This means you should be able to use the same code to handle multiple scenarios, for example, first odd multiples of 11 and then even multiples of 3 (still in the range 1 to 100).

Solution:

Question 1: Start with an array called inputtable. The array should have numbers between

*/ const inputtable = [1,2,3,4,5,6,7,8,9,10]; console.log("Question 1: inputtable: ", inputtable);

Question 2:

*/ function question2(ind, size, multi, array, result) { if(ind > size) { return result; } result.push(array[ind] * multi); return question2(ind + 1, size, multi, array, result); } function question2c(ind, size, array, result) { if(ind > size) { return result; } result.push(array[ind] * array[ind]); return question2c(ind + 1, size, array, result); }

Question 2.a: Set of multiples of 5 between 1 and 51. Name it five Table

*/ let fiveTable = []; question2(0, inputtable.length - 1, 5, inputtable, fiveTable); console.log("Question 2.a: fiveTable: ", fiveTable);

Question 2.b: Set of multiples of 13 between 1 and 131. Name it thirteen table

*/ let thirteenTable = []; question2(0, inputtable.length - 1, 13, inputtable, thirteenTable); console.log("Question 2.b: thirteenTable: ", thirteenTable);

Question 2. c: Set of squares of the numbers in inputtable. Name it squaresTable

*/ let squaresTable = [] question2c(0, inputtable.length - 1, inputtable, squaresTable); console.log("Question 2.c: squaresTable: ", squaresTable);

 Question 3: Get the odd multiples of 5 between 1 and 100.

*/ function question3(indStart, indEnd, result) { if (indStart > indEnd) { return result; } if (indStart % 2 != 0 && indStart % 5 == 0 ) { result.push(indStart); } question3(indStart + 1, indEnd, result) } let oddMultiplesOf5 = [] question3(1, 100, oddMultiplesOf5); console.log("Question 3: The odd multiples of 5: ", oddMultiplesOf5);

Question 4: Get the sum of even multiples of 7 between 1 and 100

*/ function question4(indStart, indEnd, result) { if(indStart > indEnd) { return result; } if(indStart % 2 == 0 && indStart % 7 == 0) { result += indStart; } return question4(indStart + 1, indEnd, result); } let sumEvenMultiplesOf7 = 0 sumEvenMultiplesOf7 = question4(1, 100, sumEvenMultiplesOf7); console.log("Question 4: Sum of even multiples of 7: ", sumEvenMultiplesOf7);

 Question 5: Use currying to rewrite the function below:

*/ function cylinder_volume_r(r){ return function cylinder_volume_h(h) { return 3.14 * r * r * h; } } q5part1 = cylinder_volume_r(5); console.log("Question 5.1: r = 5, h = 10: ", q5part1(10)); console.log("Question 5.2: r = 5, h = 17: ", q5part1(17)); console.log("Question 5.3: r = 5, h = 11: ", q5part1(10));

 Question 6: Use the following code to take advantage of closures to wrap content with HTML tags,

 specifically, show an HTML table consisting of a table row that has at least one table cell/element.

 You can use the console to output your results

*/ makeTag = function(beginTag, endTag){ return function(textcontent){ return beginTag +textcontent +endTag; } } const table_tag = makeTag("", "
"); const tr_tag = makeTag("", ""); const th_tag = makeTag("", ""); const td_tag = makeTag("", ""); const html_code = table_tag(tr_tag(th_tag("Firstname") + th_tag("Lastname") + th_tag("Age")) + tr_tag(td_tag("Jill") + td_tag("Smith") + td_tag("50")) + tr_tag(td_tag("Eve") + td_tag("Jacson") + td_tag("94"))); console.log("Question 6. Create the html: ", html_code);

Question 7

 */

 Question 8: Do the 'generic' version of questions 3 and 4,

 meaning the target multiple must not be hardcoded.

 This means you should be able to use the same code to handle multiple scenarios,

 for example first odd multiples of 11 and then even multiples of 3 (still in the range 1 to 100)

*/ const get_range = (start, end, step=1, targetArr=[]) => { if (start >= end) return targetArr; targetArr.push(start); return get_range(start+step, end, step, targetArr); } const get_filter = (arr, cond, targetArr=[], i=0) => { if (i >= arr.length) return targetArr; if (cond(arr[i])) targetArr.push(arr[i]); return get_filter(arr, cond, targetArr, ++i); } const sum_calculate = (arr, i=0, acc=0) => { if (i >= arr.length) return acc; acc += arr[i]; return sum_calculate(arr, ++i, acc); } const genericOddMultiples = multiple => get_filter(get_range(0, 100, multiple), n => n % 2 != 0); const genericSumEvenMultiples = multiple => sum_calculate(get_filter(get_range(0, 100, multiple), n => n % 2 == 0)); console.log("Question 8. Odd multiples of 11", genericOddMultiples(11)); console.log("Question 8. Sum Even multiples of 3", genericSumEvenMultiples(3));