Jump to content

I know im making a rookie mistake but...

steVENOM
Go to solution Solved by minibois,

You are essentially asking the user for text input, so the code interprets this as plain text.

You have to make these either int or float. Float being a number with and int without decimals. You use them like this:

myNumberVar = parseInt(yourVariable);
myNumberVar2 = parseFloat(yourVariable2); 

After doing that piece of code, your can add up your variables.

 

of course once your user enters anything but a number, the system will crash though.

I know im making a rookie mistake but, everyone has to start somewhere, and I’ve learned how to avoid this issue before but I just cant seem to rememeber how to fix it. Essentially my code in JavaScript is supposed to take input of 2 numbers and then add them together, but it takes the numbers and just lists them together. Again I know this is a common beginner mistake and I’ve done it right before but I cant remember what I’m doing wrong here. I know the variables arent being taken as strings so I’m a bit confused. What do I need to change to make my code take the 2 variables and add them together to display the output as a sum rather than a string of the 2 numbers? The inputs I used in this example of course are storage1 being 10 and storage2 being 10 as well. 

E5CE85BC-8A62-4963-A226-550AA1730BE5.jpeg

Here are my specs:

  • CPU: Intel - Core i7-8700K 6-Core Processor OC @ 5.0ghz
  • Cooler: Cooler Master - Hyper 212 EVO 82.9 CFM Sleeve Bearing CPU Cooler
  • Motherboard: Asus - Prime Z370-A ATX LGA1151 Motherboard
  • Storage: Western Digital - WD Blue 1TB 2.5" 5400RPM Internal Hard Drive
    • Samsung - 850 EVO-Series 250GB 2.5" Solid State Drive
    • WD 4TB Black - Passport
  • RAM: Corsair Vengence RGB 2x8 RAM
  • Graphics Card: MSI - GeForce GTX 1080 Ti 11GB Founders Edition Video Card
  • Case: Corsair Crystal 460x
  • Power Supply: Corsair - CXM 650W 80+ Bronze Certified Semi-Modular ATX Power Supply
  • Operating System: Microsoft - Windows 10 Home OEM 64-bit
  • Monitor: Dell - S2417DG 23.8" 2560x1440 165Hz Monitor
  • Keyboard: Corsair K70 Mk II
  • Mouse: Corsair - SCIMITAR PRO RGB Wired Optical Mouse
  • Headphones: Bose QC 35 II - Active Noise Cancelling Headphones
     
Link to comment
Share on other sites

Link to post
Share on other sites

Ah the joys of not having strictly typed variables.  It seems convenient and fun until stuff like this happens and causes no end of confusion.

But yeah, the issue is it's treating them like strings.  You'll have to convert to an integer or other number.

Solve your own audio issues  |  First Steps with RPi 3  |  Humidity & Condensation  |  Sleep & Hibernation  |  Overclocking RAM  |  Making Backups  |  Displays  |  4K / 8K / 16K / etc.  |  Do I need 80+ Platinum?

If you can read this you're using the wrong theme.  You can change it at the bottom.

Link to comment
Share on other sites

Link to post
Share on other sites

try

 

var storageTotal = storage1 + storage2;

alert(“the total equals” + storageTotal);

 

sorry if syntax is wrong, i did something like this a while back, i think that’s how i did it

✧・゚: *✧・゚:*  Quote for a reply  *:・゚✧*:・゚✧

 

✧・゚: *✧・゚:*   Ask for discord   *:・゚✧*:・゚✧

Link to comment
Share on other sites

Link to post
Share on other sites

You are essentially asking the user for text input, so the code interprets this as plain text.

You have to make these either int or float. Float being a number with and int without decimals. You use them like this:

myNumberVar = parseInt(yourVariable);
myNumberVar2 = parseFloat(yourVariable2); 

After doing that piece of code, your can add up your variables.

 

of course once your user enters anything but a number, the system will crash though.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

Yeah, like everyone else has said, you have to use parseInt() to convert those numbers into ints. Right now it's just treating them like strings, like if I did alert("happy " + "birthday);

Specs: CPU: AMD Ryzen R7 3700X @4.4Ghz, GPU: Gigabyte RX 5700 XT, RAM: 32 GB (2x 8GB Trident Z Royal + 2x 8GB TForce Vulkan Z) @3000Mhz, Motherboard: ASRock B550m Steel Legend, Storage: 1x WD Black 1Tb NVMe (boot) + 1x Samsung 860 QVO 1Tb SSD (storage), Case: Thermaltake Core V21, Cooler: Noctua NH-D15

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, Sayori said:

Yeah, like everyone else has said, you have to use parseInt() to convert those numbers into ints. Right now it's just treating them like strings, like if I did alert("happy " + "birthday);

Ooohhh that makes sense! Thank you so much guys! My instructor only taught us Number(“Enter string here”) which I didnt think would work in this situation, please let me know if that would work as well and at all possible how to use number concatination

4F898BAA-40C1-41AA-8729-7F21C452A74D.jpeg

Here are my specs:

  • CPU: Intel - Core i7-8700K 6-Core Processor OC @ 5.0ghz
  • Cooler: Cooler Master - Hyper 212 EVO 82.9 CFM Sleeve Bearing CPU Cooler
  • Motherboard: Asus - Prime Z370-A ATX LGA1151 Motherboard
  • Storage: Western Digital - WD Blue 1TB 2.5" 5400RPM Internal Hard Drive
    • Samsung - 850 EVO-Series 250GB 2.5" Solid State Drive
    • WD 4TB Black - Passport
  • RAM: Corsair Vengence RGB 2x8 RAM
  • Graphics Card: MSI - GeForce GTX 1080 Ti 11GB Founders Edition Video Card
  • Case: Corsair Crystal 460x
  • Power Supply: Corsair - CXM 650W 80+ Bronze Certified Semi-Modular ATX Power Supply
  • Operating System: Microsoft - Windows 10 Home OEM 64-bit
  • Monitor: Dell - S2417DG 23.8" 2560x1440 165Hz Monitor
  • Keyboard: Corsair K70 Mk II
  • Mouse: Corsair - SCIMITAR PRO RGB Wired Optical Mouse
  • Headphones: Bose QC 35 II - Active Noise Cancelling Headphones
     
Link to comment
Share on other sites

Link to post
Share on other sites

30 minutes ago, steVENOM said:

Ooohhh that makes sense! Thank you so much guys! My instructor only taught us Number(“Enter string here”) which I didnt think would work in this situation, please let me know if that would work as well and at all possible how to use number concatination

 

Not sure if you are asking how to do concatenation.. But I think you do, so I'm just gonna show you an example. If the user enter the numbers 1 and 6 respectively, for storage1 and storage2, these two programs ways of show the total will do two different things:

//Javascript code

var storage1 = parseInt(prompt("Enter num1"));
var storage2 = parseInt(prompt("Enter num2"));


//This will show 7. 
//The parenthesis around the two variables will make it add them up.
alert("The total is: " + (storage1+storage2));

//This will show 16. 
//The lack of parenthesis means I just want to display these 
//variables without doing anything else with them.
alert("The total is: " + storage1+storage2);

 

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

7 hours ago, steVENOM said:

Ooohhh that makes sense! Thank you so much guys! My instructor only taught us Number(“Enter string here”) which I didnt think would work in this situation, please let me know if that would work as well and at all possible how to use number concatination

 

 

Number will attempt to parse the whole input as a number, which may be also float, not just an int. parseInt parses the string for a number up to the first nonnumeric character. For example Number('123x') would return NaN (not a number) while parseInt('123') would return 123. If you're only ever going to input an integer they will return the same value. You could also use unary + to convert a string to an int (or float). For example, +'3'  would return 3.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×