Skip to main content

Task 27: Build a Simple Calculator with Functions

Start with Affirmationโ€‹

  • "I am destined for greatness. Every step I take brings me closer to my goals. I embrace challenges, knowing they are stepping stones to success. My mind is focused, my heart is determined, and my actions are aligned with my dreams."

Daily Affirmations for Success

  • โœ… I am capable, confident, and unstoppable.
  • โœ… Success flows to me because I take consistent action.
  • โœ… I attract opportunities that align with my purpose.
  • โœ… My hard work and persistence create abundance.
  • โœ… I am always learning, growing, and evolving.
  • โœ… I believe in my potential and trust my journey.
  • โœ… I turn setbacks into comebacks with resilience.
  • โœ… I am grateful for every success, big and small.

Success Reflectionโ€‹

"Every effort I made today has planted seeds for my future success. I release any doubts and trust the process. My dreams are possible, and I am getting closer every day. I am grateful, I am strong, and I am ready for more!"

๐Ÿ’ช Repeat daily, feel the power, and watch your success unfold!


๐Ÿ”น If you donโ€™t understand the task, watch the video above for the answer.


Taskโ€‹

Step 1โ€‹

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Mini Calculator</title>
</head>
<body>
<h1>๐Ÿงฎ Mini Calculator</h1>

<input type="number" id="num1" placeholder="Enter first number">
<input type="number" id="num2" placeholder="Enter second number">
<br><br>

<button onclick="add()">Add โž•</button>
<button onclick="subtract()">Subtract โž–</button>
<button onclick="multiply()">Multiply โœ–๏ธ</button>

<h2 id="result">Result will show here...</h2>

<script src="script.js"></script>
</body>
</html>

Step 2โ€‹

// ๐ŸŽฏ Get input and result elements
const result = document.getElementById("result");

function getNumbers() {
const num1 = parseFloat(document.getElementById("num1").value);
const num2 = parseFloat(document.getElementById("num2").value);
return { num1, num2 };
}

// โž• Add Function
function add() {
const { num1, num2 } = getNumbers();
const sum = num1 + num2;
result.textContent = `Result: ${sum}`;
}

// โž– Subtract Function
function subtract() {
const { num1, num2 } = getNumbers();
const difference = num1 - num2;
result.textContent = `Result: ${difference}`;
}

// โœ–๏ธ Multiply Function
function multiply() {
const { num1, num2 } = getNumbers();
const product = num1 * num2;
result.textContent = `Result: ${product}`;
}

Vison Board - Lukas - Canva Link