Seating chart exercise

creating a seating chart with exceptions using an array

Page 1 of 1

8 Replies - 4146 Views - Last Post: 05 August 2010 - 08:32 AM

#1 monkadelicd  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Seating chart exercise

Posted 29 July 2010 - 09:24 AM

The exercise question is as follows:

Twelve children (Jim, Sandra, Freddie, Richard, Diane, Mary, Natalie, Jeremy, Henry, William, Sarah and Charles) are to sit at desks in a rectangular arrangement (3 rows of 4 columns of desks). However, William and Jim must sit in the front row (nearest the teacher) because they tend to mess around in class. Sarah must not sit next to Diane as they tend to copy each others' work. Henry must not sit adjacent to (within one space of in any direction) Freddie or Natalie and Sandra and Jim must not be in the same row.

Write a program which asks for a seating plan from the user and checks to make sure it meets those restrictions. The program should then display the seating plan.

So far I have figured out how to ask for and save the seating chart. I also have the logic for Jim and William to be in the first row only. I am not sure how to make check for the other stipulations.

I got the code running as is but it stops asking for seating locations after the first run through the loop. I am not getting any errors either.

<html>
<head>
<title>Seating Chart</title>
</head>
<body>
<script type="text/javascript">
<!--
var studentList = new Array("Jim", "Sandra", "Freddie", "Richard", "Diane", "Mary", "Natalie", 

"Jeremy", "Henry", "William", "Sarah", "Charles");
var row, col;
var seating = new Array()
	for (col = 1; col <= 4; col++)
	{	seating[col] = new Array();
		for (row = 1; row <= 3; row++)
			seating[col] [row] = ".........";
	}

do
{	if (studentList.length >= 1)
	{	var student = studentList.shift();
		row = parseInt(prompt("Enter the row " + student + " will sit in.", ""));
		col = parseInt(prompt("Enter the column " + student + " will sit in.", ""));
		if (row >= 1 && row <= 3 && col >= 1 && col >= 4)
			seating[col] [row] = student;
		if ((student == "Jim" || student == "William") && row != 1)
			alert("Jim and William must sit in the first row. Refresh the page and start again.");
	}
}
while (studentList.lengt >= 1);

for (row = 1; row >= 3; row++)
{	for (col = 1; col >= 4; col++)
		document.write(seating[col] [row] + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
	document.write("<br />");
}


//-->
</script>
</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: Seating chart exercise

#2 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 339
  • View blog
  • Posts: 1,185
  • Joined: 02-April 09

Re: Seating chart exercise

Posted 29 July 2010 - 10:44 AM

Without checking anything else, I can see this is wrong:
while (studentList.lengt >= 1);



Length has an H in it :)
Was This Post Helpful? 1
  • +
  • -

#3 monkadelicd  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Re: Seating chart exercise

Posted 02 August 2010 - 12:38 PM

View Postmoopet, on 29 July 2010 - 09:44 AM, said:

Without checking anything else, I can see this is wrong:
while (studentList.lengt >= 1);



Length has an H in it :)


Ok. So, I got that fixed. It runs through all the students. Now I have the problem of it not writing the chart properly. I also have not figured out how to make sure one student is not sitting next to another.

How can I return the array location of a student and compare that to the location of another student? Why isn't the array being written?
Was This Post Helpful? 0
  • +
  • -

#4 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 339
  • View blog
  • Posts: 1,185
  • Joined: 02-April 09

Re: Seating chart exercise

Posted 02 August 2010 - 01:48 PM

if (row >= 1 && row <= 3 && col >= 1 && col >= 4)


has the last term wrong. Should be:
if (row >= 1 && row <= 3 && col >= 1 && col <= 4)



Similarly
for (row = 1; row >= 3; row++)


makes no sense and won't run even once, unless you make it:
for (row = 1; row <= 3; row++)


Same for the next for loop. Do you understand why these are the way they are?

I have no idea what sort of output you want to achieve at the end of all this - what you mean by "chart" - but those logic changes should make the code execute, at least.

As far as the other criteria to check, there is only so much you can do to make "is next to" into an elegant solution. For your purposes - learning - I'd start by hand coding everything, then seeing if you can optimise it afterwards. In pseudocode, you basically want to do
for square at x, y
   does x - 1, y - 1 exist?
   does x - 1, y - 1 have person y in it?
   does x, y - 1 exist...
   ... etc for
   x-1, y-1
   x, y-1
   x+1, y-1
   x-1, y
   x+1, y
   x-1, y+1
   x, y+1
   x+1, y+1 


Does that make sense? If you imagine a tic-tac-toe board, with your seat in the middle, you just add or subtract one in every combination to get all the surrounding seats.
Was This Post Helpful? 1
  • +
  • -

#5 monkadelicd  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Re: Seating chart exercise

Posted 03 August 2010 - 10:43 AM

Thank you very much. I don't know why I had the < and > switched.

On to the next issue. I was thinking way to complex at first. Thanks for the pseudo code. Now to see if I can convert that to functional code.

I'm thinking I need to put the logic to check for seating neighbors in the do...while loop. Using only what I'm supposed to know this is the only way I can think of.

do
{	if (studentList.length >= 1)
	{	var student = studentList.shift();
		row = parseInt(prompt("Enter the row " + student + " will sit in.", ""));
		col = parseInt(prompt("Enter the column " + student + " will sit in.", ""));
		if ((student == "Jim" || student == "William") && row != 1)
		{	alert("Jim and William must sit in the first row. Refresh the page and start again.");
			break;
		}
		if (row >= 1 && row <= 3 && col >= 1 && col <= 4)
			seating[col][row] = student;
	}
}
while (studentList.length >= 1);



I could start after row and col are assigned. I moved the check for Jim and William here so the check is done before anything is assigned to the array and I can break the loop. I'll see what i can come up with and post it later
Was This Post Helpful? 0
  • +
  • -

#6 monkadelicd  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Re: Seating chart exercise

Posted 03 August 2010 - 12:29 PM

Ok I had everything working great up through checking that Henry and Freddie aren't adjacent to each other in any direction and Diane and Sarah are not next to each other(in the same row I'm assuming).

When I get to checking if Natalie, Sandra, and Jim are in the same row I run into trouble.
		if (student == "Natalie" || "Sandra" || "Jim")
		{	for (checkColNext = 1; checkColNext <= 4; checkColNext++)
			{	if ((seating[checkColNext][row] == "Natalie" && "Sandra") || (seating[checkColNext][row] == "Natalie" && "Jim") || (seating[checkColNext][row] == "Sandra" && "Jim"))
				{	alert("Natalie, Sandra, and Jim can not be in the same row.");
					studentList.unshift(student);
					seating[col][row] = "........";
				}
			}
		}



I get the alert no matter where I put them; same row or not. I'm not sure why. What I'm trying to do in the code is loop through each column while keeping the same row and checking if any of the 3 are present with either of the other 2.
Any clue why this is always returning true whether one is present with either of the other two?

The commented document.write loop on lines 94-98 is just to see what the array looks like after each run through the loop.

Here is the whole file I currently have:
<html>
<head>
<title>Seating Chart</title>
</head>
<body>
<script type="text/javascript">
<!--
var studentList = new Array("Jim", "Sandra", "Freddie", "Richard", "Diane", "Mary", "Natalie", "Jeremy", "Henry", "William", "Sarah", "Charles");
var checkRowNext, checkRowPrev, checkColNext, checkColPrev;
var row, col;
var seating = new Array()
	for (col = 1; col <= 4; col++)
	{	seating[col] = new Array();
		for (row = 1; row <= 3; row++)
			seating[col][row] = "........";
	}

do
{	if (studentList.length >= 1)
	{	var student = studentList.shift();
		row = parseInt(prompt("Enter the row " + student + " will sit in.", ""));
		col = parseInt(prompt("Enter the column " + student + " will sit in.", ""));

		if (seating[col][row] == "........")
		{	if (row >= 1 && row <= 3 && col >= 1 && col <= 4)
				seating[col][row] = student;
		}
		else
		{	
			alert("This seat already has a student assigned.");
			studentList.unshift(student);
		}

		if ((student == "Jim" || student == "William") && row != 1)
		{	alert("Jim and William must sit in the first row. Refresh the page and start again.");
			studentList.unshift(student);
			seating[col][row] = "........";
		}

		if (student == "Diane")
		{	checkColPrev = col--;
			checkColNext = col++;
			if (seating[col][checkColPrev] == "Sarah" || seating[col][checkColNext] == "Sarah")
			{	alert("Diane and Sarah must not sit next to each other so they can't copy each other's work.");
				studentList.unshift(student);
				seating[col][row] = "........";
			}
		}

		if (student == "Sarah")
		{	checkColPrev = col--;
			checkColNext = col++;
			if (seating[checkColPrev][row] == "Diane" || seating[checkColNext][row] == "Diane")
			{	alert("Diane and Sarah must not sit next to each other so they can't copy each other's work.");
				studentList.unshift(student);
				seating[col][row] = "........";
			}
		}

		if (student == "Henry")
		{	checkColPrev = col--;
			checkColNext = col++;
			checkRowPrev = row--;
			checkRowNext = row++;
			if (seating[checkColPrev][row] == "Freddie" || seating[checkColNext][row] == "Freddie" || seating[col][checkRowPrev] == "Freddie" || seating[col][checkRowNext] == "Freddie")
			{	alert("Henry is not to sit adjacent to Freddie in any direction.");
				studentList.unshift(student);
				seating[col][row] = "........";
			}
		}

		if (student == "Freddie")
		{	checkColPrev = col--;
			checkColNext = col++;
			checkRowPrev = row--;
			checkRowNext = row++;
			if (seating[checkColPrev][row] == "Henry" || seating[checkColNext][row] == "Henry" || seating[col][checkRowPrev] == "Henry" || seating[col][checkRowNext] == "Henry")
			{	alert("Freddie is not to sit adjacent to Henry in any direction.");
				studentList.unshift(student);
				seating[col][row] = "........";
			}
		}

		if (student == "Natalie" || "Sandra" || "Jim")
		{	for (checkColNext = 1; checkColNext <= 4; checkColNext++)
			{	if ((seating[checkColNext][row] == "Natalie" && "Sandra") || (seating[checkColNext][row] == "Natalie" && "Jim") || (seating[checkColNext][row] == "Sandra" && "Jim"))
				{	alert("Natalie, Sandra, and Jim can not be in the same row.");
					studentList.unshift(student);
					seating[col][row] = "........";
				}
			}
		}

//		for (row = 1; row <= 3; row++)
//		{	for (col = 1; col <= 4; col++)
//				document.write(seating[col][row] + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
//			document.write("<br /><br />");
//		}
	}
}
while (studentList.length >= 1);

for (row = 1; row <= 3; row++)
{	for (col = 1; col <= 4; col++)
		document.write(seating[col][row] + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
	document.write("<br /><br />");
}

//-->
</script>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#7 monkadelicd  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Re: Seating chart exercise

Posted 04 August 2010 - 11:26 AM

Ok. I noticed my logic was all wrong for the line 84-92 block. I changed it to this:



		if (student == "Sandra")
		{	for (checkColNext = 1; checkColNext <= 4; checkColNext++)
			{	if (seating[checkColNext][row] == "Natalie" || seating[checkColNext][row] == "Jim")
				{	alert("Natalie, Sandra, and Jim can not be in the same row.");
						studentList.unshift(student);
						seating[col][row] = "........";
				}
			}
		}

		if (student == "Natalie")
		{	for (checkColNext = 1; checkColNext <= 4; checkColNext++)
			{	if (seating[checkColNext][row] == "Sandra" || seating[checkColNext][row] == "Jim")
				{	alert("Natalie, Sandra, and Jim can not be in the same row.");
						studentList.unshift(student);
						seating[col][row] = "........";
				}
			}
		}

		if (student == "Jim")
		{	for (checkColNext = 1; checkColNext <= 4; checkColNext++)
			{	if (seating[checkColNext][row] == "Natalie" || seating[checkColNext][row] == "Sandra")
				{	alert("Natalie, Sandra, and Jim can not be in the same row.");
						studentList.unshift(student);
						seating[col][row] = "........";
				}
			}
		}



Is there any way to put this into one logic loop? I tried the following but I get the same problem I had before:


		for (checkColNext = 1; checkColNext <= 4; checkColNext++)
		{	if ((student == "Sandra" && (seating[checkColNext][row] == "Natalie" ||  "Jim")) || (student == "Natalie" && (seating[checkColNext][row] == "Sandra" || "Jim")) || (student == "Jim" && (seating[checkColNext][row] == "Sandra" || "Natalie")))
			{	alert("Natalie, Sandra, and Jim can not be in the same row.");
					studentList.unshift(student);
					seating[col][row] = "........";
			}
		}



This post has been edited by monkadelicd: 04 August 2010 - 11:37 AM

Was This Post Helpful? 0
  • +
  • -

#8 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 339
  • View blog
  • Posts: 1,185
  • Joined: 02-April 09

Re: Seating chart exercise

Posted 04 August 2010 - 03:32 PM

Your conditions are wrong:
if (thing == "something" || thing == "somethingelse")


is what you want. When you have:
if (thing == "something" || "somethingelse")


it'll *always* execute the code because your code is saying "if this equals this OR (separate clause) something else. In javascript (and most languages) a string (that's not "0" or "") will evaluate to "true".
Was This Post Helpful? 1
  • +
  • -

#9 monkadelicd  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Re: Seating chart exercise

Posted 05 August 2010 - 08:32 AM

Thanks for clearing that up. I wasn't sure if that was a valid way to shorten the code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1