Making sure this javascript code works


Joined
Nov 12, 2022
Messages
1
Reaction score
0
The purpose of the code is to show a house. It's supposed to have all 3 a conditional (example: an if statement), a loop (example: a for or while loop), a function in the code. It is included in the code. But I dont think the code works probably.

JavaScript:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
<script [email protected]/lib/p5.js"></script>
<script>
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
// sky
fill('lightBlue');
noStroke();
rect(0, 0, width, height / 2);
// grass
fill('lightGreen');
rect(0, height / 2, width, height / 2);
// house
let xc = width / 2;
let yc = height / 2;
fill('lightYellow');
let size = 150;
rect(xc - 75, yc - 25, 150, 100);
// roof
fill('brown');
triangle(
xc - size / 2 - 30, yc - 20,
xc, yc - 100,
xc + size / 2 + 30, yc - 20
);
// walkway
fill('lightGray');               
quad(
xc - 70, height,
xc - 20, yc + size/2,
xc + 20, yc + size/2,
xc + 70, height
);
// windows
stroke('black');
fill('red');
rect(xc-15, yc+15, 30, size/2 - 15);
fill('lightGray');
rect(xc-60, yc+20, 30, 30);  // left window
rect(xc+30, yc+20, 30, 30);  // right window
// doorknob
fill(100);
ellipse(xc+9, yc+45, 8);
}
</script>
</html>
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top