In: Computer Science
Solve for number 2, Use Functional Programming ONLY, so No For loops
Use JavaScript:
NOTE: Do NOT use a form of a ‘for’ loop anywhere, including iterators. This is meant to be a functional exercise.
Here is the answer for your question in JavaScript Programming Language.
Kindly upvote if you find the answer helpful.
NOTE : As mentioned I haven't used any 'for' loops but used 'functional programming' concept 'recursion' to achieve the output.
##################################################################
CODE :
//Required variables and array objects var count = 1; var inputtable = new Array(); var resultArray = new Array(); //Call the functions createArray(); oddMultiples(inputtable); //Print result resultArray.reverse(); console.log(resultArray); //Recursive function that gets odd multiples of 5 between 1 and 100 function oddMultiples(inputtable){ if(count === 1) return; else{ if(inputtable[count] % 5 === 0){ if(inputtable[count] % 2 === 1) resultArray.push(inputtable[count]); } count--; return oddMultiples(inputtable); } } //Recrisive function that creates and array of numbers between 1 and 100 function createArray(){ if(count === 100) return; else{ inputtable.push(count); count++; return createArray(); } } |
###################################################
SCREENSHOTS :
Please see the screenshots of the code below for the indentations of the code.
###################################################
OUTPUT :
Any doubts regarding this can be explained with pleasure :)