JavaScript - How To Initialize A Negative Value?
I am using
Code: var number = -141; later on when i use the variable number in another calculation, for example Code: var result = 450 + number; I get the result NaN. How can i rectify this? Thanks Similar TutorialsIf a negative number is entered into the reading variable it should be displayed as a zero. This works with the first calculations such as average but right at they very end the the alert wont display the highest average it just displays the one which has a negative number typed in to it. Code: var region=0 var reading=0 var average=0 var count=0 var readingSum=0 var catergory=0 var dry=0 var normal=0 var wet=0 var countWet=0 var countNormal=0 var countDry=0 var totalLoop=0 var numLocations=0 var maxAverage=0 var maxRegion=0 var maxReadings=0 var totalReadings=0 // ** Find out how many locations ** // numLocations=eval(prompt("How many locations are you taking readings for?")); // ** Loop for multiple entries ** // for(totalLoop=1; totalLoop<=numLocations; totalLoop=totalLoop+1) { // ** Reset total for loop ** // readingSum=0 // ** Get Region ** // region = prompt("Enter region please"); // ** Get Readings ** // totalReadings = eval(prompt("Enter readings please")); // ** Get readings and Prevent negative number ** // if(totalReadings<=0) { alert("Please enter valid reading") } else { alert("You entered " + totalReadings); } // ** Ask for indivdual readings ** // for(count=1; count<=totalReadings; count=count+1) { reading=eval(prompt("Enter reading please")); // ** Prevent negative number ** // if(reading<0) { reading=0 alert("Please enter valid reading") } // ** Work out Total of readings ** // readingSum= readingSum + reading } // ** Work out average ** // average= readingSum/totalReadings // ** Put average into a category and display everything ** // if(average<5) { catergory="dry" countDry=countDry+1 } else if(average>=5 && average<20) { catergory="normal" countNormal= countNormal+1 } else if(average>20) { catergory="wet" countWet=countWet+1 } alert("Location is " + region + "\nNumber of weakly readings taken is " + totalReadings + "\nTotal of readings is " + readingSum + "\nAverage is " + average + "\nLocation is " + catergory); // ** Find Highest average ** // if((average>maxAverage) || (totalReadings>maxReadings)) { maxAverage=average maxRegion=region } } // ** Final Report ** // alert("Number of locations is " + numLocations + "\nDry is " + countDry + "\nNormal is " + countNormal + "\nWet is " + countWet + "\nLocation with highest average reading is " + maxRegion + "\nHighest average is " + maxAverage); Do i need a while loop? i am only allowed to use while, for and if statements I'm just starting out learning and have sort of given myself a couple of basic problems to solve. I pretty quickly found something that's apparently hard to Google. I've made a page that shows a counter number, starting at 0, with buttons that will increase or decrease the number by 1 or 10. So I'd like to figure out how to show that number in red when it is a negative number and in green when it is a positive number. I've tried a couple of things, including an if... else, but here's my latest crack: Code: var counter=0; function countUp() { lastCounter=counter; counter++; display(); } function countDown() { lastCounter=counter; counter--; display(); } function tenUp() { lastCounter=counter; counter+=10; display(); } function tenDown() { lastCounter=counter; counter-=10; display(); } function lastNumber() { counter=lastCounter document.getElementById("number").innerHTML=lastCounter; } function display () { number=counter+''; switch(counter) { case counter== 0; document.getElementById("number").innerHTML=number.fontcolor(white); break; case counter> 0; document.getElementById("number").innerHTML=number.fontcolor(green); break; case counter< 0; document.getElementById("number").innerHTML=number.fontcolor(red); break; } //end switch } //end display I suspect that what I'm doing totally wrong has to do with how I'm describing the case or with calling one function inside another function, since it works up until I start messing with that (had the document.getElementByID stuff in the countUp, countDown, etc., functions before) but I don't know. Thanks in advance for any help you can offer. |