0

I'm fairly new to Javascript, and am confused on something. Why can't the command "println("..."); be called as a variable such as: var num = println("...");. I could be wrong, and if you are able to, I'd be happy to know how. But after some testing it seems like I can't. My test code is:

function start() {
    var SENTINEL = "1 1";
    var rollOne = Randomizer.nextInt(1, 6);
    var rollTwo = Randomizer.nextInt(1, 6);
    var num = println(rollOne + rollTwo);
    if(num == SENTINEL) {
          println("You did it");
    }
}

All it's supposed to do is give to random numbers in a # # form and, if it sees that the numbers are 1,1, it will give a message. It wont give the message and can't seem to view the variable "num" as an actual variable. But when I change the variable num to simply asking the user for a number:

function start() {
    var SENTINEL = -1;
    var rollOne = Randomizer.nextInt(1, 6);
    var rollTwo = Randomizer.nextInt(1, 6);
    var num = readInt("Enter number");
    if(num == SENTINEL) {
        println("You did it");
}

} And type in -1, it triggers the sentinel, thus promptly displaying the message. This is a really roundabout way to ask a simple question but I hope I can get some help. Thank you :)

Dor Inte
  • 3
  • 1
  • 1
    What the heck is `println`? Or `Randomizer` or `readInt`? – Andrew Li Oct 19 '16 at 04:36
  • @AndrewLi Probably an assignment with a given helper library. – Derek 朕會功夫 Oct 19 '16 at 04:37
  • You would need to tell us @DorInte, no way we can guess how those methods are implemented – Clive Oct 19 '16 at 04:40
  • For clarification: println(); is used for writing text or numbers, it can also be used to write the value of a variable (It seems fairly similar to the ( console.log(...) ) I've seen thrown around here). Randomizer is a function that picks a random number between the two given integers.(In this case 1-6). And readInt is a function that creates a popup asking the user for a number, and if the user types -1, it triggers the sentinel. Sorry for the confusion, the website I'm using (codehs) uses a pseudo-language it seems. – Dor Inte Oct 19 '16 at 04:52
  • @DorInte what do you expect `println` to return that will be equal to -1? – Andrew Li Oct 19 '16 at 04:58
  • I don't, the two codes have different ways of triggering the sentinel. The first one has to have the `println` say "1 1" and the second is triggering the sentinel by the user typing -1. – Dor Inte Oct 19 '16 at 05:03
  • If you're matching on "1 1", you need to concat, not add. JavaScript is implicit... `1 + 1 = 2`, but `"1" + 1 = "11"` (i.e. double quotes change what `+` does). In your case, your `println(rollOne + rollTwo);` is incorrect. It should be `println(rollOne + " " + rollTwo);` Still won't validate until you take the answers below into consideration, which explain why you can't use the return value. – tresf Oct 19 '16 at 05:26

3 Answers3

1

Why can't the command "println("..."); be called as a variable such as: var num = println("..."); [...] It wont give the message and can't seem to view the variable

If the value returned is unusable, it is most likely undefined; i.e. The function println doesn't explicitly return anything.

In your case, you could try something like this:

var printInt = function(num) { println(num); return num; }

Note, println isn't part of the standard JavaScript language. For modern web browsers, it can be adapted to use (console.log(...)).

var printInt = function(num) { console.log(num); return num; }

And then to adapt to your code:

var num = printInt(rollOne + rollTwo);

But this still won't validate because you're comparing against "1 1" whereas your logic will return 2. JavaScript (as well as many other languages) implicitly uses addition when supplied with two numbers, but concatenation when supplied with at least one string.

var SENTINEL = "1 1";  // <---- String!
var SENTINEL = -1;     // <---- Number!

So you should consider something like this instead (renamed accordingly):

var printRolls = function(text) { println(text); return text; }
var rolls = printRolls(rollOne + " " + rollTwo);
if(rolls == SENTINEL) {
      println("You did it");
}

Or to simplify it a bit:

if(printRolls(rollOne + " " + rollTwo) == SENTINEL)
    println("You did it");
Community
  • 1
  • 1
tresf
  • 7,103
  • 6
  • 40
  • 101
  • 1
    It should be possible to assign the return value from any function to a variable, noting that some functions return `undefined`. – nnnnnn Oct 19 '16 at 04:55
  • @nnnnnn added note about `undefined` and added relevant quote to context – tresf Oct 19 '16 at 05:01
0

It is possible that println doesn't return the string that is passed into. In that case, you can use

if (SENTINEL === rollOne + " " + rollTwo)

to format the string and properly test equality.

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
0

In JavaScript it is possible to assign the return value from any function to a variable similar to how you've done it:

var anyVariable = anyFunction();

But, some functions return the value undefined. Or they return a number, or an array, or...whatever.

I imagine your println() function prints the value you pass to it somewhere (on the screen? to the console?) and then returns undefined. Or if it is returning the printed value it is in a format different to what you have used in your SENTINEL variable. So then when you try to compare that with SENTINEL it won't be equal.

To fix your original function, assign the sum of the rolls to a variable, then print and test that:

function start() {
    var SENTINEL = 2;
    var rollOne = Randomizer.nextInt(1, 6);
    var rollTwo = Randomizer.nextInt(1, 6);
    var num = rollOne + rollTwo;
    println(num);
    if(num == SENTINEL) {
          println("You did it");
    }
}

EDIT: if you want the println() to display a string like "1 1" or "3 5" to show what each of the two rolls were then do this:

println(rollOne + " " + rollTwo);

That is, create a new string that is the result of concatenating rollOne's value with a single space and then rollTwo's value.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • The + sign in the variable num might've been confusing, but it's supposed to symbolize that the variable is supposed to print both rollOne and rollTwo, not add them together. – Dor Inte Oct 19 '16 at 05:07
  • @DorInte that makes no sense? You're adding two integers then passing it to `println`, if you want to print them both use a string. – Andrew Li Oct 19 '16 at 05:08
  • It's not that I'm adding the two integers, but it's displaying the outcome of the rollOne and rollTwo. The + can be seen as an "as well as". The way the pseudo-language taught us to do that was to say rollOne += rollTwo. – Dor Inte Oct 19 '16 at 05:15
  • That's not how `+` works in JavaScript though. In JS (and pretty much every other programming language), when you say `println(rollOne + rollTwo)` what happens is first `rollOne + rollTwo` is evaluated, which means it does an addition if those variables contain numbers or a concatenation if those variables contain strings, and *then* the function `println()` is called with the result of that addition/subtraction. I'll make a minor change to my answer to show how to output something like `"1 1"` or `"4 2"`. – nnnnnn Oct 19 '16 at 05:22
  • Huh, that's interesting, I guess this really is a subpar pseudo-language. If that's the case, I'm not sure if anyone can really help if they can't understand what they're reading. It was just an off the top of my head question anyways. Thanks for all the support :) – Dor Inte Oct 19 '16 at 05:26
  • @DorInte the confusion stems from two things (neither are due to the language being sub-par). 1. The understanding of [append vs. concat](http://stackoverflow.com/questions/8962482/yet-again-on-string-append-vs-concat-vs). 2. The understanding of how a function returns a value. This has all been covered in the answers as well as the comments. You should consider marking one of them as correct. – tresf Oct 19 '16 at 05:38