I'm new to JavaScript. I want to implement recursion using two functions: one public, another private.
In Java, I'd always implement recursion like this
public int sumOfPrevious(int n){
return addNumbers(n);
}
private int addNumbers(int n){
if (n == 1)
return 1;
else
return n + addNumbers(n-1);
}
Then calling System.out.println(sumOfPrevious(5)) returns 15
But how do you implement something like that in JavaScript?
I tried this:
function sumOfPrevious(n){
return function privateFunc(n){
if (n == 1)
return 1;
else
return n + privateFunc(n - 1);
};
}
But calling console.log(sumOfPrevious(5)) just gives me [Function: privateFunc]
Why is it doing that and how do I fix this?
You need to declare the nested function and then use it, not return the nested function.
function sumOfPrevious(n){
function privateFunc(n){
if (n == 1)
return 1;
else
return n + privateFunc(n - 1);
}
return privateFunc(n);
}
You are returning the defined function itself.
Simply execute it :
function sumOfPrevious(n){
return (function privateFunc(n){
if (n == 1)
return 1;
else
return n + privateFunc(n - 1);
})(n);
}
A working example here at jsfiddle.
Hope it helps.
You return a function so you get a function...
To define private function you must create a class, as you do in Java, and then make the magic inside
function MyClass() {
var idx = this;
var my_private_member = 5;
this.my_public_member = 0;
function my_private_function() {
idx.my_public_member = my_private_member; //keyword "this" here won't reference to MyClass, but to the private function, so we keep a reference of MyClass to be accessed by the private function
}
this.my_public_function = function() {
my_private_function();
}
}
var my_instance = new MyClass();
my_private_member and my_private_function can't be accessed
Simply use a function for recursion:
JAVA and Javascript are two totally different things.
function sumOfPrevious(n){
if (n == 1)
return 1;
else
return n + sumOfPrevious(n - 1);
}
// call the function
console.log(sumOfPrevious(10)); // 55
Related
I had a class in my code and I declared a constructor inside it...
constructor(rowNumber) {
this._rows = this.init(rowNumber);
}
get lastRow() {
return this._rows[this._rows.length - 1];
}
get rows() {
return this._rows;
}
fact(n) {
return (n > 1) ? this.fact(n - 1) * n : 1;
}
createRow(row) {
var result = [];
for (let i = 0; i <= row; i++) {
result.push(this.fact(row) / this.fact(i) / this.fact(row - i));
}
return result;
}
init(rowNumber) {
var result = [];
for (let i = 0; i < rowNumber; i++) {
result.push(this.createRow(i));
}
return result;
}
}
export {Triangle};
I declared init() function at the end of my code and I used init() in the constructor... if the constructor runs at first and then init() function runs ... does it cause a problem?
Javascript is first creating a class. That'll be an object which contains all those functions on its prototype. You then sometime later instantiate that class with new, at which point constructor and init will be called. At that point it doesn't matter in which order they were written into the source code, since they've already been parsed and put onto the prototype from where they will be called.
var math = {
Factorial:function(n) {
if (n == 0) {
return 1;
}
return n * Factorial(n - 1);
},
CalculateFactorial:function() {
var myValue = document.getElementById('myTextBox').value;
if (myValue.length == 0) {
alert("Please input the correct value!");
return;
}
var result = Factorial(myValue);
document.getElementById('title').innerHTML = result;
}
}
Hello guys. New to JavaScript and testing different things, coming from C# language things are pretty the same yet different.
I am trying to do something and no success.
Having the script above attached to a simple html button , trying to call the CalculateFactorial , math.CalculateFactorial(); , but what is wierd is that CalculateFactorial() doesn't actually see the Factorial() method. But in the math object I can see both methods. Wierd.
"JavaScript runtime error: 'Factorial' is undefined"
A good practice in JavaScript is to name all of your functions even if you assign them to a variable or use them like object's property. In your case just give a name to your Factorial function like this:
Factorial:function factorial(n)
{
if (n == 0)
{
return 1;
}
return n * factorial(n - 1);
},
For the problem of not seeing Factorial in your CalculateFactorial function just use this.Factorial when you invoke it.
This should fix the issue... if you want Factorial to be an internal only function (just a utility for the exposed CalculateFactorial) then you can do so this way:
var math = new function() {
var $this = this;
var Factorial = function(n) {
if (n == 0) {
return 1;
}
return n * Factorial(n - 1);
}
$this.CalculateFactorial = function() {
var myValue = document.getElementById('myTextBox').value;
if (myValue.length == 0) {
alert("Please input the correct value!");
return;
}
var result = Factorial(myValue);
document.getElementById('title').innerHTML = result;
}
}
The deeper design issue here is this is not particularly reusable javascript. You should consider pulling CalculateFactorial() out of the math object and into it's own handler... probably a click handler would be my guess like this:
var math = new function() {
var $this = this;
$this.Factorial = function(n) {
if (n == 0) {
return 1;
}
return n * $this.Factorial(n - 1);
}
}
$("#calcButton").click(function() {
var myValue = document.getElementById('myTextBox').value;
if (myValue.length == 0) {
alert("Please input the correct value!");
return;
}
var result = math.Factorial(myValue);
document.getElementById('title').innerHTML = result;
})
That happens beceause the Factorial method is part of the math object, and it doesn't reside in the global scope. When you try to call it directly, the parser will try to find that method into the global scope, and then, it will thrown an ReferenceError.
Since you're using the method inside the object itself, you can use the this keyword, because it will look into the object's context and find the method Factorial.
Another way of calling the Factorial method is by using the object, e.g: math.Factorial.
Take a look at the example below:
var math = {
Factorial: function(n) {
return n === 0 ? 1 : n * this.Factorial(n - 1);
},
CalculateFactorial: function(txt) {
document.getElementById('title').innerHTML = this.Factorial(txt.value);
}
};
document.getElementById('myTextBox').addEventListener('input', function() { math.CalculateFactorial(this); });
<input type="text" id="myTextBox" placeholder="Fatorial">
<div id="title"></div>
Let me propose an example that works, then follow up with what fails, highlighting the point to my question.
Here, we have 3 functions being called (1 named, 2 anonymous):
var add = function(a, b) {return a+b};
var multiply = function(a, b) {return a*b};
function myFunction(fxn) {
return function(x) {
return function(y) {
return fxn(x,y);
}
}
}
myFunction(add)(2)(3)
Understandably, this call fails:
myFunction(add)(2)(3)(4)
How would I detect how many functions are being called? In the 2nd call, I'm calling 4 functions (1 named, 3 anonymous).
How would I rewrite the myFunction function in a way that compensated for any given amount of calls? I know we can detect how many arguments a function was given, but is there a way to detect how many functions are being called? I hope I worded this correctly. Thanks.
To find out if a variable contains a reference to a function you can use below code:
if (typeof(v) === "function") alert("This is a function")
Based on above you can find out on how many nested functions there are
function myFunction() {
return function() {
return function() {
return 1 + 2;
}
}
}
var count = 0;
var v = myFunction();
while (typeof(v) === "function") {
count++;
v = v();
}
alert("Nr of nested functions: " + count)
Even if this has no practical use case I can think of, this is a possible solution:
var add = function(a, b) {
return a + b
};
var multiply = function(a, b) {
return a * b
};
var counter = 0;
var result = 0;
function myFunction(fxn) {
counter = 1;
result = 0;
return function first(x) {
++counter;
return function second(y) {
++counter;
x = result ? result : x;
result = fxn(x, y);
return second;
}
}
}
myFunction(add)(1)(2)(3)(4);
alert('Result is: ' + result + '; Parentheses count: ' + counter);
What is the difference between
settings = {
edit: function (key, value) {
return anotherFunction(key, value) {
return value * 2;
};
}
};
and
settings = {
edit: function edit(key, value) {
return anotherFunction(key, value) {
return value * 2;
};
}
};
?
There's no difference when executing.
However, in the second case (named function), you can call the function recursively easier because it has a name.
For example, with a named function you can do:
fact: function factorial(n) {
if(n == 0) return 1;
return n * factorial(n-1); //You can do this with a named function easily
}
Without a name, this would be tricky.
Cheers
The essential difference is better debugging. In your developer tools, the named function in your second example will appear as edit in a backtrace; your first example will appear as anonymous. This can be extremely confusing when you're 10 function deep, and they are all called anonymous.
There are three reasons to give a function an inherent name. The first is that everyone does it. It's what everyone is used to.
function factorial(n) {
var accum = 1, i;
for (i = 1; i <= n; i++) {
accum *= i;
}
return accum;
}
The second is to understand stack traces better, as #meagar wrote.
The third is to let you write call functions recursively.
var factorial = function(n) {
var a = 1;
return (function factRecursive(k, a) {
if (k >= 2) {return factRecursive(k - 1, k * a)}
else {return a;}
})(n, a);
}
In JavaScript, is it possible to insert a line into a function that already exists? I want to create a function that inserts a line at a specific position in a function:
function insertLine(theFunction, lineToInsert, positionToInsert){
//insert a line into the function after the specified line number
}
For example, would it be possible to programmatically insert the line checkParameterTypes(min, "string", max, "string"); before the first line of this function?
function getRandomInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
If you want something to happen at the beginning of a function, you can use the following. You do have access to this and the arguments from your injected function. So it will still work for functions that require a specific context.
function inject(before, fn) {
return function(){
before.apply(this, arguments);
return fn.apply (this, arguments);
}
}
For example
function add(a, b) {
return a + b;
}
function concat(a, b) {
return a + b;
}
/**
* You can repeat index and type to check multiple arguments
*/
function createArgumentChecker(index, type /**index, type, ... */) {
var originalArgs = arguments;
return function() {
for (var i=0; i < originalArgs.length; i+=2) {
var index = originalArgs[i],
requestedType = originalArgs[i+1],
actualType = typeof arguments[index];
if (typeAtIndex != actualType) {
console.log("Invalid argument passed at index " + index +
". Expected type " + requestedType + "but it's " + actualType );
}
}
}
}
function logArguments() {
console.log(this, arguments);
}
// Inject an argument checker
add = inject(add, createArgumentChecker(0,"number", 1, "number"));
concat = inject (concat, createArgumentChecker(0, "string", 1, "string"));
// You can even do it multiple times, inject an argument logger;
add = inject(add, logArguments);
concat = inject(concat, logArguments);
JSfiddle
This can be handy when debugging websites that you can't modify the source code, I wouldn't use it do parameter checking unless you can strip it our for the production version.
Yes you can but using eval is always evil ;)
function insertInbetween (arr, value, index) {
var inserted, i, newarr = [];
for (i = 0; i < arr.length; i++) {
if(i == index && !inserted) {
newarr[i] = value;
inserted = true;
}
newarr.push(arr[i]);
}
return newarr;
}
function test (a, b) {
console.log(a,b);
}
var fstrarr = test.toString().split('\n');
eval(insertInbetween(fstrarr, "console.log('injected!');", 1).join('\n'));
Edit:
As mentioned in the comments to your question you'll loose scope by doing so.