Some Simple Questions from a Complete Beginner

I’d call it an “operator” as in “performs an operation”, the operation in this case being incrementing the variable value.

I don’t know where you found the image, but IMHO the comments are partial explanations and would be better more like
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators

The increment operator increments (adds one to) its operand and returns a value.

  • If used postfix, with operator after operand (for example, x++), then it increments and returns the value before incrementing.
  • If used prefix, with operator before operand (for example, ++x), then it increments and returns the value after incrementing.
1 Like

This is where I’m doing the exercises.

I thought an operator was limited to things like this:

  1. Assignment Operator : assigns the value of the operand to the right to the operand to the left.
    For example:
    var a = 1

  2. Comparison Operators:
    === : equality (the same as)
    !== : inequality (different than)
    <, >, >=: greater than, lesser then, etc…

3. Arithmetic Operators:
+,-,*,/: this one is fairly simple.

In my mind:
myVar++ wouldn’t qualify as an operator as it includes a variable as well. The operator would be the ++ of the line of code, not the whole line of code itself. That’s the way I had processed the notion of operator anyways.

I do have another question which something that had been stumping me, sort like the increments aspect.

So in this exercise I need to modify the following code:
var myStr; // Change this line
to

Assign the following three lines of text into the single variable myStr using escape sequences.

[Image removed as I’m new and can only post one, grr…]

I know that you can escape by using the . I just practiced that by quoting within a text string. I’m also given a reference guide:
Exercise with the guide for various escape notations. You can also check the objective of the exercise there.

So let’s think about this.
(1) After the FirstLine & SecondLine I need a carriage return, which is /r.
(2) Before the SecondLine & ThirdLine, I need to have /n because a new line is starting.
(3) I need a \\ before the SecondLine preceded by /t to get the same result (tab + backslash).

So I thought this would be the right answer:
var myStr \n FirstLine \r \tab \\ SecondLine \r \n ThirdLine ; // Change this line
…but it’s not. I get the following error saying I a syntax error with my \n.


So I checked the following objectives for the exercise:
[See first link]

So I removed all the spaces and it still didn’t work:
var myStr\nFirstLine\r\tab\\SecondLine\r\nThirdLine ; // Change this line
I got the same error mistake actually.


So looking at the objectives, I found out that a new line doesn’t come at the beginning of a line but rather at the end. I had misunderstood.

So since it’s assigning a variable, I figured I’d need the = operator as well. Also, since they’re strings and not values, I need to use quotations which I had missed.

var myStr = 'FirstLine'\n'SecondLine' 'ThirdLine' ; // Change this line
Still getting the same error message, there’s something wrong with my syntax of \n.

If I look at my objectives from top to bottom and try again:
var myStr = "FirstLine\n\t\\'SecondLine'\nThirdLine"; // Change this line

  1. I don’t have any spaces.
  2. All three strings are there.
  3. First line is followed by the newline character.
  4. The \t is there as well.
  5. SecondLine is preceded by the backslash character.
  6. There is a \n between the SecondLine and ThirdLine.

So I run…


Wow, great improvement. But still missing the last objective. Why is that? It’s clearly there:

So I figured maybe I messed up my quotations:

So I removed all the quotation marks and… I got it!
var myStr = "FirstLine\n\t\\SecondLine\nThirdLine"; // Change this line
The preview mislead me into making a mistake I think:
image as the font is black instead of being red. I was trying to be exactly the same.


I wanted to share my reasoning so that people could point out where I have some fundamental misunderstandings. That’s why I took the time to share my stream of consciousness so I can rectify some misconceptions I already have before going further.

So I keep reading about return and I still don’t get it. For some reason I just don’t understand what it does at all.

function ourTrueOrFalse(isItTrue) {
  if (isItTrue) { 
    return "Yes, it's true";
  }
  return "No, it's false";
}

Is return a way of saying that a code block is finished when dealing with a statement? Even in this new exercise about Boolean it shows up again and I can’t continue until I understand this concept.

In regard to the return statement, that tells the interpreter to not interpret any further lines of that function, and to return the value back to the function caller.

1 Like

So you mean if there were no return it would keep on going and go to “No it’s false” as well?

That’s right it would.

[off/topic]
Can JavaScript accept and return multiple function variables by reference?
[/off-topic]

Yes it can.

What are your expected inputs and outputs?

But then what’s the difference between a break and a return statement?

A break is used to to terminate a loop, switch, or label statement.
A return is used to return a value from a function.

Both interrupt the execution flow of the code, but achieve very different things.

1 Like

[off-topic]
I was curious and have no particular requirements at the moment but handy to know.

Perhaps mention about passing multiple variables by reference after the OP thoroughly understands his/her original query.

Also the difference and possible problems if any in passing arrays.
[/off-topic]

It was recommend a long time ago not to have multiple function return values because it was considered confusing especially when trying to understand the functionality :slight_smile: Far better to have a single point of return as the last function statement.

Setting the return value before calling a break passes the set value to the single return statement.

I am aware that functions can also return different types (such as bool, strings, numerics, etc) but really like how the latest PHP 7 can specify a function’s return type. Errors are thrown if the type is not as specified.

Are break statements and switch always used together? Are they ever separate?

And another question too (about return yet again:

In this example:

function myFun() {
  console.log("Hello");
  return "World";
  console.log("byebye")
}
myFun();

I understand that console.log("byebye") never gets to be taken into consideration because the return statement prevents it from doing so. After that the function myFun() is called upon. That’s clear.

Question 1:
What does the whole console thing even means? Does it mean that the result is stored somewhere? It’s only used for debugging, correct?

function abTest(a, b) {
  // Only change code below this line
  
  
  
  // Only change code above this line

  return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));
}

// Change values below to test your code
abTest(2,2);

So I have the exercise above to complete.

Modify the function abTest so that if a or b are less than 0 the function will immediately exit with a value of undefined .

I partly understand the return statement at the bottom.

Could I get just a hint to get me started? Not the answer, just a vague hint.

Thanks!

Break and switch statements aren’t always used together. The break statement has separate uses too, such as breaking out of a while loop.

A switch statement without a break statement though isn’t of much use, because without the break you can’t prevent other labelled sections from being executed.

1 Like

The post above mine looks to have been dramatically edited after I replied. That’s normally a bad idea because no announcement occurs when edits are made. It was purely by chance that I came back and saw the massive change.

Small edits are fine, but large changes tend to go unnoticed.

It’s best to make another post when a lot of new content is added.

2 Likes

Correct,

console.log("byebye")

will never be executed beacause the return statement stops the function execution.

The console.log() prints whatever you pass to it as an argument to the developer console of your browser. You can access it (in most browsers) by pressing F12 and clicking on the “console” tab.
It’s something that is very useful when you need to debug some code.

For your question, the instructions say:

Modify the function abTest so that if a or b are less than 0 the function will immediately exit with a value of undefined .

You’ll need to write your solution in the part that goes before the

return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));

which include checking if either a or b are less than 0.

Remember that undefined is a primitive value and as such it can be “returned” (much like a number or a String). More info on the primitive types here.

2 Likes

I’m used to being to told to edit my message instead of posting a new one to avoid double posting. Thanks for letting me know!

I learned about the term ‘primitive value’ just yesterday from someone else so it’s a good coincidence.

Remember that undefined is a primitive value and as such it can be “returned” (much like a number or a String). More info on the primitive types here.

Ok so all primitives can be returned. But if I want to check:

which include checking if either a or b are less than 0.

I should probably use an operator <0 in that case, am I right along the right track?

Well keep in mind that your return statement will be evaluated before it’s actually returned.

return 3 is simple - it returns the primitive Number 3.

return 1+2 is also simple - it also returns the primitive Number 3, because it evaluates 1+2, and puts the result into the return.

return a < b (yes i know that’s not the example you gave) will also return a primitive - in this case we don’t actually know what the value of the primitive will be (because it depends on the values of a and b), but we know that the type of primitive returned will be a Boolean - it will either return true or false.

1 Like