+ 4
Why this function doesn't work?
var f = prompt ("first number:", );// if you enter 2 var l = prompt ("last number:", );//if you enter 20 var s = prompt ("length of sequence:", );// if you enter 10 function Arithm(a, A, n) { var sum = (a + A)/2*n return sum; }//Why alert(Arithm(f, l, s));// this doesn't work alert(Arithm(2, 20, 10));// like this?
6 Respuestas
+ 8
Bear parseInt(<number>); is for make integer from a (float) number
Like try out this (insert to JS Tab):
window.onload=function test()
{
var x = 26.4;
var y = parseInt(x);
alert(y);
}
😃
+ 8
Iryna Yudina remove "," from your prompt() function. You don't need "," after the "question-text. So var f = prompt ("question") ; --> and done.
Try this:
var f = prompt ("first number:" );// if you enter 2
var l = prompt ("last number:" );//if you enter 20
var s = prompt ("length of sequence:" );// if you enter 10
function Arithm(a, A, n) {
var sum = (a + A)/2*n
return sum;
}//Why
alert(Arithm(f, l, s));// this doesn't work
alert(Arithm(2, 20, 10));// like this?
+ 2
I figured it out.
I just had to add + before prompts to convert them into a numbers. Like
var f = +prompt (....);
Now it works correctly.
+ 1
You have to grave the value like this.
var first = f.value;
var last = l.value;
var seq = s.value;
Then you can use it in function. Like alert(Arithm(first,last,seq)) 😒✌
+ 1
That is great 😆
+ 1
I think you can also use parseInt() to convert the input into numbers. Not sure though...