JavaScript - Computer Object
Hey guys,
I need to create a constructor for a computer object. This object must have three properties: speed, and mem_live mem_dead. Then I need to create a new object using this constructor and then have its properties displayed on the screen. Look at what I'm up to so far: Quote: function Computer(speed, mem_live, mem_dead) { this.speed = speed; this.mem_live = mem_live; this.mem_dead = mem_dead; } var computer1 = new Computer("4.0ghz", true, false); document.write(computer1.speed + " " + computer1.mem_live + " " + computer1.mem_dead); What am I doing wrong? It always just shows : 4.0ghz, true, false Similar TutorialsHi everyone, Is it possible to determine at the startup of the page if the visitor is using a computer or a phone to access the site? I would like to make the intro process much better than a simple "click here for HTML version or click here for Flash version". I would like for it to simply redirect to the page I set it to go to depending on whether or not they're using a regular browser or a mobile browser. How would I go about this? Thank you all very much in advance. so here is my "Project http://sw.cs.wwu.edu/~strickk/Project2/project2.html and here are the directions where im stuck at, just right click view source to see the code. I believe what i am doing wrong is where i enter my variables and i dont know how to get an alert message to pop up using an if statement as well as getting the values for the distances to show up correctly directions: 8. Now we will write some JavaScript to validate the input. We don’t want the user to be able to enter the same origin and destination city when they book a ticket. So we will use an if statement to check that. If they have entered the same origin and destination city then we will tell them that by using an alert statement and make them select again. All of your code to validate the input code goes in between the single quotes of the onClick event in the Calculate Fare button. Follow these steps: a) Assign the value of the origin city to a variable called origin Note that the value that gets assigned to origin is actually 0, 60, 90, 120 or 150 (and NOT Bellingham, Everett, Seattle etc.) since the value we gave to the each element of the list was its distance from Bellingham. This will make our lives easier later when we compute the fare. b) Assign the value of the destination city to a variable called destination in a similar fashion. c) Write an if statement that will test whether origin is equal to destination and if it is then do two things. i. Issue an alert message that says Please input different origin and destination cities ii. Stop the execution of the JavaScript code in the onClick event. Use return; as the second statement inside the if statement. Remember to use curly braces to denote that two statements are contained within the if statement. Your if statement will have the following structure to it. if (<put the test you want to do here>) { alert(<put the message here>); return; } //Need a function that sees if the computer has a 2 move forced win, if so take first of those two moves. //and then see if the opponent has a 2 move forced win. if so, then blaock the first of those two moves. // The gameboard array --this holds the game status var gaBoard = [['o', 'o', 'o', 'o', 'o', 'o', 'o'], ['o', 'o', 'o', 'o', 'o', 'o', 'o'], ['o', 'o', 'o', 'o', 'o', 'o', 'o'], ['o', 'o', 'o', 'o', 'o', 'o', 'o'], ['o', 'o', 'o', 'o', 'o', 'o', 'o'], ['o', 'o', 'o', 'o', 'o', 'o', 'o']]; var gcPlayer = "r"; // current player color var gbGameOver = false; //Loading the Gameboard function vLoad() { var strTable, i, j, strID, strOver, strOut, strTitle, strHover, strPush, strArrow; strTable = "<table cellpadding='0' cellspacing='0'>"; //put a row in the table for the title for (i = 0; i < 1; i++) { strTable += "<tr>"; strTitle = "<h1 style= 'color:Orange; font-family:Broadway'>Connect Four</h1>"; //put in the title strTable += strTD(7, strTitle); strTable += "</tr>"; // Buttons area strTable += "<tr>"; strTable += strTD(4, strButton("New Game", "vReset()")); strTable += strTD(3, strButton("Computer Move", "vComputerMove()")); strTable += "</tr>"; } //Arrows Row strTable += "<tr>"; //put in the columns for (j = 0; j < 7; j++) { strID = "img" + j; strHover = "vHover(\"" + strID + "\")"; strPush = "vPush(\"" + strID + "\"," + j + ")"; strArrow = "vArrow(\"" + strID + "\")"; strTable += strTD(1, strImage("arrow.jpg", strID, strPush, strHover, strArrow)); } strTable += "</tr> <tr><td colspan='7'><hr/></td></tr>"; //put a row in the table for the gameboard for (i = 5; i >= 0; i--) { strTable += "<tr>"; //put in the columns for (j = 0; j < 7; j++) { strID = "img" + i + j; strTable += strTD(1, strImage("open.jpg", strID, "", "", "")); } strTable += "</tr>"; } //put a row in the table for the text area for (i = 0; i < 1; i++) { strTable += "<tr>"; strTable += strTD(7, strTextArea(16, 66)); strTable += "</tr>"; } strTable += "</table>"; frm1.innerHTML = strTable; } //*************************************************************************************************** //Functions for Modifying controls function vHover(strID) { document.getElementById(strID).src = "hover.jpg"; } function vArrow(strID) { document.getElementById(strID).src = "arrow.jpg"; } function vMakeRed(strID) { document.getElementById(strID).src = "red.jpg"; } function vMakeBlack(strID) { document.getElementById(strID).src = "black.jpg"; } function vMakeOpen(strID) { document.getElementById(strID).src = "open.jpg"; } function iTopSlot(iCol) { var iRow, iCol; iRow = 0; while ( (iRow < 6) && (gaBoard[iRow][iCol] != 'o')) iRow++; return iRow; } function vPush(strID, iCol) { var iRow, iCol; document.getElementById(strID).src = "push.jpg"; /* if (gbGameOver) { return 0; } */ // see if we can drop a piece into the selected column if (gaBoard[5][iCol] == 'o') { iRow = iTopSlot(iCol); gaBoard[iRow][iCol] = gcPlayer; strID = "img" + iRow + iCol; // switch players if (gcPlayer == "r") { vMakeRed(strID); if (bWinner(gcPlayer)) { alert(gcPlayer + " has won the game."); frm1.taOutput.value += gcPlayer + " has won the game."; gbGameOver = true; } gcPlayer = "b"; } else { vMakeBlack(strID); if (bWinner(gcPlayer)) { frm1.taOutput.value += gcPlayer + " has won the game."; alert(gcPlayer + " has won the game."); gbGameOver = true; } gcPlayer = "r"; } } else { frm1.taOutput.value += "That column is full - try again.\n"; } } function bWinner(cPlayer) { var iCol, iRow; //Horizontal Win for (iRow = 0; iRow < 6; iRow++) { for (iCol = 0; iCol < 4; iCol++) { if ((gaBoard[iRow][iCol] == cPlayer) && (gaBoard[iRow][iCol + 1] == cPlayer) && (gaBoard[iRow][iCol + 2] == cPlayer) && (gaBoard[iRow][iCol + 3] == cPlayer)) return true; } } //Verticle Win for (iRow = 0; iRow < 3; iRow++) { for (iCol = 0; iCol < 7; iCol++) { if ((gaBoard[iRow][iCol] == cPlayer) && (gaBoard[iRow + 1][iCol] == cPlayer) && (gaBoard[iRow + 2][iCol] == cPlayer) && (gaBoard[iRow + 3][iCol] == cPlayer)) return true; } } //diagonal win Positive Slope for (iRow = 0; iRow < 3; iRow++) { for (iCol = 0; iCol < 4; iCol++) { if ((gaBoard[iRow][iCol] == cPlayer) && (gaBoard[iRow + 1][iCol + 1] == cPlayer) && (gaBoard[iRow + 2][iCol + 2] == cPlayer) && (gaBoard[iRow + 3][iCol + 3] == cPlayer)) return true; } } //diagonal win Negative Slope for (iRow = 3; iRow < 6; iRow++) { for (iCol = 0; iCol < 4; iCol++) { if ((gaBoard[iRow][iCol] == cPlayer) && (gaBoard[iRow - 1][iCol + 1] == cPlayer) && (gaBoard[iRow - 2][iCol + 2] == cPlayer) && (gaBoard[iRow - 3][iCol + 3] == cPlayer)) return true; } } return false; } function vComputerMove(){ var iRow,iCol,iWinCol; //Try to win iWinCol = -1; iLoseCol = -1; for(iCol = 0; iCol < 7; iCol++) { iRow = iTopSlot(iCol) if(iRow < 6) { gaBoard[iRow][iCol] = gcPlayer; if(bWinner(gcPlayer)) iWinCol = iCol; gaBoard[iRow][iCol] = 'o'; } } if (iWinCol > -1) { vPush("img" + iWinCol, iWinCol); } //Try to Block if (gcPlayer == 'b') { for (iCol = 0; iCol < 7; iCol++) { iRow = iTopSlot(iCol) if (iRow < 6) { gaBoard[iRow][iCol] = 'r'; if (bWinner('r')) iLoseCol = iCol; gaBoard[iRow][iCol] = 'o'; } } } else if (gcPlayer == 'r') { for (iCol = 0; iCol < 7; iCol++) { iRow = iTopSlot(iCol) if (iRow < 6) { gaBoard[iRow][iCol] = 'b'; if (bWinner('b')) iLoseCol = iCol; gaBoard[iRow][iCol] = 'o'; } } } if (iLoseCol > -1) { vPush("img" + iLoseCol, iLoseCol); } else vRandomMove(); } function vRandomMove() { var iRandom, iRow; iRow = 6; while (iRow == 6) { iRandom = (Math.floor(Math.random() * 7)); iRow = iTopSlot(iRandom); } vPush("img" + iRandom, iRandom); } function vReset() { for (i = 5; i >= 0; i--) { for (j = 0; j < 7; j++) { gaBoard[i][j] = 'o'; strID = "img" + i + j; vMakeOpen(strID); } } frm1.taOutput.value = ""; gcPlayer = "r"; } //*************************************************************************************************** //Functions for making form controls //Button Maker function strButton(strValue, strOnclick) { return "<input type='button' value='" + strValue + "' onclick='" + strOnclick + "' />" } //Image Maker function strImage(strSource, strID, strOnclick, strMouseOver, strMouseOut) { var strReturn; strReturn = "<img src='" + strSource + "' id='" + strID + "' onclick='" + strOnclick + "' "; strReturn += "onmouseover='" + strMouseOver + "' onmouseout='" + strMouseOut + "' />" return strReturn; } //TD Maker function strTD(iColspan, strContent) { return "<td colspan='" + iColspan + "'>" + strContent + "</td>"; } //Text Area Maker function strTextArea(iRows, iCol) { var strReturn; strReturn = "<textarea id='taOutput' "; strReturn += "' rows= '" + iRows + "' cols='" + iCol + "'readonly='readonly'>"; strReturn += "</textarea>"; return strReturn; } I'm writing a report for my college class about what it takes to get into the field of computer programming and i need to obtain information from someone who is in the position of hiring entry level computer programmers. it would Really! help me out if someone has time to fill out these questions. I know its asking for a lot but if anyone has the time it would be highly appreciated! 1. Tell me a little bit about the company? ( Main focus, how did it start, who are your customers, company growth,etc) 2. Where do you see the industry in 5 years? 3. What makes the company successful? 4. What are you looking for in a potential candidate? 5. When interviewing a potential employee, what questions do you expect them to ask you? 6. How could a typical day on the job be described? How much variety is there on a day-to-day basis? 7. When you look at resume, what is your main focus in terms of order and importance, reference? 8. How do you feel about networking as a tool to research potential candidates? (ex. LinkedIN, Facebook?) 9. What education, experience and qualifications are need to enter the field as entry level position? 10. What is the salary range and job responsibilities? 11. What is best liked and least liked about this field / job? 12. What is a typical interview in this field? How is it different then regular interviews? 13. Is there a demand for people in this field? 14. What special advice could you give a person entering the field? 15. What types of training do companies offer persons entering this field? 16. Which professional journals and organizations would help me learn more about this field? 17. How can a new graduate obtain experience in the field? How and where can one get internship? 18. What types of technologies and software is used in the field? 19. Would you have any more information that will be of use to me? 20. Can you suggest others who may be valuable sources of information? Hi, every time I try and alert: [ { number:0, secondnumber:0 }, { number:2, secondnumber:1 }, { number:1, secondnumber:2 } ] it just shows [object object], [object object], [object object]. Why is this and what can I do to make the record be shown as it is above in an alert? Thanks. I created a method for displaying an object's properties: Code: renderfunction = false; function showProperty (object, property) { document.write ('<td class="type">' + (typeof object[property]) + '</td>' + '<td class="name">' + property + '</td>'); document.writeln('<td class="value">' + ( (typeof object[property] != 'function') ? object[property] :( (property != 'showProperties') ? ( renderfunction ? object[property]() : ('<span class="self">NOT RENDERED</span>') ) : ('<span class="self">THIS</span>') ) ) + '</td>'); document.writeln('<td class="hasOwnProperty" >' + ( object.hasOwnProperty(property) ? "Local" : "Inherited" ) + '</td>'); if (typeof object[property] == 'function') { document.writeln ('<td class="function">' + object[property] + '</td>'); } else { document.writeln ('<td class="function"> </td>'); } } As long as renderfunction = false, the object is fine coming out of this function. However, if I change renderfunction to true, all my properties become undefined. Why isn't this working as I expect it to? How should I fix it? Thanks in advance, -Brian. I can't get any info from Firebug except that one line, uncaught exception [object Object]. The code fully worked, then I needed to make it dynamically create Sortables from the scriptaculous library based on how many X were in a table in my database, which I've done, and I'm thinking it may be a simple slight parse error of some type, I'm not too good with Javascript, because now my script barely works. I've double checked the script's source code, the PHP variables are exactly what they should be. Code: print<<<HERE Sortable.create('sortlist$box', { tag: 'img', overlap:'horizontal',constraint:false, containment: $list, dropOnEmpty: true, onChange: function(item) { var list = Sortable.options(item).element; if(changeEffect) changeEffect.cancel(); changeEffect = new Effect.Highlight('changeNotification', {restoreColor:"transparent" }); }, onDrop: function(item) { var thing=Sortable.options(item).element.identify(); var anchors = document.getElementById(thing).childNodes.length-2; if(anchors > 20){ alert('This box had 20 creatures in it already, your last action has not been saved.'); window.location.reload(); } else{ new Ajax.Request("saveImageOrder.php", { method: "post", parameters: { data: Sortable.serialize("sortlist$box") } }); } } }); HERE; $box++; } ?> }); </script> if you solve this I'll send ya $10 via paypal Hello together! I generate html code with jsp. In that jsp there a several framesets and frames. And yes i know, frames are not really up to date but it's an old program and i have to deal with it now. Anyway, in the top frameset i have an onload attribute like onload="load()". In the function load i want to access the Element.prototype object. But unfortunately typeof Element gives me "undefined". So i looked a little deeper and found that window.toString() gives me "[object]" and not as expected "[object window]" so somehow my window doesn't know that its construcor is Window. window.construcor is "undefined" as well. And i don't have access to the Element object. I really don't know where the error could be. When the page is loaded and i access the same window over the console, then everything is right. But in my function a can't get access to the objects i need. I also don't know what part of the code could be useful to post here, but maybe someone had a similar problem before? i should say that this problem only occurs in IE8. In IE9 it works perfectly. Has anyone any idea?? Hi all, I'm stumped on finding a way in javascript to create an object factory whose instances are also object factories. In short I want something like that below, but no joy ... any clues? Code: function createClass () { return new createClass() function createClass() { return new createInstance () function createInstance () { //Default properties, values and methods which might later be extended } } } var createDoor = createClass(); var door1 = createDoor(); var door2 = createDoor(); var createChair = createClass(); var chair1 = createChair (); var chair2 = createChair (); Ignore post (if mod, please delete)
I'm writing a program that involves a network of interconnected nodes (or simply objects in my example below). It depends on being able to access properties of an object's linked objects (a bit oddly worded, sorry)... Problem is I'm not sure how to properly access those properties... see below please. <script> //This is an example of a problem im having in my own code... //I want to access the name of the object within the links array wintin the object... var objA = {name: "Object A", links: [objB, objC]}; var objB = {name: "Object B", links: [objC, objD, objE]}; var objC = {name: "Object C", links: [objB]}; var objD = {name: "Object D", links: [objE]}; var objE = {name: "Object E", links: [objD]}; //ex: I want to access the name of Object A's first link... console.log(objA.links[0].name); </script> I'm hoping to get "Object B"... But instead I get: TypeError: Result of expression 'objA.links[0]' [undefined] is not an object. Is there another way around this? Any thoughts are appreciated. Quote: menu: function( a, b ) { $( b ).observe( 'click', function( event ) { event.stop(); if( $( a ).visible() ) { $( a ).hide(); $( b ).removeClassName( 'selected' ); document.stopObserving( 'click' ); } else { $( a ).show(); $( b ).addClassName( 'selected' ); document.observe( 'click', function( e ) { if( e.target.id != a && e.target.id != b && !Element.descendantOf( e.target, $( a ) ) ) { $( a ).hide(); $( b ).removeClassName( 'selected' ); document.stopObserving( 'click' ); } }); } }); $$( '#' + b + ' > a' ).each( function( element ) { element.observe( 'click', function( event ) { $( a ).hide(); $( b ).removeClassName( 'selected' ); document.stopObserving( 'click' ); }); }); } This work's perfrect accept when i use it with others on the menu it leaves the other ones open, how do i get it to close the open one when i open a new menu.. Thanks. Hello. Is there any way to get the variable name of an object from inside the object? E.g. PHP Code: function Bla(){ this.write = function(){ document.write(<objectname>); } } obj1 = new Bla(); obj1.write(); //Outputs obj1 Here is my script: PHP Code: function myTimer(seconds, obj){ this.seconds = seconds; this.obj = obj; this.startTimer = function(){ if(this.seconds>0){ this.seconds--; this.obj.innerHTML = this.seconds; this.timer = setTimeout("Timer.start()",1000); } } } Timer = new Timer(10, obj); Timer.startTimer(); Now the problem is that the variable that contains the object must be named "Timer". This way I cannot create new timer objects with different variable names I have tried: this.timer = setTimeout("this.start()",1000); but it doesn't work. That's why it would be good to detect the variable name and instead use something like this: this.timer = setTimeout(varname+".start()",1000); I would rather not have to pass the variable name through a parameter like this: Timer1 = new Timer(10, obj, "Timer1"); Thanks in advance. I am trying to troubleshoot my javascript function below. Code: <script type="text/javascript"> function txtboxfill(fillobj) { var rChar=String.fromCharCode(13); var myVal = "To: " + this.form.to.value + rChar; myVal = myVal + "From: " + this.form.from.value + rChar; myVal = myVal + "Subj: " + this.form.subj.value + rChar + rChar; myVal = myVal + "Message: "; fillobj.value = myVal; } </script> Then I have a text box input field with an Code: <input type="text" id="tp" name="to" size="34" class="totext" onkeyup="txtboxfill(this.form.msg_area);"> and when I enter text in this field it gives me a 'this.form.to' is null or not an object and does not populate the textarea. BTW, msg_area is the id of my textarea. Thank you. Code: function globals() { this.states = ""; } now I can: Code: globals.states = .... how to extend first code to be able: Code: globals.states.something = .... ? Greetings, I have a floating object that work find within my page. However, I would like to only show after a button is clicked. Any ideas how to do this. Thanks Hello, below you will find two objects, background and photo1, which I need help cleaning up a bit. This will eventually be imported using Json but for now during development it sits as is in my script. I need help with a couple things: - You will notice the 'val' property is identical in both objects. How can I move this into an outside function that each object can share? I'm having trouble bc I couldn't get 'this' to work outside of its current context. - Second, how can I restructure this so that I can instantiate a new object. So if I wanted to add a new layer I'd like to do something like importLayers.push[new Layer('photo2')] Code: var importLayers = { background: { layerType: 'background', index: 0, layerContent: { content: function() { return $(element).find('#background_Content'); }, width: 850, height: 530, xPos: 0, yPos: -150, skewX: 0, skewY: 0, angle: 0, originX: function() { return (this.sWidth() / 2); }, originY: function() { return (this.sHeight() / 2); }, scaleX: 1, scaleY: 1, sWidth: function() { return this.width * this.scaleX; }, sHeight: function() { return this.height * this.scaleY; }, aspect: 0, opacity: 100, val: function(prop, val) { switch(prop) { case 'width': if(plugin.constrain) this.scaleX = this.scaleY = val / this.width; else this.scaleX = val / this.width; break; case 'height': if(plugin.constrain) this.scaleY = this.scaleX = val / this.height; else this.scaleY = val / this.height; break; case 'angle': this.angle = val; } buildTransformation(this, this.content()); } }, }, photo1: { layerType: 'photo', index: 1, layerContent: { content: function() { return $(element).find('#photo1_Content'); }, width: 1920, height: 1280, xPos: 0, yPos: 0, skewX: 0, skewY: 0, angle: 0, originX: function() { return (this.sWidth() / 2); }, originY: function() { return (this.sHeight() / 2); }, scaleX: .15625, scaleY: .15625, sWidth: function() { return this.width * this.scaleX; }, sHeight: function() { return this.height * this.scaleY; }, aspect: 0, opacity: 100, val: function(prop, val) { switch(prop) { case 'width': if(plugin.constrain) this.scaleX = this.scaleY = val / this.width; else this.scaleX = val / this.width; break; case 'height': if(plugin.constrain) this.scaleY = this.scaleX = val / this.height; else this.scaleY = val / this.height; break; case 'angle': this.angle = val; } buildTransformation(this, this.content()); } }, } } Hi All, (Another Post LOL) Ive been getting alot of help with js recently THANKS! One last thing and i shouldnt be on here for a while How would i go about doing this Code: <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="750" height="550" codebase="http://active.macromedia.com/flash4/cabs/swflash.cab#version=4,0,0,0"> <param name="movie" value="SWF/helicopter.swf"> <param name="quality" value="high"> <param name="menu" value="false"> </object> into JS using .innerHTML = "Above Stuff"; I always get an error + i dont know how im going wrong with the quotes :S ANY HELP (GOOD STUFF); Thanks In Advance, Jamie. NOM NOM NOM! I have a select with a OnChange Function. I would like to know how to create a different object depending on the selection on the Select. Example: MySelect have Option 1 and Option 2, so If (Myselect.value == Option 1) {Create Object 1} If (Myselect.value == Option 2) {Create Object 2} I'm using the object under <body> and it's working, but when I'm trying to use it under a <script> does not work. This is the object I'm trying to create: (It is a map) so what I'm trying is to change the values or map depending on the selection of the Select. <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="900" height="600" id="zoom_map" align="top"> <param name="movie" value="us_albers.swf?data_file=senate.xml" /> <param name="quality" value="high" /> <param name="bgcolor" value="#FFFFFF" /> <embed src="us_albers.swf?data_file=senate.xml" quality="high" bgcolor="#FFFFFF" width="100" height="100" name="Clickable U.S. Map" align="top" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"> </embed> </object> Thank you very much I want to print the name of a field on the screen, but it is just printing "[object HTMLInputElement]", which of course is the object. How can I make the JS print the actual name? Code: function validate(field,txt) { with (field) { if (value==null||value=="") { field.style.borderColor="#c51717"; field.style.color="#c51717"; field.style.backgroundColor="#ffbbbb"; var fname = field; document.getElementById("errorBox").innerHTML = "<p>Please enter your "+ fname +"!</p>"; return false; } else { return true; } } } /*Check whole form for errors*/ function validate_form(thisform) { /*With the form script was executed from do the following...*/ with (thisform) { /*Checks if data has been entered into fields*/ if (validate(username,"Your username is required!")==false) { username.focus(); return false; } if (validate(password,"Your password is required!")==false) { password.focus(); return false; } } } |