# Algebra 1 – Lesson 2

3April 8, 2013 by Meghan Riling

CODE A

#ask the user for the points

x1 = parseInt(prompt(“What is the x-value of the first point?”))

y1 = parseInt(prompt(“What is the y-value of the first point?”))

x2 = parseInt(prompt(“What is the x-value of the second point?”))

y2 = parseInt(prompt(“What is the y-value of the second point?”))

#find the difference in the y-values

yDifference =

#find the difference in the x-values

xDifference =

#divide to find slope

slope =

#report the slope to the user

alert( )

CODE B

#ask the user for the point and the slope

x = parseInt(prompt(“What is the x-value of the point?”))

y = parseInt(prompt(“What is the y-value of the point?”))

slope = prompt(“What is the slope of the line?”)

#we know that y = mx + b. that means that b = y – mx

#first do the m*x part

slopeByX =

#then subtract

b =

#tell the user their y-intercept

alert( )

CODE C

#ask the user for the point and the slope

x1 = parseInt(prompt(“What is the x-value of the first point?”))

y1 = parseInt(prompt(“What is the y-value of the first point?”))

x2 = parseInt(prompt(“What is the x-value of the second point?”))

y2 = parseInt(prompt(“What is the y-value of the second point?”))

#Use your first program to find the slope.

#Use the second program to find the y-intercept.

#Remember: you don’t need to ask for new info.

#You already know a point, and you just found the slope.

#Tell the user the equation of the line!

alert( )

CODE D

if(9 4)

alert(“TRUE”)

else

alert(“FALSE”)

if(7 7)

alert(“TRUE”)

else

alert(“FALSE”)

if(2 1)

alert(“TRUE”)

else

alert(“FALSE”)

if(14 2)

alert(“TRUE”)

else

alert(“FALSE”)

if(3 15)

alert(“TRUE”)

else

alert(“FALSE”)

CODE E

number = Math.floor(Math.random()*10)

#Describe the game to the user.

#FYI: The possible numbers are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

alert( )

guess = 10

while(guess != number)

guess = prompt(“Make a guess.”)

if( )

alert(“You got it!”)

else if( )

alert(“Go higher!”)

else

alert(“Go lower!”)

#Use your first program to find the slope.

#ask the user for the points

x1 = parseInt(prompt(“What is the x-value of the first point?”))

y1 = parseInt(prompt(“What is the y-value of the first point?”))

x2 = parseInt(prompt(“What is the x-value of the second point?”))

y2 = parseInt(prompt(“What is the y-value of the second point?”))

#find the difference in the y-values

yDifference = y2 – y1

#find the difference in the x-values

xDifference = x2 – x1

#divide to find slope

m = yDifference / xDifference

#Use the second program to find the y-intercept.

#we know that y = mx + b. that means that b = y – mx

#first do the m*x part

slopeByX = m*x2

#then subtract

b = y2 – slopeByX

#Remember: you don’t need to ask for new info.

#You already know a point, and you just found the slope.

#Tell the user the equation of the line!

alert(“The equation is y” + “=” + m + “x +” + b)

number = Math.floor(Math.random()*10)

#Describe the game to the user.

#FYI: The possible numbers are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

alert( “I’m thinking of a number between one and ten. Guess.” )

guess = 10

while(guess != number)

guess = prompt(“Make a guess.”)

if(guess==number)

alert(“You got it!”)

else if(guess<=number )

alert("Go higher!")

else

alert("Go lower!")

[…] -an entry on my programming club blog with code to copy (lesson 1 and lesson 2) […]