Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › javascript help with variable xD
New Posts  All Forums:Forum Nav:

javascript help with variable xD

post #1 of 5
Thread Starter 
Hey - working on an assignment and what I need to do is create a prompt for a value and then take that value and do two functions with it. So an example of what I'm trying to do is prompt for a value which I'll enter 2 and then multiple that by 2 and display the result. After that function I want to take the value I entered and multiple it by 6 and then display that function.

I was able to do it by declaring the variable twice but that doesn't make sense? When I declare the variable once it will take (2*2) and then multiply that by 6. When really I want to multiply 2 by 2 and then 2 by 6


Here is my code
Code:
function new()
{
   var number = prompt("Enter a number" , "0");
      {
         number = parseFloat(number) * 2;
         document.write(number + " is the result ");
      }
document.write("<br />");
      {
         number = parseFloat(number) * 6;
         document.write(number + " is the result ");
      }
}




This will take number and multiply it by two and then take the answer from that function and multiply it by 6. Is there a way were I can prompt once and take that value and multiply it by 2 and then multiply the same value entered by 6?
Obsidian
(16 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k Z68X-UD5-B3 Sapphire 6970 G.Skill 2x4GB 
Hard DriveHard DriveOptical DriveCooling
Crucial M4 Samsung F3 Something cheap Swftech Apogee HD 
CoolingCoolingCoolingOS
EK FC-6970V2 MCP 355 XSPC EX360 Windows 7  
MonitorPowerCaseAudio
Samsung XL2370 OCZ ModXStream Pro 700W Corsair Obsidian 800D Asus Xonar DG 
  hide details  
Reply
Obsidian
(16 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k Z68X-UD5-B3 Sapphire 6970 G.Skill 2x4GB 
Hard DriveHard DriveOptical DriveCooling
Crucial M4 Samsung F3 Something cheap Swftech Apogee HD 
CoolingCoolingCoolingOS
EK FC-6970V2 MCP 355 XSPC EX360 Windows 7  
MonitorPowerCaseAudio
Samsung XL2370 OCZ ModXStream Pro 700W Corsair Obsidian 800D Asus Xonar DG 
  hide details  
Reply
post #2 of 5
Thread Starter 
I think I figured it out by taking the var away and declaring number as a global variable. It is working - so I think I'm doing it right xD.
Obsidian
(16 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k Z68X-UD5-B3 Sapphire 6970 G.Skill 2x4GB 
Hard DriveHard DriveOptical DriveCooling
Crucial M4 Samsung F3 Something cheap Swftech Apogee HD 
CoolingCoolingCoolingOS
EK FC-6970V2 MCP 355 XSPC EX360 Windows 7  
MonitorPowerCaseAudio
Samsung XL2370 OCZ ModXStream Pro 700W Corsair Obsidian 800D Asus Xonar DG 
  hide details  
Reply
Obsidian
(16 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k Z68X-UD5-B3 Sapphire 6970 G.Skill 2x4GB 
Hard DriveHard DriveOptical DriveCooling
Crucial M4 Samsung F3 Something cheap Swftech Apogee HD 
CoolingCoolingCoolingOS
EK FC-6970V2 MCP 355 XSPC EX360 Windows 7  
MonitorPowerCaseAudio
Samsung XL2370 OCZ ModXStream Pro 700W Corsair Obsidian 800D Asus Xonar DG 
  hide details  
Reply
post #3 of 5
Quote:
Originally Posted by Zakel2 View Post

I think I figured it out by taking the var away and declaring number as a global variable. It is working - so I think I'm doing it right xD.

I'd avoid global variables as much as you can, whenever you can.

There are a few problems here, the first of which is that 'new' is a reserved word. Check out JavaScript's reserved word list.

I don't really know if you know what a 'function' is, as the above code only contains one function. If you were going to prompt outside of a function and allow the function to output the results, I'd go with:
Code:
function multiplyNumber(aNumber)  {
    document.write("Number multiplied by two: " + (aNumber * 2) + "<br />");
    document.write("Number multiplied by six: " + (aNumber * 6));
}


var number = prompt("Enter a number", "");
multiplyNumber(number);

You shouldn't need to use parseFloat() - instead you should check if the input is a number before giving it to the function with some simple error checking and re-prompt if an invalid input was entered.

The reason yours wasn't working, is you reassigned the 'number' variable to it's current value * 2, then multiplied that result by six. In fact, you can output the number like I did above without even modifying the value of the variable - the 'number' variable from the input is passed to the function and is multiplied when it is output. No reassignments required.
Edited by jNSK - 2/16/12 at 1:23am
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
post #4 of 5
Thread Starter 
Hmmm - that was a very poor explanation on my part. I was unaware that new was a reserved word xD. Anyway that was a really fast/sloppy example of what I was trying to accomplish.

Anyway - I got the ( single?) function to work while still declaring a variable. I guess I considered it multiple functions because I was trying to do two different things? Meh, I'm still a huge noobie lol. Also, I'm using parseFloat() because the numbers I'm using are fractional numbers.
The idea is that the parseFloat() will convert the prompt value from a string to usable numbers right?
Code:
function example()
{
   var number = prompt("Enter a number");
   document.write(number + " times 2 = " + parseFloat(number) * 2);
   document.write(number + " times 6 = " + parseFloat(number) * 6);
}
   
Obsidian
(16 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k Z68X-UD5-B3 Sapphire 6970 G.Skill 2x4GB 
Hard DriveHard DriveOptical DriveCooling
Crucial M4 Samsung F3 Something cheap Swftech Apogee HD 
CoolingCoolingCoolingOS
EK FC-6970V2 MCP 355 XSPC EX360 Windows 7  
MonitorPowerCaseAudio
Samsung XL2370 OCZ ModXStream Pro 700W Corsair Obsidian 800D Asus Xonar DG 
  hide details  
Reply
Obsidian
(16 items)
 
  
CPUMotherboardGraphicsRAM
i5 2500k Z68X-UD5-B3 Sapphire 6970 G.Skill 2x4GB 
Hard DriveHard DriveOptical DriveCooling
Crucial M4 Samsung F3 Something cheap Swftech Apogee HD 
CoolingCoolingCoolingOS
EK FC-6970V2 MCP 355 XSPC EX360 Windows 7  
MonitorPowerCaseAudio
Samsung XL2370 OCZ ModXStream Pro 700W Corsair Obsidian 800D Asus Xonar DG 
  hide details  
Reply
post #5 of 5
Quote:
Originally Posted by Zakel2 View Post

Hmmm - that was a very poor explanation on my part. I was unaware that new was a reserved word xD. Anyway that was a really fast/sloppy example of what I was trying to accomplish.
Anyway - I got the ( single?) function to work while still declaring a variable. I guess I considered it multiple functions because I was trying to do two different things? Meh, I'm still a huge noobie lol. Also, I'm using parseFloat() because the numbers I'm using are fractional numbers.
The idea is that the parseFloat() will convert the prompt value from a string to usable numbers right?
Code:
function example()
{
   var number = prompt("Enter a number");
   document.write(number + " times 2 = " + parseFloat(number) * 2);
   document.write(number + " times 6 = " + parseFloat(number) * 6);
}
   

Ah yes, if you want to use fractional numbers then you'll need parseFloat() in there. I assumed you were only using round numbers (try to include as much details as possible!)

No worries, we've all been a noobie at one stage. You'll learn quickly. What you have above is one function. It looks fine, but don't forget to include the
Code:
<br />

in your first document.write, or the output will look messy!

I usually make functions only do the calculations rather than prompt the user as well. You then pass the variable into the function, as per the example I showed you above. By doing this, the flow of your program is easier to read because you can easily see that, with the use of an aptly-named function, the program is prompting for an input then doing a calculation (within the function) using the value given in the prompt. While this isn't a problem for a small application such as yours, it's good practice to get into because when you get into doing large projects like I do (by large, I mean 1000s of lines), ensuring the flow of the application is readable is key to being able to work as part of a team if other people are going to be reading and working with your code.

All the best, happy learning!
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
Project 4
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5 750 Lynnfield @ 4.4 GHz, 1.41v EVGA P55 SLi (132-LF-E655-KR) - A72 BIOS EVGA GeForce GTX 680 4GB G.Skill Ripjaws @ 9-9-9-24, 1.51V 
Hard DriveOptical DriveOSMonitor
80GB Intel X25-M SSD + 500GB WD + 2TB Samsung F3 Samsung Super Writemaster DVD+-R Windows 7 Ultimate Edition Samsung SyncMaster 2433 24" 
KeyboardPowerCaseMouse
Logitech G15 Corsair TX750 750W Corsair Obsidian 800D + Scythe Kaze Master Ace Razer Deathadder Respawn - Black 
Mouse Pad
Steelseries QcK Medium - Black 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › javascript help with variable xD