Day 6: javaScript for number of houses, bedrooms, square footage, and display each record. function forSale(){ var myhouseJSON; var myObj, x; var i=0; var text=""; var santabarbara=0; var carpinteria=0; var montecito=0; var santaynez=0; var goleta=0; var solvang=0; text= localStorage.getItem("MyhouseJSON", myhouseJSON); myObj=JSON.parse(text); for (i = 0; i < myObj.length; i++) { if (myObj[i][1] =="Santa Barbara") { santabarbara++ //alert("i = " + i); } else if(myObj[i][1]=="Goleta") { goleta++ //alert("10to50 = " + tenToFifty); } else if(myObj[i][1]=="Carpinteria") { carpinteria++ //alert("i = "+ i); } else if(myObj[i][1]=="Montecito") { montecito++ //alert("i = "+ i); } else if(myObj[i][1]=="Solvang") { solvang++ //alert("i = "+ i); } else if(myObj[i][1]=="Santa Ynez") { santaynez++ //alert("i = "+ i); } } document.getElementById("sb").innerHTML= santabarbara; document.getElementById("mon").innerHTML= montecito; document.getElementById("gol").innerHTML= goleta; document.getElementById("sy").innerHTML= santaynez; document.getElementById("sol").innerHTML= solvang; document.getElementById("car").innerHTML= carpinteria; } function forSale2(){ var myhouseJSON; var myObj, x; var i=0; var text=""; var stuff=""; text= localStorage.getItem("MyhouseJSON", myhouseJSON); myObj=JSON.parse(text); text=myObj; house.forEach(myFunction); document.getElementById("demo2").innerHTML="" +"Two bedroom homes " +"" + stuff; function myFunction(item,index){ text += index + ": " + item + ""; if (myObj[index][5] =="two"){ stuff += index + " : " + myObj[index][3] + ""; } } } // three bedroom houses function forSale3(){ var myhouseJSON; var myObj, x; var i=0; var text=""; var stuff=""; text= localStorage.getItem("MyhouseJSON", myhouseJSON); myObj=JSON.parse(text); text=myObj; house.forEach(myFunction); document.getElementById("demo3").innerHTML="" +"Three bedroom homes " +"" + stuff; function myFunction(item,index){ text += index + ": " + item + ""; if (myObj[index][5] =="three"){ stuff += index + " : " + myObj[index][3] + ""; } } } // four bedroom houses function forSale4(){ var myhouseJSON; var myObj, x; var i=0; var text=""; var stuff=""; text= localStorage.getItem("MyhouseJSON", myhouseJSON); myObj=JSON.parse(text); text=myObj; house.forEach(myFunction); document.getElementById("demo4").innerHTML="" +"Four bedroom homes " +"" + stuff; function myFunction(item,index){ text += index + ": " + item + ""; if (myObj[index][5] =="four"){ stuff += index + " : " + myObj[index][3] + ""; } } } function squareFeet(){ var myhouseJSON; var myObj, x; var i=0; var text=""; var stuff=""; var index=0; text= localStorage.getItem("MyhouseJSON", myhouseJSON); myObj=JSON.parse(text); text=myObj; house.forEach(myFunction); document.getElementById("square").innerHTML="" +"Square feet " +"" + stuff; function myFunction(item,index){ text += index + ": " + item + ""; stuff += index + " : " + myObj[index][3] + " sf " + myObj[index][9] + ""; } } function displayFile(){ var myhouseJSON; var myObj, x; //var i=0; var text=""; var homePrice=0; text= localStorage.getItem("MyhouseJSON", myhouseJSON); myObj=JSON.parse(text); //alert(text); var r = document.getElementById("record").value; //alert("record number = " + r); document.getElementById("searchR").innerHTML= "Individual Records Record Number " + r; document.getElementById("city").innerHTML= "City " + myObj[r][1]; document.getElementById("address").innerHTML= "Address " + myObj[r][3]; document.getElementById("bedrooms").innerHTML= "Bedrooms " + myObj[r][5]; document.getElementById("bathrooms").innerHTML= "Bathrooms " + myObj[r][7]; document.getElementById("squareFeet").innerHTML= "Square Feet " + myObj[r][9]; document.getElementById("price").innerHTML= "Price " + myObj[r][11]; document.getElementById("acres").innerHTML= "Acres " + myObj[r][13]; document.getElementById("pool").innerHTML= "Pool " + myObj[r][15]; document.getElementById("stories").innerHTML= "Stories " + myObj[r][17]; document.getElementById("style").innerHTML= "Style " + myObj[r][19]; //function change(){ //alert("r = " + r); //alert("myObj[r][19]" + myObj[r][19]); var image = document.getElementById('circle'); if (r==0 && myObj[r][19] == "Cape Cod"){ image.src="cape0.jpg" } if (r==1 && myObj[r][19] == "Cape Cod"){ image.src="cape2.jpg" } if (r==6 && myObj[r][19] == "Cape Cod"){ image.src="cape6.jpg" } if (r==10 && myObj[r][19] == "Cape Cod"){ image.src="cape10.jpg" } if (r==13 && myObj[r][19] == "Cape Cod"){ image.src="cape13.jpg" } if (r==16 && myObj[r][19] == "Cape Cod"){ image.src="cape16.jpg" } if (r==21 && myObj[r][19] == "Cape Cod"){ image.src="cape21.jpg" } if (r==26 && myObj[r][19] == "Cape Cod"){ image.src="cape26.jpg" } // craftsman homes if (r==2 && myObj[r][19] == "Craftsman"){ image.src="craft2.jpg" } if (r==8 && myObj[r][19] == "Craftsman"){ image.src="craft8.jpg" } if (r==15 && myObj[r][19] == "Craftsman"){ image.src="craft15.jpg" } if (r==18 && myObj[r][19] == "Craftsman"){ image.src="craft18.jpg" } // colonial if (r==3 && myObj[r][19] == "Colonial"){ image.src="col3.jpg" } if (r==7 && myObj[r][19] == "Colonial"){ image.src="col7.jpg" } if (r==17 && myObj[r][19] == "Colonial"){ image.src="col7.jpg" } if (r==24 && myObj[r][19] == "Colonial"){ image.src="col24.jpg" } // ranch style houses if (r==5 && myObj[r][19] == "Ranch"){ image.src="ran5.jpg" } if (r==11 && myObj[r][19] == "Ranch"){ image.src="ran11.jpg" } if (r==20 && myObj[r][19] == "Ranch"){ image.src="ran20.jpg" } // victorian if (r==4 && myObj[r][19] == "Victorian"){ image.src="vic4.jpg" } if (r==9 && myObj[r][19] == "Victorian"){ image.src="vic9.jpg" } if (r==22 && myObj[r][19] == "Victorian"){ image.src="vic22.jpg" } if (r==25 && myObj[r][19] == "Victorian"){ image.src="vic25.jpg" } // cottage if (r==14 && myObj[r][19] == "Cottage"){ image.src="cot14.jpg" } if (r==19 && myObj[r][19] == "Cottage"){ image.src="cot19.jpg" } // French if (r==12 && myObj[r][19] == "Country French"){ image.src="fre12.jpg" } // mediterranean if (r==26 && myObj[r][19] == "Mediterranean"){ image.src="med26.jpg" } Copy and paste this information into your project just after the saveFile() function. Save your application. There six javascript functions here: forSale() forSale2() forSale3() forSale4() squareFeet() displayFile() Let's look at them one at a time. forSale() forSale() reads the file, initializes the variables needed in the function, turns the text into myObj which identifies each element in the file. The record number r is determined by looking at the length of each record. There 27 records in the file 0-26. For example record 4's city would be myObj[4][1]. If myObj[4][1]= "Santa Barbara", then add 1 to the number of houses in Santa Barbara that are listed in our file. The variable name for Santa Barbara is santabarbara and the two plus signs increment or add one to the variable each time thru the loop if the condition is true. Next we need to send the information obtained in the javaScript code to the users' html page. The following lines accomplish this. document.getElementById("sb").innerHTML= santabarbara; document.getElementById("mon").innerHTML= montecito; document.getElementById("gol").innerHTML= goleta; document.getElementById("sy").innerHTML= santaynez; document.getElementById("sol").innerHTML= solvang; document.getElementById("car").innerHTML= carpinteria; The number of houses in each city is displayed in a table on the user's html page. Here is the table. South Coast Real Estate LocationNumber Santa Barbara Goleta Santa Ynez Montecito Carpinteria Solvang Here is what some of the coding looks like for our table. Each table is made up of rows and table data cells. A paragraph id is displayed in the appropriate cells. <tr><td>Santa Barbara</td><td><p id="sb"></p></td></tr> Notice the p id is inside six of the table data cells showing number of homes, <td>. The coding for the table is located at Day 8: forSale2() The purpose of this function is to identify all the houses in the file that have two bedrooms. First we initialize the variables, get the file, turn it into an object. Next we want to iterate or loop through the items. This is where forEach() array method can be helpful. The next line does just that. array.forEach() method iterates over the array items, in ascending order. Remember the name of our array is "house". Each item in the array is called and sent to another function where it looks for two bedroom homes, "myObj[index][5]". The variable in the object searched is [5]. If there is a match, it is selected and the index or record number is displayed and the address of the house. Next demo2 which represents the address of each house is sent the information from the javascript function. <P id ="demo2"> First, two bedroom homes, is displayed in headline form, then the record number followed by a colon and then the address. house.forEach(myFunction); document.getElementById("demo2").innerHTML= "<h4>" + "Two bedroom homes " + "</h4>" + stuff; function myFunction(item,index){ text += index + ": " + item + "<br>"; if (myObj[index][5] =="two"){ stuff += index + " : " + myObj[index][3] + "<br>"; } } } The addition assignment operator (+=) adds the value of the right operand to a variable and assigns the result to the variable. stuff is equal to the index number a colon and the address of the house. Here is the output. Two bedroom homes 0 : 123 Ashley Road 6 : 456 Rio Vista 8 : 4567 Linden Ave. 9 : 456 Parra Granda 14 : 4450 Abrego 20 : 4450 Trigo 26 : 877 Birkenstock Road forSale3() This javascript function is almost identical to the previous one for two bedroom houses. The only changes are what myObj[index][5] is equal to and where the results are displayed, <p id="demo3> forSale4() This javascript function is almost identical to the previous one for two and three bedroom houses. The only changes are what myObj[index][5] is equal to and where the results are displayed, <p id="demo4> Square footage Again this function works the same way as the ones for two, three, and four bedrooms. The variable in the array being examined is [index][9]. Displaying the file one record at a time The application needs to have a way of displaying records individually. Once, for example, all three bedroom houses have been identified, we need to examine the additional fetures of this house. <h3>Find a record in the file</h3> Search for a record <input type=number height=250 value=0 max=26 min=0 name="record" id="record" onChange="displayFile()";> Add this line to the HTML section just before the ending nav tag. This is the line that allows the user to enter the record number to view. We want them to input a number between 0 and 26, the number of houses in the array. The number input control allows the user to click on two small arrows to increase or decrease the number. The OnChange attribute calls the displayFile() function. Here is the function. function displayFile(){ var myhouseJSON; var myObj, x; var text=""; var homePrice=0; text= localStorage.getItem("MyhouseJSON", myhouseJSON); myObj=JSON.parse(text); var r = document.getElementById("record").value; document.getElementById("searchR").innerHTML= "<h3>Individual Records</h3> <br> Record Number " + r; document.getElementById("city").innerHTML= "City " + myObj[r][1]; document.getElementById("address").innerHTML= "Address " + myObj[r][3]; document.getElementById("bedrooms").innerHTML= "Bedrooms " + myObj[r][5]; document.getElementById("bathrooms").innerHTML= "Bathrooms " + myObj[r][7]; document.getElementById("squareFeet").innerHTML= "Square Feet " + myObj[r][9]; document.getElementById("price").innerHTML= "Price " + myObj[r][11]; document.getElementById("acres").innerHTML= "Acres " + myObj[r][13]; document.getElementById("pool").innerHTML= "Pool " + myObj[r][15]; document.getElementById("stories").innerHTML= "Stories " + myObj[r][17]; document.getElementById("style").innerHTML= "Style " + myObj[r][19]; The variables are declared Text is assisned the value of the file. The JavaScript function JSON.parse () converts text into a JavaScript object: The variable r for record is captured from the user input. Each variable in the file is displayed using innerHTML The next thing to do is to display a picture of each house. I used abbreviations for the style of houses like, cape, ran, vic, etc and then added the record number on the end of the file name for identification purposes. All files are .jpgs First I made a list of all houses by style: Cape Cod, Colonial, Victorian, Ranch, Mediterranean, Cottage, Country French. <img src="" alt="house picture" id="circle" height=400 width=400> The line above is in the HTML section. The house pictures are substituted for a blank image with the id of circle. The variable in the array that we want to examine is [19] the style of the house. var image = document.getElementById('circle'); if (r==0 && myObj[r][19] == "Cape Cod"){ image.src="cape0.jpg" } if (r==1 && myObj[r][19] == "Cape Cod"){ image.src="cape2.jpg" } Day 7: The innerHTML tags for houses by city and 2, 3 and 4 bedrooms. The function needs a place to return the results of it's function. The first function forSale() displays the number of houses in each city. I had the results appear in a tablular form. Here is the table for you to add to your project. South Coast Real Estate LocationAddress Santa Barbara Goleta Santa Ynez Montecito Carpinteria Solvang Copy and paste this information into your project right after the beginning aside tag.. Save your application. <p id="demo2"></p> <p id="demo3"></p> <p id="demo4"></p> <p id="square"></p> Key the above infomation just after the table you just added to your project. Save your project. Let's run the application to see if we have any mistakes. Run using the browser of your choice. I am using Chrome for this project. If you do not achieve the desired results, press the F12 to activate the javascript debugger. The console will show you the error and the line that it ocurred in. Correct your error and run again. Here is an example of a javascript error in the forSale() function. Look at the line number where the error occurred. In this example, we were trying to display the results found for number of houses in Santa Barbara. If you look closely, you will see that the tag is missing the letter "a" in santabarbar Fix the error, save and retest. Day 8: displayFile() function and pictures of houses. We want to create a function to display each house in our database. We also need photographs of these homes. The pictures are located in the table in the far right column. Right-click each of them, use save as and select the file names indicated in the table. I just got these off of the Internet. I did pick them by style so that they would match the records. Rec No.StyleImageFile Name 0Cape Codcape0.jpg 2Cape Codcape2.jpg 6Cape Codcape6.jpg 10Cape Codcape10.jpg 13Cape Codcape13.jpg 16Cape Codcape16.jpg 21Cape Codcape21.jpg 26Cape Codcape26.jpg 3Colonialcol3.jpg 7Colonialcol7.jpg 17Colonialcol17.jpg 24Colonialcol24.jpg 14Colonialcol14.jpg 19Colonialcol19.jpg 2Craftsmancraft2.jpg 8Craftsmancraft8.jpg 15Craftsmancraft15.jpg 18Craftsmancraft18.jpg 5Ranchran5.jpg 11Ranchran11.jpg 20Ranchran20.jpg 4Victorianvic4.jpg 9Victorianvic9.jpg 22Victorianvic4.jpg 25Victorianvic25.jpg 17Cottagecot17.jpg 24Cottagecot24.jpg