2017-07-05 6 views
0

こんにちは私はこの関数で問題を抱えていました。数値、開始点、最終点をとり、始点から終点までの数値の乗算表を書く関数を作りたかったのです。例えば、tabmul(10,2,4)は乗算表を返す関数が負の数で失敗します

10.2 = 20

10.3 = 30

10.4 = 40

これは全て良好であるが、それは負の数では動作しないを返します。例えば、 tabmulため(10、-4、-1)を生成する必要があり

10-4 = -40

10-3 = -30

10-2 = -20

10.-1 = -10

ただし、何も返されません。これは私のコードです:

function tabmul(a,b,c){ \\function that generates the multiplication table 
    var myarray = new Array(); 
    var x 
    for(x=b; x<=c; x++){ 
     myarray[x - b] = a*x; 
     document.write(a + "." + x + "=" + myarray[x - b] + "<br>") 
    } 
} 
var a = prompt("Enter the number whose table you want to calculate: ",""); 
var b = prompt("Enter the place where you want the table to start",""); 
var c = prompt("Enter the place where you want the table to end",""); 
\\ this checks if the starting point is smaller or equal than the ending point of the table 
if (0 <= c-b) { 
    tabmul(a,b,c); 

} else { 
    alert("The starting point is bigger than the ending point"); 
} 
+0

、および-1(エンドポイント)よりも-4(始点)また、交換する方が良いだろう –

+0

大きいです「」 「x」で表示されます。 ;-) – RobG

+0

そうですか?私は0 <= c-bはb <= cと同じだと思います。開始点は終了点以下です。 –

答えて

0

あなたは文字列を比較しています。これは、プロンプトが文字列を返すためです。 a,b,cを数字に変換する必要があります。コメントに間違った記号を使用している場合は、それらを修正する必要があります。

function tabmul(a,b,c){ //function that generates the multiplication table 
 
    a = Number(a); 
 
    b = Number(b); 
 
    c = Number(c); 
 
    var myarray = new Array(); 
 
    var x 
 
    for(x=b;x<=c;x++){ 
 
     myarray[x-b] = a*x; 
 
     document.write(a+"."+x+"="+myarray[x-b]+"<br/>") 
 
    } 
 
} 
 
var a = prompt("Enter the number whose table you want to calculate: ",""); 
 
var b = prompt("Enter the place where you want the table to start",""); 
 
var c = prompt("Enter the place where you want the table to end",""); 
 
if(0 <= c-b){ //this checks if the starting point is smaller or equal than the ending point of the table 
 
tabmul(a,b,c); 
 
} 
 
else{ 
 
    alert("The starting point is bigger than the ending point"); 
 
}

+0

ありがとうございます。作成した関数f(x)に数値パラメータを入力すると、数値の代わりに文字列とみなされます。ありがとうございました。 –

+0

プロンプトは文字列を返します。 – Dij

0

また、それが数値であることを確認するNumberparseIntを使用して番号にご入力を変換する必要があります。

その後、負の数を使用してインデックスを使用して配列を参照する際に問題が発生する可能性があります。例えば

myarray[x-b] // can fail if x-b<0. Also for a large number n it will insert empty elements in your array up to N. 

次を取る:

var myarray= new Array(); 
myarray[-4]=1; 
console.log(JSON.stringify(myarray)); 
// result "[]" 
myarray= new Array(); 
myarray[10]=1; 
console.log(JSON.stringify(myarray)); 
// result "[null,null,null,null,null,null,null,null,null,null,1]" 

あなたはそれを文字列に変換することができます:

myarray['"' + (x-b) + '"'] = a*x; 

それともあなただけ(プッシュを使用することができます)と、あなたのインデックスがゼロでスタートします。:

for(x=b;x<=c;x++){ 
    myarray.push(a*x); 
    document.write(a+"."+x+"="+myarray(myarray.length-1)+"<br/>") 
} 
+0

プッシュは面白い機能のようです。私はまだプログラミングをしていないので、私はまだ学んでいます、私の質問には申し訳ありません。私はプッシュと.length関数の詳細を学びます –

0

1)正常にする関数の引数の名前。
2)の代わりに、右の前に 'VAR' の 'for' ループにインデントを使用しますと 'ましょう':

for(let x = b; x <= c; x++){} 

3)は、セミコロンを忘れないでください。出発点は、エンドポイントよりも大きい場合は、チェックしている

function multiplication(val, start, end) { 
    if(end - start <= 0) return; 
    for(let i = start; i <= end; i++) { 
    console.log(val + ' * ' + i + ' = ' + val * i); 
    } 
} 
multiplication(10, -4, -1); 

10 * -4 = -40 
10 * -3 = -30 
10 * -2 = -20 
10 * -1 = -10 
+0

私は数学のバックグラウンドから来て、変数と関数にアルファベットとギリシャ文字を使用するのに慣れています。 "x"が、私は慣れなければならないと思う。答えは入力を数値に変換することだと思います。 –

+0

他の人が読むことができるコードを書くべきです。 – Angels

関連する問題