JavaScript factorial [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Cant wrap my head around arguments.callee and why truefactorial = 120. Some help would be much appreciated
function factorial(num){
if (num <= 1) {
return 1;
} else {
return num * arguments.callee(num-1)
}
}
var trueFactorial = factorial;
factorial = function(){
return 0;
};
alert(trueFactorial(5)); //120

Inside a function, arguments.callee refers to that function.
So factorial is recursive - it calls itself. No matter what name you might use for it.
Redefining the name factorial to reference a different function has no affect on the first definition, because nowhere in that first definition does it use the name factorial.

As noted in the comments arguments.callee is deprecated and you should not use it (it won't even work if your code is running in strict mode). However, if you replace arguments.callee with factorial in your example, the end result will not do anything useful:
function factorial(num){
if (num <= 1) {
return 1;
} else {
return num * factorial(num-1);
}
}
var trueFactorial = factorial;
factorial = function(){
return 0;
};
alert(trueFactorial(5)); // 0
This is because factorial gets reassigned before your function is called, and when it ultimately tries to call factorial() inside the function, the new factorial() just produces a 0.
There is a way around this, and that is to use a named function expression:
var factorial = function factorial (num) { // <--- This line
if (num <= 1) {
return 1;
} else {
return num * factorial(num-1);
}
}
var trueFactorial = factorial;
factorial = function(){
return 0;
};
alert(trueFactorial(5)); // 120 (Yay!)
If you do this, even if the factorial variable is reassigned outside of the function, it will keep its original meaning inside the function, and will not break.

Related

I need to sort out this algorithm [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
could you please your help.
how to answer this question ?
I need your help thinks
function factorial(n) {
if (n === 0) {
return 1;
} else {
return n * n ;
}
}
factorial(6)
this is not right.. i need to use writing code with Loop javascript.
Get the factorial of n. The inheritance of n is the number 1 through n multiplied by all numbers.
example)
answer should be like this
factorial(4); // => 24
factorial(5); // => 120
One way is to use a simple for loop.
var answer = 1;
for(var i = 2; i <= n; i++)
{
answer *= i;
}
return answer;
You can also do this recursively by multiplying your input by the result of the previous value.
function factorial(n){
if(n <= 1) return 1;
return n * factorial(n-1);
}

Can´t call the function in JavaScript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am making a calculator, where you write in a text input. For testing the code, I just made up a random count and a few "ifs". Here´s the code:
var res=0, operator, pattern, num1, num2
myText= "2~3+2"
pattern= /([\+\-\*\~\V/])/;
var nums= myText.split(pattern);
function makeCount() {
num1= Number(num1);
num2= Number(num2);
if (operator=== "~") {
num1= Math.pow(num1,num2);
nums.splice(i-1, 3, num1);
}
if (operator=== "+") {
num1= num1+num2;
nums.splice(i-1, 3, num1);
}
}
function SrtLoop() { //checks all the numbers
for (var i=0; i<nums.length; i++) {
if (nums[i]=== "~") {
num1=nums[i-1];
num2=nums[i+1];
operator="~";
makeCount();
}
if (nums[i]=== "+") {
num1= nums[i-1];
num2= nums[i+1];
operator="+";
makeCount();
}
}
}
SrtLoop();
res=num1;
Before, I just put the the for loop, without a function, but I realized, after the program checked the operators, it would not check again. So I thought that putting the for loop inside a function, I would call it once, and after making the count it would call the for loop function again. Turns out that without the function, in the end, res=10 (expected), with the function, without calling it, res=undefined (expected), but calling the function destroys all the code and nothing appear in the screen.
Note: i am new here and sorry if my English is bad
Your makeCount function has no reference to i whatsoever. You haven't declared i as a global variable (although that's not a great practice). You also haven't passed it as an argument to the makeCount function.
var res=0, operator, pattern, num1, num2
myText= "2~3+2"
pattern= /([\+\-\*\~\V/])/;
var nums= myText.split(pattern);
function makeCount(i) {
num1= Number(num1);
num2= Number(num2);
if (operator=== "~") {
num1= Math.pow(num1,num2);
nums.splice(i-1, 3, num1);
}
if (operator=== "+") {
num1= num1+num2;
nums.splice(i-1, 3, num1);
}
}
function SrtLoop() { //checks all the numbers
for (var i=0; i<nums.length; i++) {
if (nums[i]=== "~") {
num1=nums[i-1];
num2=nums[i+1];
operator="~";
makeCount(i);
}
if (nums[i]=== "+") {
num1= nums[i-1];
num2= nums[i+1];
operator="+";
makeCount(i);
}
}
}
SrtLoop();
res=num1;
See the fiddle: https://jsfiddle.net/0f2yrxgu/
It doesn't log anything as I really don't understand what you're doing here. However, the error is gone.
The variable i is undefined in function makeCount(). Modify as follows:
function makeCount(i) {
//Your code
}
Modify the calls as follows:
makeCount(i);

Is there an alternative to javascript loop? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I was working on a program, I came to a point where I wanted to loop a value and then use it to test user's input with the if statement, but the problem was that I couldn't use the value of the loop.
At least that's what I think, then I tried placing the if statement as one of the codes that the loop statement is supposed to execute, surprisingly it worked, but when I tried more that one if statement (ie. else if), it only executed one of my if statement.
Please is there an alternative method to use?
Below is the code (Input a Number between 0 and 10):
<input type="number" id="i" />
<input type="button" value="submit" onclick="result()" />
<p id="k"></p>
<script>
function result (){
//first method
var x= i.value;
var y = 0
while (y<10){
y++
}
if (x==y) {
document.getElementById("k").innerHTML = "correct"
}
/ * it didn't work probably because the if statement assumes y to be 0. /*
//second method
while (y<10){
y++
if (x==y) {
document.getElementById("k").innerHTML = "correct"
}
else if (x >y ) {
document.getElementById("k").innerHTML ="wrong"
}
}
//it only tested one of the if statement
</script>
Or is there another way instead of looping?
Some issues with your code:
The first instance the if statement is not inside the loop.
Once you're done with the loop you want to exit it with a break;
In the 2nd instance the else if block should appear after the loop. Why? For example the user enters 2. In the first iteration of the loop 2==1 will fail, 2 > 1 will be true, but it's not the wrong answer. x > y is only the wrong answer when y itself is >= 10.
Instead of looping you can check if the value itself is greater than 0 and less than 10, you can combine as many conditions as you want e.g.
function result (){
//first method
var x= Number(i.value); // Number isn't necessary.
if ( (x > 0) && (x < 10) ) { // The && means "and", so if x is greater than 0 and less than 10.
document.getElementById("k").innerHTML = "correct";
} else {
document.getElementById("k").innerHTML = "wrong";
}
}

How to make this function call add(4)(5)(6) work? which will return 15 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I know one way is there any other way to make this type of function call and get the correct addition.
My answer:
function add(x) {
return function(y) {
return function(z) {
return x + y + z
}
}
}
add(4)(5)(6)
It means that the first function returns another function and then that returned function is called immediately then that function returns another function which is then called immediately which will give us the final answer.
As others have mentioned, you can't quite do that. You could create a fluent interface sort of similar to that though, by extending the Number prototype:
Number.prototype.add = function(num) {
return this.valueOf() + num;
}
Then, you could use it like this:
// Instead of add(2)(7)(11):
(0).add(2)
.add(7)
.add(11);
// Or:
(2).add(7).add(11);
Not sure I'd recommend actually doing that, but it's a fun exercise.
You should pass an array to your function as parameter and make a loop on it:
function add(num) {
var sum = 0;
for (var i = 0; i < num.length; i++) {
sum += num[i];
}
return sum;
}
alert(add([10,20,30]));//will return 60
You can't do that. The first set of parenthesis is there to pass the parameter to your function. You are allowed to pass more than one parameter to your function like this:
var total = add(2, 8);
Inside your add function, you be able to create a sum of every parameter using the arguments keyword:
function add() {
var returnValue = 0;
for (i = 0; i < arguments.length; i++) {
returnValue += arguments[i];
}
return returnValue;
}
The second set of parenthesis will be applied on the value (or object) returned by your function. add(2)(8) would be the same as writing 2(8), which doesn't make sense.

Javascript Equal Probability [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Hi I need a probability in javascript that is a function and returns X 1 in every 2 times and Y 1 in every 30.2 times. The rest of the time the function should returns Z.
The function should be able to be called many times in a row.
This is what I have came up with so far
probability(){
var randomNumber = random() * 100;
if (randomNumber > 50) {
x()
}
else if (randomNumber > 3.31125827815 ) {
y()
}
else {
z()
}
}
Currently, your function will call y() if randomNumber is between 3.3 and 50. You probably want < instead of >
else if (randomNumber < 3.31125827815 ) {
It might make it a bit more readable to sort the intervals in ascending order:
function probability(){
var r = random();
if (r < 1/30.2){
y();
}else if(r < 0.5){
z();
}else{
x();
}
}

Categories

Resources