i am so newbie in programming, what i want is create a file which able automatically count triangel around (actually this is pythagoras problem).
i had tried to code with simple programming (the result is written below), and use function in javascript.
i use simple html file to call the javascript file, but the result is always wrong.
function fungsiKllSegitiga1(a,b) {
var c = Math.SQRT(a * a + b * b);
var kll=a+b+c;
return c.sqrt;
}
function fungsiKllSegitiga2(a,b) {
var c = (a * a + b * b)^(0.5);
var kll=a+b+c;
return c.sqrt;
}
i want variable c to count pythagoras, but i can't do it properly. any suggestion?
Try This:
function fungsiKllSegitiga1(a,b) {
var c = Math.SQRT(a * a + b * b);
return c;
}
fungsiKllSegitiga1(tmp, tmp)
tmp means temporary, change it to the numbers you want.
Related
I'm trying to write a function (GetPositiveInteger) that generates a random integer between variables a and b. if a > b, it's supposed to switch the values of the two so that b=>a before generating the Number.
I don't know if I'm misunderstanding the question or what because I can't figure out where I'm meant to be converting a and b to integers and my program keeps just returning NaN. here's my code maybe someone could take a look at it?
function GetRandomInteger(a, b, c) {
a = Number(a), b = Number(b);
if (a > b) {
var a = 2,
b = 1,
c = a;
a = b;
b = c;
} else {
var x = parseInt(Math.random() * b) + a;
}
return x;
}
let x;
console.log(Number(GetRandomInteger(x)));
When a > b, you're setting them to specific numbers before swapping them, instead of swapping the original values.
The code that generates the random number shouldn't be in else, because you won't run that after swapping a and b. It should just be after the if.
You don't need the c parameter. Use a temporary variable declared inside the function when swapping.
Use Math.floor() to convert a floating point number to an integer. parseInt() is for parsing strings (it will work in this case because it first converts the float to a string, but it's better to use the more specific function).
You need to call the function with two arguments. In the example below I just hard-coded them, but you can use your function that asks the user for numbers. Just use it twice to set two variables.
function GetRandomInteger(a, b) {
a = Number(a), b = Number(b);
if (a > b) {
let temp = a;
a = b;
b = temp;
}
var x = Math.floor(Math.random() * b) + a;
return x;
}
console.log(GetRandomInteger(1, 10));
console.log(GetRandomInteger(15, 3));
I just stumbled upon an interesting example of recursive lambdas and I don't really understand why it works in this manner.
rec = lambda x : 1 if x==0 else rec(x-1)*x
f = rec
rec = lambda x: x+1
print(f(10))
Same in javascript.
var rec = function(a) {
if (a == 0) return 1;
return rec(a - 1) * a;
}
var f = rec
rec = function(a) {
return a + 1;
}
console.log(f(10));
To my surprise both of these print 100 instead of 10! (as I would expect).
Why does the reassignment of rec change the behavior of the f function? When the rec variable is captured in lambda, doesn't it refer to the lambda itself?
Edit.
As most of the answers explain what is going on, let me rephrase the question because I am looking for a more in depth explanation.
So at the moment of the declaration of the function rec in the first line why doesn't the rec in the body of the function bind to itself?
For example if you take JavaScript and rewrite the first line in a seemingly "same" way as suggested in one of the answers you get:
var rec =function rec(a) {
if (a == 0) return 1;
return rec(a - 1) * a;
};
f = rec;
rec = function (a) {
return a + 1;
}
console.log(f(10));
This one prints out 10! as one would expect.
So in this case the "inner rec"(in the function body) binds to the rec of the function-name instead of looking at rec variable and the reassignment of the variable rec didn't change the behavior.
So what I am really asking is the mechanism by which those languages decide what to bind the variables in the lambdas.
I am writing an interpreter myself for a class project and I came across to the same question of when and where to bind those variables. So I wanted to understand how this works in popular languages to implement something similar.
I will address it for python because that is what i am familiar with.
First, this behaviour is only possible because python (and looks like javascript i presume) follows late-binding for their closures. further read
Late binding is when the names within a closure are looked up at runtime (unlike early binding where the names are looked up at compile time.)
What this allows is to mutate behaviours at run time, by rebinding variables that are being looked up at runtime(such as functions like rec).
The last step then is just converting the lambda functions into equivalent def syntax so the real behaviour is clearer.
The code:
rec = lambda x : 1 if x==0 else rec(x-1)*x
f = rec
rec = lambda x: x+1
print(f(10))
Can be equivalent to:
First:
def somefunc(x):
return 1 if x==0 else rec(x-1)*x
Note, python will not complain about rec not existing even on a clean session/kernel because it does not look the value up during function definition. late binding means unless this function is called, python does not care about what rec is.
Then:
rec = somefunc
f = rec
def someotherfunc(x):
return x + 1
f(10) #3628800
Now we change the rec function
rec = someotherfunc
And observe that subsequent function calls of f will use the late-bound rec, that is looked up on the function call.
f(10) #100
PS. Complete code added below:
def somefunc(x):
return 1 if x==0 else rec(x-1)*x
rec = somefunc
f = rec
def someotherfunc(x):
return x + 1
f(10) #3628800
rec = someotherfunc
f(10) #100
You could add some console.log and view, that first f is called with 10, then rec with 9 and the result is 10 * 10.
var rec = function(a) {
console.log('f', a);
if (a == 0) return 1;
return rec(a - 1) * a;
};
f = rec;
rec = function(a) {
console.log('rec', a);
return a + 1;
}
console.log(f(10));
Keeping rec.
var rec = function rec(a) {
console.log('f', a);
if (a == 0) return 1;
return rec(a - 1) * a;
};
f = rec;
rec = function(a) {
console.log('rec', a);
return a + 1;
}
console.log(f(10));
These 3 statements can sum up to a single statement
rec = lambda x : 1 if x==0 else rec(x-1)*x
f = rec
rec = lambda x: x+1
from 1 & 2
f = lambda x : 1 if x==0 else rec(x-1)*x
from above & 3
f = lambda x : 1 if x==0 else x*x
I will suggest to use variable names properly, here you don't need reassignment
why it uses second rec not the first one ?
well your function call is happening after reassignment of rec so you have the latest value in rec as
rec = function(a) {
return a + 1;
}
var f = function(a) {
if (a == 0) return 1;
return rec(a - 1) * a;
}
var rec = function(a) {
return a + 1;
}
console.log(f(10));
I don't have a control over the function invocation and I need to pass a few parameters with a pointer. Is it possible to do it?
var a = function(a, b){
return a + b;
}
b = a; //parameter here???
Any help will be appreciated!
You can use currying, the outer function call will return a pointer to another function that references the parameters you passed. Then you can call the inner function without passing the parameters.
var a = function(x, y) {
function(){
return x + y;
}
}
b = a(2, 3);
b();
The correct way to pass parameters to a is to replace your last line with : b = a(1, 2);.
This will give you 3 as a result, stored into the variable b.
You can see the current value of b at any time by running console.log(b); :
var a = function(a, b){
return a + b;
}
b = a(1, 2); // parameters here
console.log(b); // prints 3
But as I said, the use of b twice here is confusing. A better naming would be :
// sum is a better name
var sum = function(a, b){
return a + b;
}
result = sum(1, 2); // parameters here : 1 corresponds to a, 2 to b
console.log(result); // prints 3
Figured out myself. I am sorry if the question is not clear enough.
This is what I needed.
var a = function(a, b) {
return function(){
console.log(a + b);
}
};
b = a(3, 4);
b();
This is my page:
http://dl.dropboxusercontent.com/s/3kc058darzus1uu/pythagoras_questions.html
And it's supposed to be a revision site for some students I'm tutoring, but I'm having some trouble getting a number to round to 2 dp and I'm not sure why. I've written on lines 99 to 107:
function answers() {
c = Math.sqrt(a*a+b*b)
console.log(c)
Math.round(c*100)/100
console.log(c)
var el = document.getElementById("c")
el.innerHTML="$"+c+"$"
MathJax.Hub.Queue(["Typeset", MathJax.Hub, el])
}
but for some reason it skips the Math.round() function. Here's a sample output from the console:
46.32493928760188
46.32493928760188
I just don't understand why it isn't rounding, what am I doing wrong? Strangely typing
Math.round(c*100)/100
directly into the console gives: 46.32.
Screenshot:
You have to assign the result (returned value) of Math.round(c * 100) / 100 to the variable c again.
And always use a semicolon to separate your JavaScript statements so that you won't have an error when two statements get on the same line (f.e. when you minify the js).
function answers() {
var c = Math.sqrt(a * a + b * b);
c = Math.round(c * 100) / 100;
var el = document.getElementById("c");
el.innerHTML = "$" + c + "$";
MathJax.Hub.Queue(["Typeset", MathJax.Hub, el]);
}
You need:
c = Math.round(c*100)/100;
As it is, your function doesn't update c, nor write its result to any variable.
You need to assign the result of Math.round to a variable, e.g.
c = Math.round(c*100)/100;
mkay why make multiple set's
when u can just
function answers() {
c = Math.round(Math.sqrt(a*a+b*b)*100)/100
console.log(c)
var el = document.getElementById("c")
el.innerHTML="$"+c+"$"
MathJax.Hub.Queue(["Typeset", MathJax.Hub, el])
}
Edit:
another alternative
if the number is already a float is the .toFixed() option
function answers() {
c = Math.sqrt(a*a+b*b).toFixed(2)
console.log(c)
var el = document.getElementById("c")
el.innerHTML="$"+c+"$"
MathJax.Hub.Queue(["Typeset", MathJax.Hub, el])
}
My code:
function calculate(sides) {
var sides = prompt("Triangle side lengths in cm
(number,number,number)"); //String will be size of 4
var nsides = sides.split(" "); //Splits content into array format
//Convert Array instances to integer values a,b,c
for(var loop=0;loop<=nsides.length;loop++) {
if(nsides[loop]!=",")
a = nsides[loop];
if(nsides[loop]!=",")
b = nsides[loop];
if(nsides[loop]!=",")
c= nsides[loop];
} //End for
//Area Calculation
var s = (a+b+c)*0.5 ; //represents the semiperimeter
var area = Math.sqrt(s*(s-a)*s(s-b)*(s-c)) //area calculation
//Result
sides = alert("The triangle's area is " + area + " square cm");
} //End function
//Main calculate(length);
I'm looking to set side a, b, and c to integers; however in order to do that I have to go through the array (I first converted it to an array from a string)
I'm going to add in some standard validation later; as of now I can't seem to place the values from the string entered into 3 separate integers being a b and c.
Other than that, is there a better way i can go about this?
Thanks.
Maybe I misunderstand your question, but is this what you're looking for?
var sides = prompt("Triangle side lengths in cm (number,number,number)");
var nsides = sides.split(",");
var a = +nsides[0];
var b = +nsides[1];
var c = +nsides[2];
//Area Calculation
//...
Note the use of + to force the strings from the array into numbers.
function calculate() {
var sides = prompt("Triangle side lengths in cm (number,number,number)"),
nsides = sides.split(","),
a = parseFloat(nsides[0]),
b = parseFloat(nsides[1]),
c = parseFloat(nsides[2]),
s = (a + b + c) / 2,
area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
alert("The triangle's area is " + area + " square cm");
return area; // return the area
}
First of all I removed your parameter, it was totally unnecessary and was overwritten by the declaration of sides in the first line. Then I changed the split to , so it follows your instructions. Then you need to parse the string to integers using parseInt and specifiying the radix 10, then you can go on with your calculations. Just a last thing, you wrote Math.sqrt(s*(s-a)*s(s-b)*(s-c)), see that s(s-b) causes an exception because you are using a number to be called as a function.