|
Use the sign function the values of which you can use in your switch statement:
var nums = new Array(-13,2,67,3,0,56,12,-41);
var len = nums.length;
var positives = 0;
var negatives = 0;
var zeroes = 0;
for (i=0; i<len; i++) {
counter(num[i];
}
function counter(number) {
var sign = (number > 0) ? 1 : (number < 0) ? -1 : 0;
switch (sign) {
case 1:
positives++
break;
case 0:
zeroes++;
break;
case -1:
negatives++;
break;
};
Regards,
— Manfred
"I had the right to remain silent, but I didn't have the ability!"
Ron White, Comedian
|
|
|
|
|
I am currently a college student so please be kind to me being a novice! I am working on an assignment and I just can't figure out where I am going wrong.
Assignment Details:
Function: tst_name
Parameter: A string.
Returns: true if the given string has the form
String1, String2 letter
where both strings must be all lowercase letter except for the first letter and letter must be uppercase; false otherwise
My JavaScript file:
function tst_name(name) {
var ok = name.search(/^[A-Z][a-z]+, [A-Z][a-z]+, [A-Z]\.?$/);
if (ok == 0) {
return true;
} else {
return false;
}
}
var tst = tst_name(Woods, Trista, M);
if (tst) {
document.write("Program error <br />");
} else {
document.write("Valid name. <br />");
}
var tst = tst_name(woods, trista, m);
if (tst) {
document.write("Invalid name <br />");
} else {
document.write("Program error <br />")
}
My HTML file:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"
>
<html lang="en">
<head>
<title>Woods | Test Name | Exercise 4.12</title>
<script type="text/javascript" src="exercise4-12.js">
</script>
</head>
<body>
<script>
document.write("Woods, Trista, M: " + tst + "<br />");
document.write("woods, trista, m: " + tst + "<br />");
</script>
</body>
</html>
When I view this in a browser it just shows:
Woods, Trista, M: undefined
woods, trista, m: undefined
So I'm just confused. I followed an example in our book and I just don't know what I need to do in order to get the variables to write the correct statements in my HTML document.
|
|
|
|
|
Your test script is attempting to pass three variables (which don't exist) named Woods, Trista and M. Ditto for the lower case as well.
surround them with quotes. "Woods, Trista M"
|
|
|
|
|
Oh geez! It's always the simple things that screw me up. I also noticed I was missing a semicolon after one of my statements too.
Thanks for helping a beginner out!
|
|
|
|
|
tristarterror wrote: Thanks for helping a beginner out!
No problem.
Semicolons in javascript are a bit of a nuisance because there are some nasty surprises with return statements in particular.
Two books you definitely ought to have handy. Javascript the Definitive Guide 5th edition (O'Reilly), and Douglas Crockford's Javascript The Good Parts (O'Reilly).
The language is a b*stard of a mess. Even the original 'official' language specification is crap.
|
|
|
|
|
hi experts, i need help about skype in my webpage.
i have a textbox client-side, in the webpage
<input type="text" id="phoneSkype_ws" />
<input type="button" onclick="return call();" value="Call!" />
when i click on call button, i need to call by skype, the same number in the textbox, but i don't know how!!!
with the following code, i'm not able to do this
<div id="skypeButton">
<script type="text/javascript">
var num = $("#telefono").val();
Skype.ui({
"name": "call",
"element": "skypeButton",
"participants": ????,
"imageSize": 32
});
</script>
</div>
hope someone can help me!!
|
|
|
|
|
the new windows 8 msn site (t.msn[^] has links that depress on mouse-down. Does anyone know how I can get source javascript that achieves the same effect?
TIA
bart
|
|
|
|
|
I think you could have the same effect by applying the CSS pseudo-class for the anchor.
Below is the example from W3Schools
a:link {color:#FF0000;}
a:visited {color:#00FF00;}
a:hover {color:#FF00FF;}
a:active {color:#0000FF;}
active defines the effect on mouse down event.
|
|
|
|
|
Oh, thanks. Its actually the way the link is depressed that I was interested in..
bart
|
|
|
|
|
i have problem to display info of google maps, when click in outside of map. but if inside of map when i click a marker, info of google maps can display.
function onload() {
var st=getURLParameters("man");
var kd_link=getURLParameters("kd");
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var dt="";
xmlhttp.open("GET","hasil.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var tbl=xmlDoc.documentElement.getElementsByTagName('page');
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 15,
center: new google.maps.LatLng(-7.8711188835883945, 112.52471923828125),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var gmarkers=[];
var marker, i;
function Linkclicked(i) {
GEvent.trigger(gmarkers[i], "click");
}
var kde;var nm;var l1;var l2;var bk;
for (i = 0; i < tbl.length; i++) {
kde=xmlDoc.getElementsByTagName("kode")[i].childNodes[0].nodeValue;
nm=xmlDoc.getElementsByTagName("nama")[i].childNodes[0].nodeValue;
l1=xmlDoc.getElementsByTagName("lat")[i].childNodes[0].nodeValue;
l2=xmlDoc.getElementsByTagName("long")[i].childNodes[0].nodeValue;
bk=xmlDoc.getElementsByTagName("bk")[i].childNodes[0].nodeValue;
marker = new google.maps.Marker({
position: new google.maps.LatLng(l1, l2),animation:an, icon:bk,map: map
});
google.maps.event.addListener(marker, 'click',(function(marker, i) {
return function() {
infowindow.setContent(xmlDoc.getElementsByTagName("nama")[i].childNodes[0].nodeValue);
infowindow.open(map, marker);
}
})(marker, i));
gmarkers[i]=marker;
}
}
that's my code, when i try to call one of the google maps info marker .
<a href="javascript:Linkclicked('0')">try</a>
it couldn't be displayed... please give me solution??
|
|
|
|
|
|
thanks i had solution about this. i used trigger at google maps.
|
|
|
|
|
I wrote this script some time ago, it works fine on PC's, but on mobile devices, I need to optimize it and get it to run faster in speed.
So like on a iPhone 4, it takes about 3 seconds to run.on a PC it takes 1 second, which is slow.
Just looking for ideas or tips, or another set of eyes to check it out.
1. Confirms a value in the textbox, if not, textbox turns pink, and the error arrow shows
2. checks if it's a number
3. increments value.
function jquery_Add2Cart_Numeric_Up() {
var vFlag = true;
var txtFocus = $('[id*="_txt_Focus_Field"]').val();
var txtError = $('[id*="_txt_Error_Field"]').val();
var qty_value = $('[id*="_txt_Modal_Add2Cart_SelectQty"]').val();
if (qty_value.length < 1) {
$('[id*="_txt_Modal_Add2Cart_SelectQty"]').css('background-color', txtError);
vFlag = false;
}
else if (qty_value.length > 0) {
$('[id*="_txt_Modal_Add2Cart_SelectQty"]').css('background-color', txtFocus);
var re_Qty = new RegExp("^([1-9]|[1-9][0-9]|[1-9][0-9][0-9])$")
var txt_Qty = $('[id*="_txt_Modal_Add2Cart_SelectQty"]').val();
var match_Qty = re_Qty.exec(txt_Qty);
if (match_Qty == null) {
$('[id*="_txt_Modal_Add2Cart_SelectQty"]').css('background-color', txtError);
$('[id*="_lbl_Modal_Add2Cart_Center_Container_QtyMessage"]').text("Invalid Quantity!");
$('[id*="_txt_Modal_Add2Cart_SelectQty"]').val("1");
vFlag = false;
}
else if (match_Qty !== null) {
$('[id*="_txt_Modal_Add2Cart_SelectQty"]').css('background-color', txtFocus).show();
$('[id*="_lbl_Modal_Add2Cart_Center_Container_QtyMessage"]').text("Select your quantity:");
var qty = parseInt($('[id*="_txt_Modal_Add2Cart_SelectQty"]').val());
if (qty < 1000) {
qty ++
$('[id*="_txt_Modal_Add2Cart_SelectQty"]').val(qty);
}
}
}
return false;
}
|
|
|
|
|
Jeepers, in 6-7 secs on a 650Mhz android phone, I can generate an image thats 256x256, with 2 calls to _each_ of square root, sin and cos - perPixel.
Something I notice is that you've got many instances of $('[id*="_txt_Modal_Add2Cart_SelectQty"]') in your code. I'd try just evaluating that statement once, before then using it later - much like you did for the css rules.
I dont jQuery, I apologise if the syntax is incorrect.
In any case,the point is - evaluate expensive things as few times as possible.
In my code, this could be done by pre-computing sin cos and sqrt tables. In yours I'd assume you could evaluate the statement I've mentioned. I expect it takes jQuery some time to parse the statement before finding the element that you wish to target.
I.e
var txtModalSelectQtyEl = $('[id*="_txt_Modal_Add2Cart_SelectQty"]');
...
...
var qty_value = txtModalSelectQtyEl.val();
...
...
txtModalSelectQtyEl.css('background-color', txtFocus);
..
..
Make it work. Then do it better - Andrei Straut
|
|
|
|
|
I didn't notice the numerous calls to the qty textbox.
What I did think of, is that perhaps I should just assume that the value is correct or an integer, and if not, then run the rest of the script. It's really fast now, and does not hesitate anymore.
On the iPhone, it was a matter of making the increment buttons bigger for a more positive contact with the finger or thumb.
Did I mention that it's just a webpage running on a phone?
Love the feedback though, this function has been giving me trouble for quite a while now, if you think my new idea is bad, just let me know.
[Edit]
Hey Eric, just saw your name on the post. Let me know what you think of the new design below, if it sucks, please let me know so I can work on it again. Thanks Jim
function jquery_Add2Cart_Numeric_Up() {
try {
var qty = parseInt($('[id*="_txt_Modal_Add2Cart_SelectQty"]').val());
if (qty < 1000) {
qty++;
$('[id*="_txt_Modal_Add2Cart_SelectQty"]').val(qty);
}
}
catch (err) {
var vFlag = true;
var txtFocus = $('[id*="_txt_Focus_Field"]').val();
var txtError = $('[id*="_txt_Error_Field"]').val();
var qty_value = $('[id*="_txt_Modal_Add2Cart_SelectQty"]').val();
if (qty_value.length < 1) {
$('[id*="_txt_Modal_Add2Cart_SelectQty"]').css('background-color', txtError);
vFlag = false;
}
else if (qty_value.length > 0) {
$('[id*="_txt_Modal_Add2Cart_SelectQty"]').css('background-color', txtFocus);
var re_Qty = new RegExp("^([1-9]|[1-9][0-9]|[1-9][0-9][0-9])$")
var txt_Qty = $('[id*="_txt_Modal_Add2Cart_SelectQty"]').val();
var match_Qty = re_Qty.exec(txt_Qty);
if (match_Qty == null) {
$('[id*="_txt_Modal_Add2Cart_SelectQty"]').css('background-color', txtError);
$('[id*="_lbl_Modal_Add2Cart_Center_Container_QtyMessage"]').text("Invalid Quantity!");
$('[id*="_txt_Modal_Add2Cart_SelectQty"]').val("1");
vFlag = false;
}
else if (match_Qty !== null) {
$('[id*="_txt_Modal_Add2Cart_SelectQty"]').css('background-color', txtFocus).show();
$('[id*="_lbl_Modal_Add2Cart_Center_Container_QtyMessage"]').text("Select your quantity:");
}
}
}
return false;
}
|
|
|
|
|
Ah, that looks a little less processor intensive. I continually forget about try-catch blocks myself.
Thanks for the reminder Jim.
Though I do notice, if the user passes a non-integer, the code will still try to parse it as an int again. It will also make the repeated calls to jQuery to evaluate the statement I mentioned earlier. But as always, less optimization is often better than more - since readability is often the casualty. Optimization is good - where necessary. That's why we only write asm when we have to etc.
I probably shouldn't... but here goes:
At a job I worked at once, I'd often have a cigarette with one of the guys from the English Learning Center. Somehow, he'd remembered my name as David. I considered a couple of times correcting him, but in the end, figured - we've no reason to communicate in an official capacity at work, nor did we have any reason to discuss one-another during the course of normal duties. I couldn't see it as really mattering.
So, this went on for about 2 1/2 years or so. In my last week there, one of the girls let the cat out of the bag and called me Simon. Somewhat shocked and incredulous, he asked why I'd never corrected him. I simply told him that he'd been consistent - he'd called me David for as long as I could remember. Clearly I was identified as being me - did it really matter what he called me? We had a good laugh and decided that it really didn't matter.
Boy did we have a good laugh over that one.
Make it work. Then do it better - Andrei Straut
|
|
|
|
|
ha ha ha
I forgot it was Simon;
I don't know what made me think of Eric.
I think I know, it's the enh in your handle, Eric just popped up in my head for some reason.
Thanks for pointing out that extra parseInt, I'll take care of it right now.
That should do the trick for now. Thanks for the eyes on the script!
Jim
|
|
|
|
|
It's a pleasure as always, Jim.
Make it work. Then do it better - Andrei Straut
|
|
|
|
|
I thought I would get back to playing with KnockoutJs in a simple HTML and JS environment using JQuery to access JSON data from Web Services.
My question is, how do/would you set up the various JS files and folders in your web application?
I mean, physically, do you create a single JS file per ViewModel and a single HTML file for the view ? Do you put them in the same folder, or have everything at the root?
I'm not a web developer, so be gentle - use shot words
Also, does anyone have experience using Typescript with KnockoutJs. I'm just about to start and would appreciate any help/advice.
MVVM # - I did it My Way
___________________________________________
Man, you're a god. - walterhevedeich 26/05/2011
.\\axxx
(That's an 'M')
|
|
|
|
|
_Maxxx_ wrote:
My question is, how do/would you set up the various JS files and folders in your web application?
My own JS goes into "scripts" folder. The rest, JQuery, Knockout etc. are fetched from CDNs.
_Maxxx_ wrote: do you create a single JS file per ViewModel and a single HTML file for the view ?
Nope... but some say to do so.
_Maxxx_ wrote: using Typescript with KnockoutJs
I'ven't use Knockout yet, Typescript it's good... atleast from the documentation, I'm also starting to use it. It's a compiler that compiles typescript into javascript. Write your code, feed it to the compiler and use the generated JS in your website. Easy Peasy!
|
|
|
|
|
Amitosh Swain wrote: My own JS goes into "scripts" folder. The rest, JQuery, Knockout etc. are fetched from CDNs.
Yep - I do that - but I wasn't sure about the VMs created for Knockout - whether they go with the scripts, in a subfolder of scripts, or with the html 'views'
The TypeScript idea is great (I actually thought about doing something like it myself but MS must have been tapping my thoughts )
What I am trying to figure out is exactly how to write a Knockout VM using TypeScript "properly"
MVVM # - I did it My Way
___________________________________________
Man, you're a god. - walterhevedeich 26/05/2011
.\\axxx
(That's an 'M')
|
|
|
|
|
_Maxxx_ wrote: Knockout VM using TypeScript "properly"
Knockout + Typescript... Good Idea, but never tried it.
|
|
|
|
|
_Maxxx_ wrote: I mean, physically, do you create a single JS file per ViewModel and a single HTML file for the view ? Do you put them in the same folder, or have everything at the root?
I've used both Knockoutjs and Typescript in a project. I kept my jquery knockout etc in the Scripts folder, but then created a folder structure that matched the namespaces of my project.
At that point, if I were working on controls for the application, I would create all my VM's and HTML files separately but keep them together in that folder.
_Maxxx_ wrote: Also, does anyone have experience using Typescript with KnockoutJs. I'm just about to start and would appreciate any help/advice.
Typescript is a great tool for developing javascript. As a C# developer initially, it made the transition to javascript rather painless. The features in the latest release which include being able to develop with generics has the potential to greatly reduce the amount of code needed to be written by hand for a project.
Typescript also has ambient declaration files that support a wide range of javascript libraries. You can find them all here.
Hope this helps!
"In the middle of difficulty lies opportunity." Albert Einstein
|
|
|
|
|
Payrok wrote: Hope this helps!
Thanks - yes it does - following that link right now!
MVVM # - I did it My Way
___________________________________________
Man, you're a god. - walterhevedeich 26/05/2011
.\\axxx
(That's an 'M')
|
|
|
|
|
hey guys.
I m looking for 1 or 2 people to start creating a RPG online game with HTML5, nodejs and any other technology that is needed.
the thing i have in mind is a 3d game with low res but good looking game like http://www.playtankworld.com/
three js is awesome but i dont know if it's just in a point that such a game can be created with.
the alternative idea is a classic 2d sprite base game.
i have really good ideas in mind so it can be really awesome.
any suggestion, help will be appreciated.
lets be the first.
|
|
|
|
|