Need Help With Random Number Program

Hey forum users!
I need some assistance with a random number program. The RNG itself is fine, but I can’t figure out a way to use an if else statement to output “You Win” if the number is greater than .5 and “You Lose” if the number is less.

My current HTML schema is:

RNG

Welcome to Meteor!

{{> hello}}

Click

Your number is {{number}}

{{word}}

And my current JS schema is:

if (Meteor.isClient) {
// counter starts at 0
Session.setDefault(‘number’, Random.fraction());
Session.setDefault(‘word’, “”);

Template.hello.helpers({
number: function () {
return Session.get(‘number’);
}
});

Template.hello.helpers({
word: function () {
return Session.get(‘word’);
}
});

Template.hello.events({
‘click button’: function () {
// increment the counter when button is clicked
Session.set(“number”, 0+Random.fraction());
}

});

Template.hello.events({
‘click button’: function () {
// This is where I want the If Else
Session.set(“word”, 3);
}

});

}

if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
}

And the output is:

I am extremely new to coding so please use dumbed down words if possible.

Assistance would be greatly appreciated. Also, if anyone sees any way I can make this program more efficient I would like to hear it.

sorry the HTML code actually generated. Just ignore it and focus on the JS code.

So every time i try creating an If Else statement and setting “word” equal to “You Lose/Win” I just get no output as a result.

I would use a helper function in a template rather than a session variable…
Naiive random Did I win and between1and3 number.

var didIWin = function() { return Math.random()*1+0 > 0.5 }

var between1and3 = function() { Math.floor( Math.random()*3+1 ) }

If you must set a session, set the variable from the return of a function it’s cleaner.
Session.set ( “word”, between1and3 );

IMHO

I don’t really understand that very well :stuck_out_tongue:, when I said i was new i meant really new. Is there anyway you can explain that in simpler terms and also, how would i output my result of “You win” in the body?

It would probably be easier for me to write a small meteor app for you does want you want it to do. I’m pretty busy, but if you let me do it over the weekend I don’t mind helping you.

Can you explain what you want the app to do. A simple user story, if you don’t know already in programming, we try to group around the functionality around a simple story explaining what the end result is. It’s easier than trying to guess what you are doing with code (especially since you are new to programming).

As a user I should be shown a menu that shows me a word. When I Guess the word… yadda ya.

I was beginner in programming languages about 12 years ago too. Welcome here

Here’s a really quick example (runnable app here):

a) meteor create win-lose

b) meteor add reactive-var

c) Replace the contents of win-lose.html with:

<head>
  <title>Win or Lose?</title>
</head>

<body>
  <h1>Win or Lose?</h1>
  <p>Click the button - if your result is greater than 0.5, you win!</p>
  <button>Go!</button>
  <p>
    Result: {{result}} <strong>{{winOrLose}}</strong>
  </p>
</body>

d) Replace the contents of win-lose.js with:

if (Meteor.isClient) {

  Template.body.onCreated(function () {
    this.result = new ReactiveVar(0);
  })

  Template.body.helpers({
    result() {
      return Template.instance().result.get();
    },
    winOrLose() {
      const instance = Template.instance();
      let winOrLose;
      if (instance.result.get() > 0) {
        winOrLose = (instance.result.get() > 0.5) ? 'You win!' : 'You lose!';
      }
      return winOrLose;
    }
  });

  Template.body.events({
    'click button'(event, instance) {
      instance.result.set(Math.random());
    }
  });

}
1 Like

That would be awesome! I’m actually attempting to create a small virtual currency gambling site for a game called EVE. I’m just learning the basics RN so it won’t be set up for a long time. What i actually want to do is have a gambling program that has you enter a value that you want to wager, then if you win it pays back 1.95X what you wagered. If you lose you just lose the money you wagered.

Hey thanks for this,
I was going to do this but I had unexpected travel over the weekend and looks like you did a fine job here illustrating this.

1 Like