Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

javascript - does not run random messages...

this is suppose to be a random message that is choosen, however it keeps selecting the 10th one...is there an error with this code?
function ranTime(){
   var myNumbera = Math.floor( Math.random() * 6 ); 
   var myNumber2a = Math.floor( Math.random() * 6 ); 
   var myNumber = 10+Math.floor(Math.random()*11) ;
   var myNumber2b = 10+Math.floor(Math.random()*11) ;
   var myNumber2 =  Math.floor( Math.random() * ( 10 - 5 ) + 5 ); 

   var advertise_num =  10+Math.floor(Math.random()*11);
   var advertise_msg
   
   if (advertise_num = 1) {
   advertise_msg = 'Does your service offer 24/7 Live Chat support? Auto Here does!'
   }
   
   if (advertise_num = 2) {
   advertise_msg = 'At Auto Here we handle all your needs!'
   }

   if (advertise_num = 3) {
   advertise_msg = 'A recent survey showed 89% of all clients recieved help with 8 hours. Does your service offer that?'
   }
   
   if (advertise_num = 4) {
   advertise_msg = 'Please complete our survey at the end, we want to hear from you!'
   }
   
   if (advertise_num = 5) {
   advertise_msg = 'Find a better de555petitor price guaranteed!'
   }
 
    if (advertise_num = 6) {
   advertise_msg = 'Find a better dea66petitor price guaranteed!'
   }
   
      if (advertise_num = 7) {
   advertise_msg = 'Find a better d777etitor price guaranteed!'
   }
   
      if (advertise_num = 8) {
   advertise_msg = 'Find a better deal?888petitor price guaranteed!'
   }
   
      if (advertise_num = 9) {
   advertise_msg = 'Find a better dea9 competitor price guaranteed!'
   }
   
         if (advertise_num = 10) {
   advertise_msg = 'Find a better10r price guaranteed!'
   }

Open in new window

0
GlobaLevel
Asked:
GlobaLevel
  • 11
  • 8
  • 7
  • +2
4 Solutions
 
cmalakarCommented:
all your if conditions should have "==" instead of "="

as in

if (advertise_num == 1) {
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
You are not checking values with if == using = to assign them

use ==
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
also there are no ; for ending lines at

advertise_msg = 'bla bla'

they should end with ;
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
BadotzCommented:
Change this:

var advertise_num =  10+Math.floor(Math.random()*11);

to:

var advertise_num = Math.floor(11 * Math.random());
0
 
BadotzCommented:
The above will give numbers from 0 to 10. If you want 1 through ten, the this will do the trick:

var advertise_num = Math.ceil(10 * Math.random());
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
10+ math.floor........ makes your code return a number between 10 to 20

so better use this corrected & checked code
 
function ranTime(){
   var myNumbera = Math.floor( Math.random() * 6 ); 
   var myNumber2a = Math.floor( Math.random() * 6 ); 
   var myNumber = 10+Math.floor(Math.random()*11) ;
   var myNumber2b = 10+Math.floor(Math.random()*11) ;
   var myNumber2 =  Math.floor( Math.random() * ( 10 - 5 ) + 5 ); 
   
   var randomized=Math.random();
   var number_max=3;//set from here highest expected
   var advertise_num = parseInt(randomized*number_max)+1;
   
   var advertise_msg;
    
   if (advertise_num == 1) {
   advertise_msg = 'Does your service offer 24/7 Live Chat support? Auto Here does!';
   }
   
   if (advertise_num == 2) {
   advertise_msg = 'At Auto Here we handle all your needs!';
   }

   if (advertise_num == 3) {
   advertise_msg = 'A recent survey showed 89% of all clients recieved help with 8 hours. Does your service offer that?';
   }
   
   if (advertise_num == 4) {
   advertise_msg = 'Please complete our survey at the end, we want to hear from you!';
   }
   
   if (advertise_num == 5) {
   advertise_msg = 'Find a better de555petitor price guaranteed!';
   }
 
    if (advertise_num == 6) {
   advertise_msg = 'Find a better dea66petitor price guaranteed!';
   }
   
      if (advertise_num == 7) {
   advertise_msg = 'Find a better d777etitor price guaranteed!';
   }
   
      if (advertise_num == 8) {
   advertise_msg = 'Find a better deal?888petitor price guaranteed!';
   }
   
      if (advertise_num == 9) {
   advertise_msg = 'Find a better dea9 competitor price guaranteed!';
   }
   
         if (advertise_num == 10) {
   advertise_msg = 'Find a better10r price guaranteed!';
   }
   alert("advertise_num is "+advertise_num+"& message : "+advertise_msg);
}

Open in new window

0
 
cmalakarCommented:
Actually, you can write it simply like this..
var advertise_num = Math.floor(Math.random() * 10);
 switch(advertise_num){
   case 1:
   advertise_msg = 'Does your service offer 24/7 Live Chat support? Auto Here does!';
   break;
   
   case 2:
   advertise_msg = 'At Auto Here we handle all your needs!';
   break;

   case 3:
   advertise_msg = 'A recent survey showed 89% of all clients recieved help with 8 hours. Does your service offer that?';
   break;

//Write all case statements
}

 alert("advertise_num is "+advertise_num+"& message : "+advertise_msg);

Open in new window

0
 
BadotzCommented:
You do not parseInt because both Math.ceil and Math.floor return an integer value.

So as I stated before, one of the following is all you need:
//
var advertise_num = Math.floor(11 * Math.random()); // Return 0 through 10

var advertise_num = Math.ceil(10 * Math.random());; // Return 1 through 10
//

Open in new window

0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
Parse integer parses integer part even if variable set as a string . I'd rather use parseInt to not losing time with such js errors. (coz integer(8) != string(8) ). Surely code here is very simple and we know it will came up as integer. I think both results are same. parseInt and Math.ceil is identical here. Just my hands loves  parseInt :)
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
BTW parseInt captures integer value from a variable even if it's a string
i mean parseInt("i am 4 years old") returns 4;
Mostly useful if you know what you are doing. Better is always defining variables as int, sting...  before
0
 
BadotzCommented:
>>parseInt and Math.ceil is identical here

Really?

Math.ceil returns an integer value from a float, and rounds up. parseInt returns an integer value but does not round. So in the sense that they both return an integer value they are "identical".

And here, there is no need here to worry about strings. I have never had rand return anything but a float - have you?
0
 
BadotzCommented:
>>i mean parseInt("i am 4 years old") returns 4;

I don't know what to say.

When would this ever be necessary? And why wouldn't you use a regex (or some other rovust method) to return a number from a string?
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
I guess you know where to use parseInt , better than me but here are some sample usages;
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt
For the ones not familiar with regular expressions much and some simple extractions like capturing browser versions functions like this are useful i think.

You may not use it here but it's created for a purpose and  , everybody is free what to use or not.
I just used here to make sure i have an integer,  if integer ceil result put in an string variable.(if variable set was string before -not here-)

Btw; My example is just for informing the asker about that function.

Best Regards...
0
 
BadotzCommented:
Another potential problem with using parseint to "extract" a number from a string is if there is no valid number in the string, it will return NaN, which is not a number, and, in fact, stands for "Not_a_Number".

To quote the MDC website ( https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseInt ):
If the first character cannot be converted to a number, parseInt returns NaN.

For arithmetic purposes, the NaN value is not a number in any radix. You can call the isNaN function to determine if the result of parseInt is NaN. If NaN is passed on to arithmetic operations, the operation results will also be NaN.

Open in new window

0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
for last word of me ... i need to correct a pronounce mistake of me .
Math.ceil is identical here
I thought i've written Math.floor because i've never used ceil (while it was a more clever approach)

Obviously ;

ceil rounds a number to the next larger integer      -> 1,7 to 2
floor rounds a number to the next smaller integer  -> 1.7 to 1
parsInt extracts the integer number from a string   -> 1.7 to 1
0
 
Michel PlungjanIT ExpertCommented:
erdincgc:
1) semicolons are not mandatory in Javascript at end of lines except in event handlers
2) parseInt("I am 4") will give NaN since parseInt only works on a string if the first non-blank char is a digit.

GlobalLevel:


var ads = ["Does your service offer 24/7 Live Chat support? Auto Here does!",
"At Auto Here we handle all your needs!",
"A recent survey showed 89% of all clients recieved help with 8 hours. Does your service offer that?",
"Please complete our survey at the end, we want to hear from you!",
"Find a better de555petitor price guaranteed!",
"Find a better dea66petitor price guaranteed!",
"Find a better d777etitor price guaranteed!",
"Find a better deal?888petitor price guaranteed!",
"Find a better dea9 competitor price guaranteed!",
"Find a better10r price guaranteed!"
];
function getAd() {
  return ads[Math.floor(Math.random()*ads.length)]
}

Open in new window

0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
mplungjan thank you for enlighting me .... i'm used to place a semicolon but i've learnt they are not needed generally now ... indeed when i just correct some errors in the code asker gave us i had used floor as he did but damn js sent an error so i just tried parseInt instead of finding the error's source (now i know it was because of my development environment which i ran test through it -online tester- ... ) so i just pasted that code ... surely math functions are more suitable...  thanks again :)
0
 
GlobaLevelAuthor Commented:
I want to thank all of you for your extensive help..I have tried ALL of your suggestions, unfortunately none are working....can you help?
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
Are there any online sample page which we can see the full code ?
0
 
Michel PlungjanIT ExpertCommented:
I would like to know what does not work

http://jsfiddle.net/mplungjan/Yk93J/

Click RUN and it will show a random ad each time (sometimes twice in a row, but that is how random works)
0
 
GlobaLevelAuthor Commented:
if you got to:

http://rdsrc.us/CSnJyC 

and click live chat...you will see advertisements on the wait pages...undernearth' Welcome'...and 'someone will be right with you'
0
 
Michel PlungjanIT ExpertCommented:
I do not see any ads

And why can't you use my suggestion?

Did you look at the fiddle? http://jsfiddle.net/mplungjan/Yk93J/
0
 
BadotzCommented:
There is nothing to trigger any of the JavaScript on that page - perhaps something should be invoked in the window.onload event?
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
If i didn't get you wrong you want to do some checking when visitor clicks "start live chat" ?

Then you should replace onclick in line

<a href="http://lc.textconnext.com/LC_wait_new_1.aspx" target="_blank" onclick="return popCC(this);">Start Live Chat</a>

<a href="http://lc.textconnext.com/LC_wait_new_1.aspx" target="_blank" onclick="if(setSession()==true) popCC(this); else return false;">Start Live Chat</a>

and remove the

setSession()

line from popCC() function
0
 
GlobaLevelAuthor Commented:
Thanks for the suggestions ...i will try later...can I throw another wrench in there....the real kicker is when you first get to the live chat window where the rep and user exchange messages...it works the first time around ...but if you open live chat again during the same session you get a blank window...'no' data...that's one I can't figure out....you literally have to close live chat and close the website and open the entire website again!
0
 
GlobaLevelAuthor Commented:
I apologizes for the delay....if you are all still around I would approciate your help....


can you please see the following link and click live chat...the live chat is where the issues are...if you go to the Live Chat first time around after you reboot your computer....it works fine...but on the second time the dialogue with the rep and user comes up blank....

@erdincgc...

I tried what you suggested at this post here:35795172....but it still came up with the same problems...
0
 
GlobaLevelAuthor Commented:
and here is the link....if you click 'Live Chat' in the upper left hand corner...
0
 
GlobaLevelAuthor Commented:
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
i'll be back with solutions after sleeping a little :)
0
 
GlobaLevelAuthor Commented:
Hi erdincgc...

was curious if you had a chance to look ...let me know if you have any feedback/questions...thanks...
0
 
Michel PlungjanIT ExpertCommented:
@Global Level, I was wondering if you ever looked at the code I wrote especially for you.

Thanks
0
 
BadotzCommented:
No worries - glad to help.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 11
  • 8
  • 7
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now