„JavaScript“ programa, kad būtų rodoma daugybos lentelė (su pavyzdžiais)

Šiame pavyzdyje išmoksite sugeneruoti skaičiaus daugybos lentelę „JavaScript“.

Norėdami suprasti šį pavyzdį, turite žinoti šias „JavaScript“ programavimo temas:

  • „Loop“ „JavaScript“

1 pavyzdys: daugybos lentelė iki 10

 // program to generate a multiplication table // take input from the user const number = parseInt(prompt('Enter an integer: ')); //creating a multiplication table for(let i = 1; i <= 10; i++) ( // multiply i with number const result = i * number; // display the result console.log(`$(number) * $(i) = $(result)`); )

Rezultatas

 Įveskite sveiką skaičių: 3 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 3 * 4 = 12 3 * 5 = 15 3 * 6 = 18 3 * 7 = 21 3 * 8 = 24 3 * 9 = 27 3 * 10 = 30

Pirmiau pateiktoje programoje vartotojas raginamas įvesti sveiko skaičiaus vertę. Tada forkilpa naudojama kartojant nuo 1 iki 10, kad būtų sukurta daugybos lentelė.

2 pavyzdys: daugybos lentelė iki diapazono

 /* program to generate a multiplication table upto a range */ // take number input from the user const number = parseInt(prompt('Enter an integer: ')); // take range input from the user const range = parseInt(prompt('Enter a range: ')); //creating a multiplication table for(let i = 1; i <= range; i++) ( const result = i * number; console.log(`$(number) * $(i) = $(result)`); )

Rezultatas

 Įveskite sveiką skaičių: 7 Įveskite diapazoną: 5 7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35

Ankstesniame pavyzdyje vartotojas raginamas įvesti sveiką skaičių ir diapazoną, kuriam jie nori sukurti daugybos lentelę.

Vartotojas įveda sveiką skaičių (čia 7 ) ir diapazoną (čia 5 ). Tada sukuriama dauginimo lentelė, naudojant forto diapazono kilpą.

Įdomios straipsniai...