Zdravím,
rozhodl jsem se, že napíši v JavaScriptu kalkulačku. Je dělaná na tom, že každá početní operace má před sebou a za sebou mezeru. Díky těmto mezerám poté rozdělím řetězec do polí. Zatím používám jenom 4 operace : odečítání, sčítání, násobení a dělení. Při kliknutí do = by se měla udělat nová řádka a napsat správný výsledek. Je tu ale menší problém. Chci, aby se na 1 řádce dalo vypočítat alespoň 5 početních operací a při nejlepším 10. Pro každou variaci znaků (+ - / *) bych musel udělat definici, jak se má daný příklad vypočítat. Zjistil jsem, že pokud bych chtěl s 10 operacemi bude to 1 398 100 variací. Není nějaký lepší způsob, jak nadefinovat všechny variace početních operací - + / *?
Fórum › JavaScript, AJAX, jQuery
Kalkulačka (definice jak vypočítat variace různých početních operací)
Toto vlákno bylo označeno za vyřešené — příspěvek s řešením.


Všechny tyhle problémy za vás s přehledem vyřeší funkce eval():
var calculation = '2 * ((3 - 1) + 6 / 2)';
alert(eval(calculation));
U statické stránky s kalkulačkou je to buřt, jinak by se to hodilo ošetřit funkcí, která zkontroluje, jestli se řetězec skládá pouze z operátorů, čísel, mezer a závorek.
Už jsem se bál, že budu muset napsat ten milion definicí. Děkuji ti mnohokrát, ani nevíš, jak jsi mi pomohl/a.
Já to ošetřovat nemusím... ta kalkulačka bude mobilní aplikace a jelikož všechny ty příklady vpisuji do textarea s atributem readonly, tak se tam nemůže dostat žádný jiný znak :)

Přidej příspěvek
Toto téma je starší jak čtvrt roku – přidej svůj příspěvek jen tehdy, máš-li k tématu opravdu co říct!
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
Aktuálně jsou podporována videa ze serverů YouTube, Vimeo a Dailymotion.
×






























































Uživatelé prohlížející si toto vlákno
Uživatelé on-line: 0 registrovaných, 16 hostů
Podobná vlákna
Jak vypočítat PTR DS? — založil vanasi
Jak vyvolat více operací po spuštění události? — založil Machta
Definice pole v C (jak použít pole uvnitř struct?) — založil oxidián
Lehká kalkulačka - Jak vybrat + / - / %? — založil Judr Boomer
Z formulářových polí vypočítat procento rozdílu — založil Jirka
Moderátoři diskuze