Why this function doesn't work? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 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?

22nd Dec 2017, 3:43 PM
Iryna Yudina
Iryna Yudina - avatar
6 Antworten
+ 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); } 😃
5th Aug 2018, 12:20 AM
Donthack
Donthack - avatar
+ 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?
5th Aug 2018, 12:24 AM
Donthack
Donthack - avatar
+ 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.
22nd Dec 2017, 4:40 PM
Iryna Yudina
Iryna Yudina - avatar
+ 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)) 😒✌
22nd Dec 2017, 3:54 PM
NabiL
NabiL - avatar
+ 1
That is great 😆
22nd Dec 2017, 5:01 PM
NabiL
NabiL - avatar
+ 1
I think you can also use parseInt() to convert the input into numbers. Not sure though...
22nd Dec 2017, 5:19 PM
Mahmood Saifi
Mahmood Saifi - avatar