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();
}
}
Related
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 2 years ago.
Improve this question
I'm using Eloquent JavaScript to learn JavaScript. I am a beginner and I would like to understand how this exercise works, it is about recursion and relating in to the remainder operator. I don't understand the comparison and I really would love to know how it works.
function isEven(n) {
if (n == 0) return true;
else if (n == 1) return false;
else if (n < 0) return isEven(-n);
else return isEven(n - 2);
}
console.log(isEven(50)) //true;
console.log(isEven(75)) //false;
console.log(isEven(-1)) //false;
I tried testing -2 in the log and it prints true, why does it do that?
I don't fully understand recursion or JavaScript that much, I would like it if this example is explained to me like I am 5.
Labeling the different decisions:
function isEven (n) {
if (n == 0) return true; // 1
else if (n == 1) return false; // 2
else if (n < 0) return isEven(-n); // 3
else return isEven(n - 2); // 4
}
when you call isEven(-2) it then calls isEven(2) according to // 3 which then calls calls isEven(0) according to // 4 which then returns true according to // 1
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
So I'm just starting out teaching myself JS from a cheesy textbook and there's a challenge to figure out what this code does:
function clunk (times) {
var num = times;
while (num > 0) {
display("clunk");
num = num - 1;
}
}
function thingamajig (size) {
var facky = 1;
clunkCounter = 0;
if (size == 0) {
display("clank");
} else if (size == 1) {
display("thunk");
} else {
while (size > 1) {
facky = facky * size;
size = size - 1;
}
clunk (facky);
}
}
function display(output) {
console.log(output);
clunkCounter = clunkCounter + 1;
}
var clunkCounter = 0;
thingamajig(5);
console.log(clunkCounter);
For the life of me, I can't follow the logic here that gives the answer at the back, which is that "clunk" will display in the console 120 times. They mention it has to do with factorials (I was never any good with math...) but I can't find the step where the code to display "clunk" repeats that many times. To spare you all the details, every time I walk through it on paper/in my head I just get "clunk" 1 as the output in the console... can anyone hold my hand through this or show me where this factorial part is happening? Thanks!
I am not going to go through the code with you. But I am going to introduce you to a method in which you can go through the code with yourself, inspect it step by step, and understand it better.
What i am referring to is the use of debugging. By running the above code with node inspect script.js you can run the script in a debugging mode. This will allow you to place little stop signs within the script that will help you analyze what's going on as it is running.
The main stop sign is simply debugger.
When you put this in the code, the inspect mode will stop at that point and let you access the different variables and methods set up in the script from the console it self. That way you can track what's happening to the different elements and see step by step how the script is operating.
Analyzing it this way will help you understand the logic in a much more profound way and I highly recommend you give it a try.
Here is the script with the debugger set up in a decent way:
function clunk (times) {
var num = times;
while (num > 0) {
display("clunk");
num = num - 1;
debugger;
}
}
function thingamajig (size) {
var facky = 1;
clunkCounter = 0;
debugger;
if (size == 0) {
display("clank");
debugger;
} else if (size == 1) {
display("thunk");
debugger;
} else {
while (size > 1) {
facky = facky * size;
size = size - 1;
debugger;
}
clunk (facky);
}
}
function display(output) {
console.log(output);
clunkCounter = clunkCounter + 1;
debugger;
}
var clunkCounter = 0;
thingamajig(5);
console.log(clunkCounter);
Try it out!
Here are the docs about debugger.
PRO TIP:
You can literally just copy paste the code into your chrome console and it will start the chrome debugger running the script.
Because aviya.developer has already shared with you how to debug the program, I'll talk to you about the algorithm. I would suggest you study up on programming fundamentals a bit more, because it seems like you might now have a good grasp on how loops work. This algorithm relies on the variables facky, and size to do the factorial calculation, which we then pass to the display method. So you can repeat that for a few iterations and you should be able to start to understand the flow of the program. The factorial part is the while loop in the thingamajig method.
facky = facky * size;
facky is initialized to 1, and our size is 5.
The formula for a factorial can be found easily online:
n!=n×(n−1)×(n−2)...×2×1
5! = 5 * 4 * 3 * 2 * 1 = 120
Iteration 1: 1 * 5 = 5
Iteration 2: 5 * 4 = 20
Iteration 3: 20 * 3 = 60
Iteration 4: 60 * 2 = 120
Iteration 5: 120 * 1 = 120
This is the value that we will call the function clunk with.
clunk also has a while loop, which has a termination condition of num > 0. This means we will continue to iterate while this condition is true. Once it is no longer true, the iteration will stop.
This means, we will call the method displaywith a value of "clunk" 120 times before the iteration ends.
When you are analyzing an algorithm, it's important to figure out the key components/variables/operations of that algorithm, and keep track of how they change over time or through iterations. This will indicate when the end-goal is reached.
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);
}
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";
}
}
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.