Adding fraction with integers in JavaScript - javascript

Here is what my friend noticed while working on a project. He does not have any stackoverflow account so I am asking on behalf of my friend.
var a = 1.75/3;
so it gives
a = 0.5833333333333334
Now when I add 1 to variable a I get this:
1.5833333333333335
Notice the difference in the last digits
Similarly when I do following
0.5833333333333336+1
I get
1.5833333333333335
Now replacing the 6 with 7
it gives me
1.5833333333333337
I can't understand what is going here. Can anyone please explain this?

Related

Repeat a question using a "While or Do While Loop"

I want to prompt the user for a number. If the number is less than 10, I want to prompt the user to choose another number.
let question = Number(prompt("Please pick a number"));
console.log(question);
I started my code like the image above, this console logs the response as a number and not a string.
When I format the (while, or do while loop) I get problems and my console log starts running infinitely.
After researching this, there are two things that I don't understand,
Why is it running infinitely?
Secondly, I don't know how to get the prompt to repeat itself.
Thank you
I tried research W3, and other sites but couldn't find an example that included a prompt.
First of all thanks for responding, and here I think I can answer.
let question = Number(prompt("Please pick a number"));
while (question < 10){
question = Number(prompt("Please pick a number"))
}
I made a variable equal to a number value for my string
"Please pick a number."
In the while loop, it is self explanatory, the part that was confusing me earlier was not knowing what to write within the curly brackets. When I wrote question by itself, the computer froze.
Then after asking my teacher, she said to add another prompt.
So we added question = Number(prompt("Please pick a number")).
This repeated the loop and asked the same string while converting it to a number which allowed it to be checked again to see if the value is less than 10.
Thank you for helping me with my question.
try this code
let question = "Pick a number ";
let answer =Number(prompt(question));
while (answer < 10){
answer = Number(prompt(question));
}
Here I am storing the input value from the prompt to the variable named answer
while-loop will keep calling the prompt until the value of the answer is greater than or equal to 10
Hope this solves your problem

Google Scripts extracting a specific ID

I'm trying to figure out how to pull a unique identifier from a Gmail message. I've got the body pulled with:
function myFunction() {
var emails = GmailApp.search('label:company-email subject:"Specific Criteria" ',1,10);
for (x=0;x<emails.length;x++){
var thread = emails[x].getMessages()[0].getPlainBody();
var UID; // need this varible to be set as the extracted UID formatted UID### with 3 possibly 4 numbers. It will always have the same starting "UID"
}
}
UID: need this varible to be set as the extracted UID formatted UID### with 3 possibly 4 numbers. It will always have the same starting "UID"
Edit:
I have researched regex to see if I could get the data with a regular experession, however i'm in over my head there and I'm not understanding it.
I also tried indexOf(), but when I search the thread it gives me a -1. I tested to make sure I had indexOf() correct by testing it with a phrase and the UID, and it pulls when I have a regular string, just not from the body of the email. Maybe I'm missing a conversion somewhere?
Edit #2:
Here is a sample of the email being received:
Good afternoon user,
Please contact *Company Name* (UID123) (*City,State*) (*additional info*) at your earliest convenience.
Thank you,
I deleted the regex and am gonna be honest I don't remember exactly what I tested. was doing that late last night.
Edit 3:
turned out I was doing something wrong with regex. got an answer and I needed the following:
var UID = thread.match("UID[0-9]+"); worked like a charm.
Thank you guys for helping.
Use string#match
\d+ - one or more digits
const msg = `Good afternoon user,
Please contact *Company Name* (UID123) (*City,State*) (*additional info*) at your earliest convenience.
Thank you,`;
console.log(msg.match(/UID(\d+)/)[1])

how can i create a reg exp or array to change this?

ok guys i have been working on these, this is like my main project and im pretty anxious about it, i have been practicing but im still a newbie. My code is like this and it does what it has to do, but im thinking that it could be improved to be better and more reusable, sorry to spam if i am, i already asked on the spanish version of the website with no satisfactory answer, im new to web developing and to this site, i always read the content on this site to answer my questions but for this time i didnt know how to exactly use the previous answers to fix my code, since im new to web developing and im trying to use jquery bit by bit. As i said my question is how can i create an array or a reg exp that does all the things this code does? without having to use .replace function all those times
i have tried urlencode function, and tried to iterate over arrays on jquery but i still dont know how to do it properly.
$( ".linkbestia" ).each(function() {
lnk = $(this).text();
enlace= $(this).attr("href");
espacios=lnk.replace(" ","_");
maslimpio=espacios.replace("'","%27");
muchomaslimpio=maslimpio.replace("(","%28");
muchomuchomaslimpio=maslimpio.replace(")","%29");
nuevoenlace=$(this).attr("href",enlace+muchomuchomaslimpio);
});
the actual output is for example codedquote'replaced space as i said it already does what it has to do, but i know it can be improved, i hope you guys help me since in my country these kind of questions cant be answered without a ton of difficulties
what it does right now:
what the user writes would look like this
the result would look like this
If I understand correctly you want to take href from below:
<a class="linknpc" href="url/in/url/url/The%27White_Mob">
and expected output is
The'White_Mob
after you get the href and lets say the var enlace looks like below.
var enlace = "url/in/url/url/The%27White_Mob"
Below will first use String split on '/' to get all sections from href and from resultant array get the last element by pop() and use decodeURIComponent to decode the encoded uri.
var ans = decodeURIComponent(enlace.split('/').pop())
ans would now have the value: The'White_Mob
note: If the href ends with '/' then you need to adjust above solution accordingly

Java Script and RegEx

In application that I'am working on I have dynamically generated UI it is number of controls and listed entries depends on records from DB.What I'am trying to achieve is to write RegEx that would help me extract some numbers assigned as part of controls ID.
The way how I'am assigning IDs is : cText+(row that it represents in its group)+G+(group number).For example cText4G12
What I'am looking for is to get from that ID what Row it represents in example is 4 and it's group 12. I've never used RegEx before so I'am asking for your help :)
RegEx is not something that is easy to get your head around, so I would strongly recommend that you spend a bit of time to learn it - as it will be beneficial to you in the long run.
However, against my best judgement - here is code that will get you what you need..
var id = "cText4G12";
var result = id.match(/^cText(\d+)G(\d+)$/);
result[1] will contain "4"
result[2] will contain "12"
You don't really need regex. You can simply do:
var my_id = "cText4G12";
var row = my_id.slice(my_id.indexOf("cText")+5,my_id.indexOf("G"));
var group = my_id.slice(my_id.indexOf("G")+1);

Getting a NaN on a variable that is quite clearly a number

This is by far the strangest error I've ever seen.
In my program I have one variable called avgVolMix. It's a decimal variable, and is not NaN (console.log(avgVolMix) prints something like 0.3526246 to console). However, using the variable at all in an assignment statement causes it to corrupt whatever is trying to use it to NaN. Example:
console.log(avgVolMix); <- prints a working decimal
var moveRatio = 10 + avgVolMix * 10;
console.log(moveRatio); <- prints NaN
I seriously have no idea why this is happening. I've tried everything to fix it; I've converted it to a string and then back, rounded it to 2 decimal places, adding 0.0001 to it - nothing works! This is the only way I can get it "working" right now:
var temp = 0.0;
for(i = 0; i <= avgVolMix; i+=0.1)
temp = i;
This assigns a number that is close to avgVolMix to temp. However, as you can see, it's extremely bad programming. I should also note that this isn't just broken with this one variable, every variable that's associated with a library I'm using does this (I'm working on a music visualizer). Does anyone know why this might be happening?
Edit: I'm not actually able to access the code right now to test any of this stuff, and since this is a company project I'm not comfortable opening up a jsfiddle anyway. I was just wondering if anyone's ever experienced something like this. I can tell you that I got the library in question from here: http://gskinner.com/blog/archives/2011/03/music-visualizer-in-html5-js-with-source-code.html
If its showing the variable value as NaN. Then try converting the variable as parseInt(); method. Hope it works. Because I also faced such problem and solved when tried it.

Categories