JavaScript - Converting A String (feet - Inches) Into A Number
I have an Adobe pdf fill-able form field that requires a distance computation using javascript. One of the values needed in the computation is in the format of a string as feet - inches (for example: 4'-10"). I need to convert this into a number to use to complete a computation. Does anyone have a code snippet or any ideas on how to do this. Any assistance is greatly appreciated.
Thanks. Similar TutorialsI'm not too sure how to approach this but I have a date input that uses a jQuery pop-up for a calendar, and enters the date in a format d MMM yy (1 Jan 11). Not everyone wants to be clicking and choosing the date from the calendar but to be entering it just as a number such as 0101, 010111 or 01012011 which can be faster. This would then need to be converted to the d MMM yy format as per the jQuery pop-up when they exit the input box. So really only if it's a number format it needs to look at the number of digits... and does the conversion. Has anyone done something like this before? Or is there a link to how I could go about achieving this? Many thanks David When I used toFixed() method on a number, I thought that this method round a number to a specified approximation, but I got a surprising result, the number became string! 15.23689 .toFixed ( 2 ) ==> "15.24" So does it convert the number into string? Hi all, I am having a problem converting a physical string to a integer. I have a dropdown list of a few items but are numbers though in chracters. I ned to output the value and the text of the selected item. As I cannot use a if-else statement, any help would be great. Code: <select id="std_num" onchange="displayValue()"> <option value="Two">Two</option> <option value="Zero">Zero</option> <option value="Five">Five</option> <option value="Three">Three</option> <option value="Two">Two</option> <option value="Zero">Zero</option> </select> Code: function displayValue() { alert(document.getElementById('std_num').value); var c = (document.getElementById('std_num').value); } Hi, I have a javascript program that currently takes the contents of a *.txt file and converts it to a string-type variable using a hidden iframe. For my pruposes it would be much nicer to read a *.csv file instead, however the method I'm using creates the "open/save file" pop-up window. All files are client-side and in the same folder on one computer; this is NOT server-side or internet-based. It is simply javascript used in an HTA file as programming code. The *.csv does not need to be edited, just read. I need to know how to convert the text of this *.csv file into a string variable. I have heard of using "xmlHTTPRequest (AJAX)" but am not familiar with how to use this. Any advice or code would be greatly appreciated. First off, I would like to thank everyone who has offered their help so far in my little self-appointed project dealing with astral body locations. I finally have the script working the way it was intended to and am looking at cleaning it up at this point. One of the things I would like to do in cleaning it up is change the way values are inputed when converting from Polar Coordinates to Cartesian Coordinates. The script is currently set up such that Right Accension is input in three fields identified as Hours, Minutes and Seconds. By the same token, the Declination Angle is input through three fields identified as Degrees, Minutes and Seconds. What I would like to do at this point is convert each set of three input fields to a single input field and allow the user to input the values as; Right Accension = 13:23:15.2 Declination Angle = 22:17:33 I understand that these would be inputed as String values and am familiar enough with the Split feature to extract the individual pieces of string (in this instance they would be 13, 23, 15.2 and 22, 17 and 33). What I am not familiar with, however, is converting those string values into numbers. I have done a bit of research into it and haven't really found anything that makes sense to me just yet, so if anyone can offer a bit of guidance, I am all ears. Not looking for anyone to specifically write the code for me, but if one of you fine folks could point me in the right direction as far as tutorials go, I would be thankful. GW Hello, I'm very new to javascript (OK...DAY 1) and I can't seem to even do the most simplest of procedures. All, I'd like to do is replace "sdfsdf" with "New Stuff". I can't figure out for the life of me why my code won't work and any help would be super appreciated. Thank you... Code: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> <script type ="text/javascript" src="../../js/jquery.js"></script> <script type="text/javascript"> $('#quizResults').html('<p>New stuff</p>'); </script> </head> <body> <div id="quizResults"><p>sdfsdf</p></div> </body> </html> Hi, I have a string stored in a variable var = "4.25 houses". I would like to only extract the currency value, ie "4.25" from this. Is this possible? Also I would like to divide and multiply the new variable I get... does this number need to be converted or something? Thanks in advance! I need to check if a var (lessonnum) is a string that cannot be converted to a number, i.e. "fred" "george" "catalyst" etc. but not "5" "456" or "34564" etc.: Code: var lessonnum = readCookie('lesson'); if ((intro != "12")||((lessonnum != "A STRING THAT COULD BE A NUMBER"))){ do sth. } How could I do that? A week or so ago Old Pedant and I discussed methods of converting string inputs to numbers. I speculated that *1 might be quicker. I have now run a test:- Code: <script type = "text/javascript"> var start = new Date().getTime(); for (var i =0; i <100000; i++) { var a = "2"; a = a *1; } var end = new Date().getTime(); var diff1 = end - start; alert (diff1); // 109 var start = new Date().getTime(); for (var i =0; i <100000; i++) { var a = "2"; a = parseInt(a); } var end = new Date().getTime(); var diff2 = end - start; alert (diff2); // 140 var start = new Date().getTime(); for (var i =0; i <100000; i++) { var a = "2"; a =Number(a); } var end = new Date().getTime(); var diff3 = end - start; alert (diff3); // 140 var start = new Date().getTime(); for (var i =0; i <100000; i++) { var a = "2"; a = +a; } var end = new Date().getTime(); var diff4 = end - start; alert (diff4); // 109 </script> The results vary slightly with each run, but the conclusion is that there is no real difference between the methods - except for a = a*1 and a = +a which consistently comes in lower than parseInt() or Number(). But the differences are too small to be significant. I have a string of text, and starting from a particular spot (known to be a number), I want to find the length of that number. Here's what I have: Code: ... var start = [...predetermined...] var end = 0; while (!isNaN(sourceCode.charAt(start + end))){ endCurrent++; } var myNum = sourceCode.substr(start, end) * 1; So let's say var sourceCode = "alkdjabjasdsdf-53 dnalsdb..."; , startCurrent will already be at the "5" and I want to be able to extract the "53". What I have works, but it seems cumbersome... Any advice? I just noticed that my pseudo-streaming movie code causes an error with IE7. I works fine with IE8, FireFox, Chrome... I have been looking on my own and found that it's likely a misplaced comma in my code, the other browser are debugging it, but IE7 will not. The error suggests it's where I have indicated below. I've tried removing the comma just after "about_us.jpg" and that causes the script to fail completely. Can anyone help me here, please? Code: <script type="text/javascript"> //<![CDATA[ flowplayer("player", {src: "http://www.mysite.org/flowplayer/flowplayer.commercial-3.2.5.swf", wmode: 'opaque'}, { // product key from your account key: '#$xxxxxxxxx', // configure clip to use "lighthttpd" plugin for providing video data playlist: [ { url: 'http://www.mysite.org/media/video/about_us.jpg', }, //<----error suggest here??? { url: 'http://www.mysite.org/flowplayer/streamer.php?file=about_us.flv', provider: 'lighttpd', bufferLength: "2", autoBuffering: false }], // streaming plugins are configured normally under the plugins node plugins: { lighttpd: { url: 'http://www.mysite.org/flowplayer/flowplayer.pseudostreaming-3.2.5.swf', // use ${start} as a placeholder for the target keyframe queryString:escape('&start=${start}') } } }); //]]> </script> I have a function below where every time a question is submitted, it will add a new row in the table with a textbox which allows numbers entry only. My question is that I don't know how to code these features in this function: 1: I want the text box to be between 0 and 100, so if text box contains a number which is above 100, it will automatically change the number to the maximum number which is 100. Does any one know how to code this in my function below in javascript: Code: function insertQuestion(form) { var row = document.createElement("tr"); var cell, input; cell = document.createElement("td"); cell.className = "weight"; input = document.createElement("input"); input.name = "weight_" + qnum; input.onkeypress = "return isNumberKey(event)"; cell.appendChild(input); row.appendChild(cell); } I am trying to figure out how to make a random number I can plug into a script count down from that number at certain times of the day until it reaches 0. I would like it to reset itself at midnight every day. I'm trying to make it work with a script I found on here that resets itself at midnight every day. So instead of it counting down too fast, it would count down to the next number after a randomly generated number of minutes until it reaches 0. But it wouldn't necessarily have to end at 0 at midnight. It could go from 845 to 323 at the end of the day at a slower pace. Is that possible?
Hey all, I have a simple example below showing how when I pass in the value of the value attribute of option node, and then use if operator to check whether parameter is a string or not, even though it's a string, it converts it to false boolean and triggers the else statement rather than calling a function: Code: <body> <select> <option value="createMsg">Add Message</option> <option value="removeMsg">Remove Message</option> </select> </body> Code: var menu = { handleMenu : function(callback){ if(callback === "string"){ menu[callback](); } else { console.log("Issue occurred") } }, createMsg : function(){ var content = document.createTextNode("Please give additional information."), heading = document.createElement("h1"); heading.appendChild(content); document.body.appendChild(heading); }, removeMsg : function(){ } } document.getElementsByTagName('select')[0].onchange = function(){ menu.handleMenu(this.value)}; callback should be a string so why is it saying otherwise? Thanks for response Not sure if this should be in the ASP forum or this one (probably could go either), so here goes. I don't know hardly any - probably best to say dont know any at all - javascript. However I have a need to convert some code from asp to js. the code is checking to see if certain variables are null and if so counting how many of those variables are as such. the ASP looks like this: Code: <% Dim counter counter = 0 if a <> "" then classcounter = classcounter + 1 if b <> "" then classcounter = classcounter + 1 if c <> "" then classcounter = classcounter + 1 if d <> "" then classcounter = classcounter + 1 if e <> "" then classcounter = classcounter + 1 if f <> "" then classcounter = classcounter + 1 %> I need to know how to convert this to js so I can show this number to the user without sending data to the server first. I have searched the net for something similar but I am not having much luck finding any examples that do what I would like it to do (maybe this is not something that can/should be done in js?). I hope this makes sense...thanks! Code: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> </head> <script type = "text/javascript"> var strCurrency = new Array(6); strCurrency[0]="Pound"; strCurrency[1]="Marc"; strCurrency[2]="Franc"; strCurrency[3]="Dollar"; strCurrency[4]="Nordic"; strCurrency[5]="Fivesmon"; var dblExchangeRate = new Array(6); dblExchangeRate[0]="1"; dblExchangeRate[1]="74"; dblExchangeRate[2]="8.54"; dblExchangeRate[3]="6.25"; dblExchangeRate[4]="98.1"; dblExchangeRate[5]="1.32"; function convertNumbers { } </SCRIPT> I want to see what the function would look like to be able to convert into different currencies by using arrays. I know that this would be a useless way to do it as they are updated every day, but it's for personal education purposes. (Note: The website contained a textbox in which to enter the amount in pounds, a drop-down box allowing the user to select currency, a button to click to convert, and another text box showing amount of money after converting.) Any help is greatly appreciated. I am trying to convert following pesudocose to javascript. Dont know where am i messing it up. Any help will be great PSEUDOCODE: Program TaxCalc Read purchase If(purchase>=0 AND purchase<=7) tax=0 else if(purchase>=8 AND purchase<=21) tax=1 else if(purchase>=22 AND purchase<=35) tax=2 else if(purchase>=36 AND purchase<=49) tax=3 else if(purchase>=50 AND purchase<=64) tax=4 else if(purchase>=65 AND purchase<=78) tax=5 else if(purchase>=79 AND purchase<=92) tax=6 else if(purchase>=93 AND purchase<=99) tax=7 else print error the number should be >=0 and <=99 print tax End Program JAVASCRIPT that I have so far <html> <body> <script type="text/javascript"> // Program name: Tax Calculation // Purpose: Find tax for amounts under $1 // Name: Niral Patel // Date: 2/15 START //Declare variables var purchase; //Amount to be entered by user var tax; //Tax to be calculated //Welcome the user //Ask them to enter an amount less than a dollar document.write("This program will help you calculate tax" + BR); document.write("Please enter the amount, the amount has to be less than a dollar" + BR); purchase = prompt("Enter the amount for which tax is to be calculated:",ES); purchase = parseInt(purchase) if (purchase >= 0 ) { tax = 0 } else if (purchase >= 7 ) { tax = 0 } else if (purchase >= 21 ) { tax = 2 } else if (purchase >= 35 ) { tax = 3 } else if (purchase >= 49) { tax = 4 } else if (purchase >= 64 ) { tax = 5 } else if (purchase >= 78 ) { tax = 6 } else if (purchase >= 92 ) { tax = 7 } else if (purchase >= 93 ) { tax = 6 } else if (purchase >= 99 ) { tax = 6 } else { document.write("Number should be between 0 and 99." +BR); } display tax; Stop </script> </body> </html> Hi, I was wondering - how can I convert something that a user draws inside canvas? Say, they draw a square, how could I convert that to print "You have drawn a square" - this would be posted to a text file. Thanks in advance. I am having an issue with converting my decimal to a percentage... any suggestions? Thanks - it's probably an easy solution, but I am lost below is my code (cost * .06) is where the issue is - Thanks! var cost = prompt("What is the cost of your purchase?", "") document.write("Return Value: "+cost,("<br />")); var salestax = cost * .06 document.write("Return Value: "+salestax,("<br />")); var total = cost + salestax document.write(cost + salestax); Hello, I need your help converting the code below from VBA to Javascript. I am basically attempting to export a recordset to an excel file. VBA: Code: '------------------------------------------------------- Public Sub ExportTOExcel() '------------------------------------------------------- Set oExcel = CreateObject("Excel.Application") Set oBook = oExcel.Workbooks.Add Set oSheet = oBook.Worksheets(1) Dim j As Long Dim lngCol As Long Dim lngRow As Long Dim Field As Object rs.MoveFirst lngRow = 1 With oSheet Do While Not rs.EOF lngCol = 0 For Each Field In rs.Fields lngCol = lngCol + 1 .Cells(lngRow, lngCol) = Field.Value Next lngRow = lngRow + 1 rs.MoveNext Loop End With oBook.SaveAs "C:\Book5.xls" Workbooks.Open fileName:="C:\Book5.xls" End Sub Thanks for everyones help in advance, Cheers, J |