/    Sign up×
Community /Pin to ProfileBookmark

Using Math values

I am trying to manipulate the ‘secnds’ field and keep gettin an error. My knowledge is limited, so I do not know what the error is.

Thanks in advance for your help.
yardman0.

<html>
<body>
<script type=”text/javascript”>
var live_data = ‘23.67’
var secnds = live_data.substring(0,5);
var minets = Math.floor(secnds/60); secnds = secnds%60;
alert(“secnds = ” + secnds )
var live_data1 = secnds..substr(2,4) <!– Message issued:Type mismatch (usually a non-object value
used where an object is required) –>
<!– I would like to strip out the .67 and use it
in this statement math.round(live_data1/.2).
perhaps you can tell me a way to accomplish this. –>
<!– However if I specify a value of 1:23.67 for live_data
it works. –>
alert(“live_data1 = ” + live_data1 )

</script>

</body>
</html>

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@NaldzAug 29.2006 — var live_data1 = secnds..substr(2,4)

double dot...
Copy linkTweet thisAlerts:
@NaldzAug 29.2006 — on second thought, your real problem lies on the fact that the secnds variable is not of type string and you cannot use the substr method on nonstring objects. Try converting it first into a string variable by [CODE]secnds += ""; [/CODE] before you use the substr method.
Copy linkTweet thisAlerts:
@yardman0authorAug 29.2006 — Thanks. I would have never figured that out.

Right now I am putting up shutters in anticipation of this hurricane that is threatening us.
Copy linkTweet thisAlerts:
@NaldzAug 29.2006 — glad i could help ?
Copy linkTweet thisAlerts:
@Tweak4Aug 29.2006 — Wow... please clean up your code.

#1. You have html comments inside your script. This is bad. Use // instead of <!-- to comment scripts

#2. Putting each statement on a new line makes it much easier to read. I didn't even see "secnds = secnds%60;" until I loaded the whole thing into a debugger.

#3. As Naldz said, variable types are very important.

When you create secnds, it is a string. However, in the next line, you are performing math functions on it. Beyond that, you are performing string functions again.

Replace your line declaring secnds with:
[CODE]var secnds = [B]Number([/B]live_data.substring(0,5)[B])[/B];[/CODE]
This will allow you to perform your math operations without any strange parsing errors (probably not absolutely necessary, but good form).

Likewise, either make the change Naldz suggested, or change the line to:
[CODE]var live_data1 = [B]String([/B]secnds.substr(2,4)[B])[/B];[/CODE]

Javascript does let you change your variable types on the fly, but you always must be aware of what types your variables are, so you know if/when you need to convert them.
Copy linkTweet thisAlerts:
@Tweak4Aug 29.2006 — Also, now that I think about it, unless you're positive that secnds will always be the same length, instead of substringing out the last two digits with hard-coded values, you might want to use secnds.indexOf(".") to strip out whatever is after the decimal, in case there is only on digit there... just a thought.
×

Success!

Help @yardman0 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...