My for in loop output fork fails - javascript

I'm looking at this "for in" tutorial and I don't understand why I can't get the loop to write the aProperty value instead of just its name.
http://www.tutorialspoint.com/cgi-bi...=javascript_15
I've tried:
document.write(navigator.aProperty);
document.write(navigator + . + aProperty);
And various other forms, all have failed.
If I just code
document.write(navigator.onLine);
Why can't I make the var, "aProperty" work as a document.write parameter?
Thanks!
<script type="text/javascript">
<!--
var aProperty;
document.write("Navigator Object Properties<br /> ");
for (aProperty in navigator)
{
document.write(aProperty);
document.write("<br />");
}
document.write("Exiting from the loop!");
//-->
</script>
</body>
</html>

aProperty is, indeed, just the key. to return the value you need to ask navigator for it.
Add this in your for...in loop:
for (aProperty in navigator)
{
document.write(navigator[aProperty]);
document.write("<br />");
}

Related

Basic Constructors - Entry Level Javascript

I cannot stop running into problems with this code. I figure I'm doing something monumentally wrong, and I figure asking people who know what they're doing would be the best thing to do.
The actual "problem" or assignment is to create a simple TV object. It has three characteristics. Make one. Call it. Display all the characteristics. Then it's done.
I declare variables and make a constructor. As per the rules of the assignment, both of these things must remain in the header of the document.
Next is the body. I want to create a new television object, called myTV, with those parameters. I create a function to recall the data back. Then I actively call the data back.
Somewhere along the lines I have logic problems, possible syntax problems, and who knows what else. My mind is reeling from staring at the same code for so long, so I'm going to grab something to drink and check back here in like 15. ugh.
<html>
<head>
<script type="text/javascript">
var manuTV;
var sizeTV;
var priceTV;
function Television(manu, size, price){
this.manuTV = manu;
this.sizeTV = size;
this.priceTV = price;
}
</script>
</head>
<body>
<script type="text/javascript">
var myTV = new Television("Sony", 52, 999);
function DisplayInfo(){
document.write("Television Manufacturer: " + this.manuTV + "<br />");
document.write("Size(in inches)" + this.sizeTV + "<br />");
document.write("Price:" + this.priceTV + "<br />");
}
myTV.displayInfo();
</script>
</body>
</html>
Your DisplayInfo method isn't related in any way to your Television. There are two easy things you can do to make it work. You can add DisplayInfo to Television's prototype.
Television.prototype.displayInfo = function(){
document.write("Television Manufacturer: " + this.manuTV + "<br />");
document.write("Size(in inches)" + this.sizeTV + "<br />");
document.write("Price:" + this.priceTV + "<br />");
}
This adds the method to Television's prototype so any Television can use it (tv.displayInfo()). You could also do it this way:
function displayInfo(tv){
document.write("Television Manufacturer: " + tv.manuTV + "<br />");
document.write("Size(in inches)" + tv.sizeTV + "<br />");
document.write("Price:" + tv.priceTV + "<br />");
}
Pass a Television object to DisplayInfo, (see the argument added to the method signature), then use tv instead of this. Then call the function like this:
displayInfo(myTv);

echo a javascript variable

What is the best way to echo the variable get_parestotal?
Some help please!
Thank you
<script type="text/javascript">
$(document).ready(function(){
var get_parestotal = 0
$(".parestotal").each(function(){
get_parestotal += parseFloat($(this).text());
});
alert(get_parestotal);
});
</script>
<? echo json_encode($get_parestotal); ?>
You can log variables and so on in the console.
e.g.:
console.log(get_parestotal);
You can even concatenate witht he use of +
console.log('This is your variable'+get_parestotal)
You can even add styling to your log (color, background-color,...):
console.log('%cThis is your variable'+get_parestotal+'!','color:green;');
There are some alternatives to console.log() you could use:
console.warn();
console.info();
console.error();
console.debug();
You can either do alert(myvar) or console.log(myvar).
Caveat: console.log is only supported in certain browsers...see the link.
if you want a console output than use console.log(get_parestotal);

get json specific value in javascript from json file

I have a json.json file like this
{"name1":"Hallo","name2":"Defy","name3":"Carm","name4":"Disney"}
And this script im using to read the file
<script type='text/javascript'>
$(window).load(function(){
$.getJSON("json.json", function(person){
$.each(person, function(key, value)
{document.write(key+"= "+value+"<br />");
});
});
});
</script>
This script is made to poin out all of the data but i need only "name3" value to be stored to +value+
How to do that?
You dont need to iterate then:
$.getJSON("json.json", function(person){
document.write("name3=" person.name3);
});
I'd probably stray away from document.write and append to a container.
<div id="test"></div>
$.getJSON("json.json", function(person) {
$("#test").append("<span> name3= " + person.name3 + </span>");
});
$.getJSON provides the JavaScript object that is represented by the JSON it retrieves.
To access the name3 property of the object, as with any JavaScript Object, you can simply do:
$.getJSON("json.json", function(person){
// do whatever you want with person.name3...
console.log(person.name3);
}
To access to the property you just have to use " . ", so to get the name3 you have to do value.name3
There's no no need to use a foreach if you only have 1 item
<script type='text/javascript'>
$(window).load(function(){
$.getJSON("json.json", function(person){
document.write(key + " = " + value.name3 + "<br />");
});
});
</script>

how to create a group of radio buttons with JavaScript?

Does anyone know why this simple program doesn't work?
It's a program that creates a group of radio buttons with Javascript.
<html>
<head>
<script>
function onWriteRadio(Valuse,numButtons,RadioName){
for(i=0;i<numButtons;i++){
document.write("<input type='radio' name=" + RadioName + "value=" +Valuse[i]+"/>");
document.write("<br/>");
}
}
</script>
</head>
<body onload="onWriteRadio([red,green,blue],3,'color')>
</body>
</html>
use this:
onWriteRadio(['red','green','blue'],3,'color')
string the array values. Currently, you say [red,green,blue], that means the variable red, variable green, variable blue, BUT you don't define them anywhere, so your program is saying "hmm, i do not know what red is.".. so string em.
put array values in quotes
onWriteRadio(['red','green','blue'],3,'color')
you left some quotation marks.
function onWriteRadio(values, radioName){
for (var i = 0; i < values.length; i++) {
document.write("<input type='radio' name='" + radioName + "' value='" +values[i]+"' >"+values[i]+" </ input>");
document.write("<br/>");
}
}
<html>
<body onload="onWriteRadio(['red', 'green', 'blue'], 'color')">
</body>
</html>
document.write() writes HTML expressions or JavaScript code to a document, which replaes your earlier html content.
document.write() executed after the page has finished loading will overwrite the page, or write a new page.
document.write() is a bad practice
place array values in quotes
add closing double quote for onload
make one string containing all inputs code and then write it
Your code should look smthng like this:
<html>
<head>
<script>
function onWriteRadio(Valuse,numButtons,RadioName){
var s = '';
for(i=0;i<numButtons;i++){
s += "<input type='radio' name=" + RadioName + "value=" +Valuse[i]+"/>"
}
document.write(s);
}
</script>
</head>
<body onload="onWriteRadio(['red','green','blue'],3,'color')">
</body>
</html>

using a string as the for loop expressions and condition

The following loop works:
<html>
<body>
<script type="text/javascript">
var i=0;
for (i=0;i<=5;i++)
{
document.write("The number is " + i);
document.write("<br />");
}
</script>
</body>
</html>
But the following doesn't:
<html>
<body>
<script type="text/javascript">
var i=0;
var x="i=0;i<=5;i++"
for (x)
{
document.write("The number is " + i);
document.write("<br />");
}
</script>
</body>
</html>
I'd just like to create a simple variable.
Please bear with me as I'm a newbie in JavaScript and let me know what I'm missing.
Let me provide my sample Google gadget:
<?xml version="1.0" encoding="UTF-8" ?>
<Module>
<ModulePrefs title="Sample Gadget" />
<UserPref name="order"
display_name="Results Order"
default_value="i = 0; i <= 5; i++" datatype="enum">
<EnumValue value="i = 0; i <= 5; i++" display_value="Ascending"/>
<EnumValue value="i = 5; i >= 0; i--" display_value="Descending"/>
</UserPref>
<Content type="html"><![CDATA[
<script type="text/javascript">
var i=0;
for (__UP_order__)
{
document.write("The number is " + i);
document.write("<br />");
}
</script>
]]></Content>
</Module>
It doesn't work because of the tags <> (they're not supported), and that's why I tried to define a variable for the EnumValue value.
When you say var x="i=0;i<=5;i++" you are creating a text string. This is not interpreted by JavaScript as you are expecting.
There is a definite difference between statements and text strings. Even though it looks to the eye like the same thing, it looks to the interpreter like a text string, like "hello" or "sdflkjsdflkjsdflj". JavaScript is not expecting a text string as loop parameters, it is expecting the three loop control parameters/statements. If you want to have a loop which starts and ends at different points, do something like this...
var i=0;
var start=0; //you can change the start position by changing this
var end=5; //and you can change the end also
for (i=start;i<=end;i++)
{
document.write("The number is " + i);
document.write("<br />");
}
In short: You're confusing code with data. "i=0;i<=5;i++" is data (a piece of text, a string). But when writing a for-loop you have to write initialization, condition and step as code - you cannot pass text that happens to look like the code you'd write there. (In fact, you don't want to - what should happen when the data isn't like valid code? Not to mention it's not needed - see El Ronnoco's)
Because x is a string and you cannot use for statement with a string inside.
If you need to change the upper bound of a for statement you can use a variable instead the fix number 5.

Categories

Resources