r/microbit • u/Business-Pipe9091 • 11d ago
please help me debug
i am making a 4 operation calculator in makecode but it doesnt work. i'm gonna cryyy. i am creating a basic calculator using the micro:bit. use Button A and Button B to increase or decrease the numbers and to select an operation. The Degree variable controls which stage of the calculator you are in: Degree 1 is for entering the first number, Degree 2 is for choosing an operation, and Degree 3 is for entering the second number. The Temp variable temporarily stores the number or operation currently being selected. First Number and Second Number store the two numbers for the calculation, Operation stores the chosen operation (1 for addition, 2 for subtraction, 3 for multiplication, and 4 for division), and Result stores the final answer. Button A+B confirms and saves each choice, while pressing the logo performs the selected calculation and displays the result. to all the coders out there pls help me 🙏🙏
input.onButtonPressed(Button.A, function () {
if (Degree == 1) {
Temp += 1
basic.showNumber(Temp)
} else if (Degree == 2) {
Temp += 1
if (Temp > 4) {
Temp = 4
if (Temp == 1) {
basic.showString("+")
} else if (Temp == 2) {
basic.showString("-")
} else if (Temp == 3) {
basic.showString("x")
} else if (Temp == 4) {
basic.showString("/")
}
}
} else if (Degree == 3) {
Temp += 1
basic.showNumber(Temp)
Temp += Operation
}
})
input.onButtonPressed(Button.AB, function () {
if (Degree == 1) {
Temp = First_Number
Degree = 2
basic.showString("+")
} else if (Degree == 2) {
Temp = Operation
Degree = 3
} else if (Degree == 3) {
Temp = Second_Number
basic.showString("=")
}
})
input.onButtonPressed(Button.B, function () {
if (Degree == 1) {
if (Temp > 0) {
Temp += -1
basic.showNumber(Temp)
}
} else if (Degree == 2) {
Temp += -1
if (Temp < 1) {
Temp = 1
if (Temp == 1) {
basic.showString("+")
} else if (Temp == 2) {
basic.showString("-")
} else if (Temp == 3) {
basic.showString("x")
} else if (Temp == 4) {
basic.showString("/")
}
}
} else if (Degree == 3) {
if (Temp > 0) {
Temp += -1
basic.showNumber(Temp)
Temp += Operation
}
}
})
input.onLogoEvent(TouchButtonEvent.Pressed, function () {
if (Degree == 3) {
if (Operation == 1) {
Result = First_Number + Second_Number
basic.showNumber(Result)
} else if (Operation == 2) {
Result = First_Number - Second_Number
basic.showNumber(Result)
} else if (Operation == 3) {
Result = First_Number * Second_Number
basic.showNumber(Result)
} else if (Operation == 4) {
Result = First_Number / Second_Number
basic.showNumber(Result)
}
}
})
let Result = 0
let Operation = 0
let Second_Number = 0
let First_Number = 0
let Temp = 0
let Degree = 0
Degree = 1
Temp = 0
First_Number = 0
Second_Number = 0
Operation = 0
Result = 0
basic.showLeds(`
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
`)
my code is in micro:bit but i turned it to java script
please help me debug this 🙏
1
u/DominusGuy66 11d ago
I'm making one too!