Hello I have a problem with this exercise: Write a program that calculates the area of a figure?
What figure? 1 - rectangle, 2 - triangle if user will choose rectangle the program should ask questions: -Enter base length: -Enter the height of the rectangle: statement: This field is:
if user will choose second options what is triangle a program will ask questions as: -Enter the side length -Enter the length of side statement: The area of the triangle is:
var bok1 = Number(prompt("Ile wynosi pierwszy bok prostokata?"));
var bok2 = Number(prompt("Ile wynosi drugi bok prostokata?"));
var podstawa trojkata = Number(prompt("Podaj długość podstawy:"));
var wysokosc trojkata = Number(prompt("Podaj wysokosc podstawy:"));
var co_obliczyc = (prompt("Co chcesz obliczyc? prostokat czy trojkat?"));
switch(co_obliczyc) {
so I wrote sth like that and stuck I don't want my program to ask every questions at first and then calculate I want program to ask about the figure then if user will chose one of this he has to give dimensions for the selected figure I have no idea how to do this please help
CodePudding user response:
Just swap the order of questions:
var co_obliczyc = (prompt("Co chcesz obliczyc? prostokat czy trojkat?"));
switch(co_obliczyc) {
case "prostokat":
var bok1 = Number(prompt("Ile wynosi pierwszy bok prostokata?"));
var bok2 = Number(prompt("Ile wynosi drugi bok prostokata?"));
//logic for rectangle
break;
case "trojkat":
var podstawa_trojkata = Number(prompt("Podaj długość podstawy:"));
var wysokosc_trojkata = Number(prompt("Podaj wysokosc podstawy:"));
//logic for triangle
break;
default:
alert("Not implemented")//Sorry, I don't know how to write it in Polish
break;
}