What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » [Request] Random Number Generator

[Request] Random Number Generator
Author: Message:
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
32 / Male / Flag
Joined: Apr 2004
RE: [Request] Random Number Generator
Ah, matty goes the DIY way again! :P

However, I think you have a small error in your error handling. When parseFloat can't convert Param into a valid floating point number, it'll return NaN. Now, NaN isn't just a string containing those three letters, it's a special value and - ironically - is of the type Number.

The correct way to see whether the result is valid is by using the isNaN function provided by JScript/JavaScript. I also recommend doing the parseFloat call after confirming that the /random command was sent. So, this would be the corrected code:
js code:
function OnEvent_ChatWndSendMessage(oChatWnd, sMessage){
    if (/^\/([^\s\/]+)\s*([\s\S]*)$/.exec(sMessage) !== null) {
        var Command = RegExp.$1.toLowerCase();
        var Param = RegExp.$2;
        switch (Command) {
            case 'random':
>>>                if (!isNaN(Param = parseFloat(Param))) {<<<
                    return Math.floor((Param)*Math.random());;
                } else {
                    return '';
                }
        }
    }   
    return sMessage;
}
Yes, that line is perfectly valid. First I re-assign Param to the parseFloat result, then that result is checked against isNaN. Neat, uh? :P
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
08-05-2009 07:32 PM
Profile E-Mail PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
[Request] Random Number Generator - by cerealkiller54 on 08-05-2009 at 07:25 AM
RE: [Request] Random Number Generator - by Matti on 08-05-2009 at 08:16 AM
RE: [Request] Random Number Generator - by cerealkiller54 on 08-05-2009 at 06:30 PM
RE: [Request] Random Number Generator - by matty on 08-05-2009 at 07:15 PM
RE: [Request] Random Number Generator - by Matti on 08-05-2009 at 07:32 PM
RE: [Request] Random Number Generator - by matty on 08-05-2009 at 07:55 PM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On