8. Functions, return and if / else/"There was a problem with your syntax."


#1

Hi,

When I write this code, it says: "Oops, try again. There was a problem with your syntax." And on the white window it says:"SyntaxError: expected expression, got keyword 'else'"

// Define quarter here.
var quarter = function (number)
{return number % 4};
if(quarter(24)===0)
{console.log("The statement is true")};
else{console.log("The statement is false")};

#2

Below is your code, but I put a comment next to the syntax error:

// Define quarter here.
var quarter = function (number){
    return number % 4
};
if(quarter(24)===0){
    console.log("The statement is true")
}; // Remove this semicolon.
else{
    console.log("The statement is false")
};

#3

Now it says: "Oops, try again. There was a problem with your syntax."

// Define quarter here.
var quarter = function (number)
{return number % 4;};
if(quarter(24)===0)
{console.log("The statement is true")}
else{console.log("The statement is false")};

#4

// Define quarter here.
var quarter = function (number){
    return number % 4; //Should be 'number/4'
};
if(quarter(24)===0){
    console.log("The statement is true")
}
else{
    console.log("The statement is false")
};

#5

I cannot run it too by typing the text in the following:


#6

It should be typed like this:
// Define quarter here.
var quarter = function (number){
return number / 4; //Should be 'number/4'
};
if(quarter(24)){
console.log("The statement is true")
}
else{
console.log("The statement is false")
};


#7

can someone explain to me how this code divides and makes 24 = 0?


#8

Now I got it right. I got it first wrong,because I understood the question in the wrong way. I thought that I should just put some number (any number) and divide it by 4. But when I saw another discussion about this same topic, I understood, that I should put a number that gives an answer 1/4 when it is divided by 3.

// Define quarter here.
var quarter = function (number)
{return number / 4;};

if (quarter(12) % 3 === 0 ) {
  console.log("The statement is true");
} else {
  console.log("The statement is false");
}

#9

@mariamariiia + @lightattheend The idea is that you should pick a number that is still divisible by 3 when you divided it by 4. The division by 4 is obviously done by your quarter function the check of divisibility by 3 is a bit more tricky but % has been introduced earlier right?

What it does is to compute the remainder of a division e.g. assume you only know integer numbers (computers tend to always have some problems with fractions due to limited memory see IEEE 741 for an explanation how this is done) then a calculation 7/4 would give you 1 because 4 goes into 7 once fully and a remainder of 3 which still needs to be divided by 4 but you have no symbol to express this. So what modulo does is to give you this remainder. So when a number is fully divisible by another number the remainder is 0 and that is what we check for here. Hope this makes it a little more clear.


#10

var quarter = function(number) {
return number/4 }

if (quarter(12) % 3 === 0 ) {
console.log("The statement is true");
} else {
console.log("The statement is false");
}

try this it should work


#11

Please don't just post correct code without explanation, that's against forum guidelines.


#12

@haxor789, so 12/4= 3 and this 3 which was left is again divisible by 3 ---> 3/3 = 1. So there is no remainder (the remainder is 0) so the first statement is true. Ok, now I understood it completely. Thank you for good explanation.

Edit: but it should probably work also with 24, because 24/4 is 6 and 6 divided by 3 is 2 so there is no remainder (the result is integer not something like 2,2 or something like that)?


#13

yes it works for any number that is divisible by 12 because (12 is the least common multiple of 3 and 4).


:wink:


#14

Thank you so much! I was stuck with "not having a '{' at the start of a function body"!


#15

A post was merged into an existing topic: Functions, return and if / else .help!


#16

what is wrong with my code

if (quarter() % 3 === 0 ) {
console.log("The statement is true");
} else {
console.log("The statement is false");
}


#17

Thank you for the code


#18

Really enjoyed this course so far but as a self-taught professional JS coder going through the course just for kicks, this section is way too complex.

Why do programming tutorials work with maths and numbers for so many examples?
Why make learning programming harder than it should be with confusing and ridiculous sections like these?

Not everyone has a mathematical mind that automatically assumes that the value to be inserted should be divisible by 4 and 3. In fact, shouldn't the application figure this out for us instead of us having to work out the input values?

Real world programming is often running functions, retrieving values and putting them somewhere else or performing a specific task. There will be tons of concatenations, string manipulations and function creations, but I've hardly ever used "mathematical" calculations in the real world.


#19

but I've hardly ever used "mathematical" calculations in the real world.

Guess that depends on the level on which you're working. In the end the whole magic that the computer does is based on math and the more math you know faster your algorithms may get. So if you build code on low level I can hardly imagine doing this without any math. But if you're programming on high level where other people did the math for you and you just need to use "magic" performing functions, well yes there you can probably do many things without bothering how it is done. And as JS is rather high level you're probably right but saying that you will hardly see any math in programming is too general spoken.


#20

you are right this is the right code