- Katılım
- 2 Tem 2023
- Mesajlar
- 87
- Konu Yazar
- #1

Selamlar, Bugün sizlerle HTML ve Javascript kullanarak basit bir hesap makinesi örneği paylaşmak istiyorum. Bu örnek, web geliştirmede temel düzeyde Javascript bilgisi olanlar için uygundur. İlk olarak, HTML dosyasına aşağıdaki kodu ekleyelim:
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Basit Hesap Makinesi</title>
</head>
<body>
<input type="number" id="sayi1" placeholder="Sayı 1">
<input type="number" id="sayi2" placeholder="Sayı 2">
<br>
<select id="islem">
<option value="+">Toplama</option>
<option value="-">Çıkarma</option>
<option value="*">Çarpma</option>
<option value="/">Bölme</option>
</select>
<br>
<button onclick="hesapla()">Hesapla</button>
<br>
<div id="sonuc"></div>
<script src="script.js"></script>
</body>
</html>
JavaScript:
function hesapla() {
var sayi1 = parseFloat(document.getElementById("sayi1").value);
var sayi2 = parseFloat(document.getElementById("sayi2").value);
var islem = document.getElementById("islem").value;
var sonuc;
if (islem === "+") {
sonuc = sayi1 + sayi2;
} else if (islem === "-") {
sonuc = sayi1 - sayi2;
} else if (islem === "*") {
sonuc = sayi1 * sayi2;
} else if (islem === "/") {
sonuc = sayi1 / sayi2;
}
document.getElementById("sonuc").innerHTML = "Sonuç: " + sonuc;
}