@$!-!i$!-!

Member Since 07 Dec 2014
Offline Last Active Aug 11 2015 09:11 AM
-----

Topics I've Started

Chapter 9 Delete Sticky

11 July 2015 - 12:40 PM

Hi, 

 

I found something interesting in the function deleteSticky(e) code

 

In order to delete the key from the stickiesArray, we call the getStickiesArray() and get the array. What I saw was, If we dont use the if(stickiesArray) condition as on page 451, the stickiesArray.splice() method doesnt seem to work. The error I got was 'uncaught TypeError : stickiesArray.splice is not a function'

 

Here is the code:

window.onload = init;

function init(){
	var button  = document.getElementById("addButton");
	button.onclick = createSticky;
	var stickiesArray = getStickiesArray();
	for(counter = 0;counter<stickiesArray.length;counter++){
		var keyL = stickiesArray[counter];
		var valueL = localStorage[keyL];
		insertIntoDom(keyL,valueL);
	}
}

function insertIntoDom(key,valueL){	
	var unList = document.getElementById("listOfStickies");
	var listItem = document.createElement("li");
	listItem.setAttribute("id",key);
	listItem.innerHTML = valueL;
	unList.appendChild(listItem);
	listItem.onclick = deleteSticky;
}

function createSticky(){
	var currTime = new Date();
	var time = currTime.getTime();
	var sticky = "sticky_"+ time;
	var stickyNote = document.getElementById("note_text");
	var stickyNoteContent = stickyNote.value;

	var stickiesArray = getStickiesArray();
	localStorage.setItem(sticky,stickyNoteContent);
	stickiesArray.push(sticky);
	localStorage.setItem("stickiesArray",JSON.stringify(stickiesArray));
	insertIntoDom(sticky,stickyNoteContent);
}

function getStickiesArray(){
	var stickiesArray = localStorage["stickiesArray"];
	if(!stickiesArray){
		stickiesArray = [];
		localStorage.setItem("stickiesArray",JSON.stringify(stickiesArray));
	}else {
		stickiesArray = JSON.parse(stickiesArray);
	}
	return stickiesArray;
}

function deleteSticky(e){
	var key = e.target.id;
	if ( e.target.tagName.toLowerCase()=="span"){
		key = e.target.parentNode.id;
	}
	localStorage.removeItem(key);
	var stickiesArray = getStickiesArray();

		for (counter = 0;counter<stickiesArray.length ;counter++ )	{
			if (key == stickiesArray[counter]){
				stickiesArray.splice(counter,1);
			}
		}

	localStorage.setItem("stickiesArray",JSON.stringify("stickiesArray"));
	removeStickyFromDOM(key);
}

function removeStickyFromDOM(key){
	var sticky = document.getElementById(key);
	sticky.parentNode.removeChild(sticky);
}

I dont understand why it doesnt work.. The getStickiesArray() function returns an Array anyways. So when we call the .splice() method, we are calling it on an array. Why do we have to check again if we have the array?


Chapter 7, Page 312

07 July 2015 - 02:14 PM

Hi, 

 

I am dealing with paths and trying to get a triangle on the canvas.

Following is my code:

<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE> Experimenting with paths </TITLE>
<META charset="utf-8">
		<style>
			canvas{border:1px solid black;	}
		</style>
<script src="paths.js"></script>
</HEAD>
<BODY>
	<H1>Drawig triangle with path</H1>
	<canvas width ="600" height = "500" id="triangleexpt">
		
	</canvas>
</BODY>
</HTML>
//paths.js
document.onload = function(){
	var triCanvas = document.getElementById("triangleexpt");
	var context = triCanvas.getContext("2d");
	context.beginPath();
	context.moveTo(100,150);
	context.lineTo(250,75);
	context.lineTo(125,30);
	context.closePath();
	context.lineWidth = 5;
	context.stroke();
	context.fillStyle = "red";
	context.fill();
}

I am not getting why I dont see a red triangle on my page.

Please help.

 

Thanks in Advance !


Chapter 11 Page 516-517

03 June 2015 - 04:19 PM

Hi, 

 

I am having problems with 'the css for two columns'

I edited the 'index.html' and 'starbuzz.css' files from the 'tabledisplay' directory. My html still does not show two columns in the browser.

Following are the files:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Starbuzz Coffee</title>
    <link rel="stylesheet" type="text/css" href="starbuzz.css">
  </head> 

  <body>
    <div id="header">
      <img src="images/header.gif" alt="Starbuzz Coffee header image">
    </div>
	
	<div id="tableContainer">
		<div id="tableRow">
			<div id="main">
			  <h1>QUALITY COFFEE, QUALITY CAFFEINE</h1>
			  <p>
				At Starbuzz Coffee, we are dedicated to filling all your  caffeine needs through our 
				quality coffees and teas. Sure, we want you to have a great cup of coffee and a great 
				coffee experience as well, but we're the only company that actively monitors and 
				optimizes caffeine levels. So stop by and fill your cup, or order online with our new Bean 
				Machine online order form, and get that quality Starbuzz coffee that you know will meet 
				your caffeine standards.
			  </p>
			  <p>
				And, did we mention <em>caffeine</em>? We've just started funding the guys doing all 
				the wonderful research at the <a href="http://buzz.headfirstlabs.com"
				title="Read all about caffeine on the Buzz">Caffeine Buzz</a>.
				If you want the latest on coffee and other caffeine products, 
				stop by and pay them a visit.
			  </p>
			  <h1>OUR STORY</h1>
			  <p>
				"A man, a plan, a coffee bean". Okay, that doesn't make a palindrome, but it resulted
				in a damn good cup of coffee.  Starbuzz's CEO is that man, and you already know his 
				plan: a Starbuzz on every corner.
			  </p> 
			  <p>In only a few years he's executed that plan and today 
				you can enjoy Starbuzz just about anywhere.  And, of course, the big news this year 
				is that Starbuzz teamed up with Head First readers to create Starbuzz's Web presence,  
				which is growing rapidly and helping to meet the caffeine needs of a whole new set of 
				customers.  
			  </p>
			  <h1>STARBUZZ COFFEE BEVERAGES</h1>
			  <p>
				We've got a variety of caffeinated beverages to choose
				from at Starbuzz, including our 
				<a href="beverages.html#house" title="House Blend">House Blend</a>,
				<a href="beverages.html#mocha" title="Mocha Cafe Latte">Mocha Cafe Latte</a>, 
				<a href="beverages.html#cappuccino" title="Cappuccino">Cappuccino</a>,
				and a favorite of our customers, 
				<a href="beverages.html#chai" title="Chai Tea">Chai Tea</a>.
			  </p>
			  <p>
				We also offer a variety of coffee beans, whole or ground, for you to
				take home with you.  Order your coffee today using our online
				<a href="form.html" title="form.html">Bean Machine</a>, and take
				the Starbuzz Coffee experience home.
			  </p>
			</div>

			<div id="sidebar">
			  <p class="beanheading">
				<img src="images/bag.gif" alt="Bean Machine bag">
				<br>
				ORDER ONLINE
				with the 
				<a href="form.html">BEAN MACHINE</a>
				<br>
				<span class="slogan">
					FAST <br>
					FRESH <br>
					TO YOUR DOOR <br>
				</span>
			  </p>
			  <p>
				Why wait?  You can order all our fine coffees right from the Internet with our new, 
				automated Bean Machine.  How does it work?  Just click on the Bean Machine link, 
				enter your order, and behind the scenes, your coffee is roasted, ground 
				(if you want), packaged, and shipped to your door.
			  </p>
			</div>
		</div>	
	</div>
	
    <div id="footer">
      &copy; 2012, Starbuzz Coffee
      <br>
      All trademarks and registered trademarks appearing on 
      this site are the property of their respective owners.
    </div>
  </body>
</html>

body { 
  background-color: #b5a789;
  font-family:      Georgia, "Times New Roman", Times, serif;
  font-size:        small;
  margin:           0px;
}

#header {
  background-color: #675c47;
  margin:           10px;
  height:           108px;
}

#tableContainer {
  display:          table;
  border-spacing:   10px;  
}

#tableRow {
  display:          table-row;
}

#main {
  display:          table-cell;
  background:       #efe5d0 url(images/background.gif) top left;
  font-size:        105%;
  padding:          15px;
  vertical-align:   top;
}

#sidebar {
  display:          table-cell;
  background:       #efe5d0 url(images/background.gif) bottom right;
  font-size:        105%;
  padding:          15px;
  vertical-align:   top;
}

#footer {
  background-color: #675c47;
  color:            #efe5d0;
  text-align:       center;
  padding:          15px;
  margin:           10px;
  font-size:        90%;
}

h1 {
  font-size:        120%;
  color:            #954b4b;
}

.slogan { color: #954b4b; }

.beanheading {
  text-align:       center;
  line-height:      1.8em;
}

a:link {
  color:            #b76666;
  text-decoration:  none;
  border-bottom:    thin dotted #b76666;
}
a:visited {
  color:            #675c47;
  text-decoration:  none;
  border-bottom:    thin dotted #675c47;
}


Please help me fix this.


Chapter 10 Page 357

18 March 2015 - 07:03 PM

Hi, 

 

page 357 of the book says

"Each widget needs its own event handler"

"if you are creating several checkboxes, you are going to need a separate event handler for each of them"

 

I get the point, but don't understand why we need a separate event handler for each widget if the behaviour of the event handler is the same.

 

I tried the following code and it works correctly.

sound_panel1.py:

from tkinter import *
import pygame.mixer
def create_gui(app, mixer,sound_file):
    
    track = mixer.Sound(sound_file)
    track_playing = IntVar()

    Checkbutton(app,variable=track_playing,command=track_toggle,
                text=sound_file).pack(side = LEFT)

    volume = DoubleVar()
    volume.set(track.get_volume())
    Scale(app,variable = volume,command = change_volume,from_ = 0.1,to = 0.9,
          resolution = 0.1,label = "Volume",orient = HORIZONTAL).pack(side = RIGHT)

dj.pyw:

from tkinter import *
from sound_panel1 import *
import pygame.mixer

app = Tk()
app.title("Head First Mix")

mixer = pygame.mixer
mixer.init()

def change_volume(v):
    track.set_volume(volume.get())
    
def track_toggle():
    if track_playing.get() == 1:
        track.play(loops = -1)
    else:
        track.stop()
        
create_gui(app,mixer,"50459_M_RED_Nephlimizer.wav")
create_gui(app,mixer,"49119_M_RED_HardBouncer.wav")

def shutdown():
    mixer.stop()
    app.destroy()

app.protocol("WM_DELETE_WINDOW",shutdown)
app.mainloop()

Can someone please elaborate more on this?

 

Thanks in advance :)


Chapter 6

08 December 2014 - 04:59 PM

Hi,

I was working on the code in the 6th Chapter. The code works fine for the 4 options on the menu but when I Input 5(Quit), I get an "IndexError: list index out of range" error.
Moreover, I do not see the transactions.txt file being created in the directory my python codes are in.
I am running Python 3.3.2

Following is the code:
from transactions import *
import promotion
import starbuzz

items = ["DONUT","LATTE","FILTER","MUFFIN"]
prices = [1.50,2.0,1.80,1.20]
running = True

while running:
    option = 1
    for choice in items:
        print(str(option)+"."+choice)
        option = option + 1
    print(str(option)+".Quit")
    choice = int(input("Choose an option: "))
    if choice == option:
        running = False
    else:
        credit_card = input("Credit card number: ")
        price = promotion.discount(prices[choice-1])
        if input("Starbuzz discount card? ") == 'Y':
            price = starbuzz.discount(price)
save_transaction(price,credit_card,items[choice-1])