r/CodingHelp • • Apr 25 '19

How would I do this? Trying to start coding in JavaScript by understanding pseudocode.

Post image
23 Upvotes

11 comments sorted by

4

u/chubbard022 Apr 25 '19

Think of mod as remainder, so if you could take 2 mod x where x is equal to five. You will get that 2 can go into 5 with a remainder of 1. So that’s what your answer would be

5

u/Hifen Professional Coder Apr 25 '19

thats only the first half, then you have to resolve the condition which would trigger the second return, the answer would therefore be 2.

5

u/SupremeDesigner Apr 25 '19

This is some rather awful pseudocode though, I've never seen an if done like that before, with no curly brackets or indentation?

4

u/Askee123 Apr 25 '19

Talk about shit code. No indentation on the following statement to indicate it's part of the if statement, no brackets, and just a ton else wrong with it (the dude couldn't just say function instead procedure?). It's like this instructor wants to flex for no reason.

3

u/SupremeDesigner Apr 25 '19

A procedure is a method that doesn’t return anything, so this absolutely should be a function.

1

u/Hifen Professional Coder Apr 25 '19

I imagine thats the point though.

If this is a training exercise they are trying to force you to logic through it rather then rely on style conventions

1

u/SupremeRedditBot Professional Bot || Mod Apr 25 '19

Please Add A Flair To Your Post!

Suggested Flair: [Javascript]

 


To add a flair:

  • Click flair underneath your post

  • Select a flair

  • Click save

 


I am a bot run by /u/SupremeDesigner for /r/CodingHelp || This was an automated ^response

1

u/[deleted] Apr 25 '19

Modulo, sometimes called the remainder operator, will return the remainder of the division (assuming both numbers are positive). So for the pseudocode, the result of the conditional in the if statement will return false (5 mod 2 = 1....1 != 0). This means that the statement following the if statement will not run and the return 2 statement will run. The 2 will then be displayed when control returns to the original procedure call.

Edit: spelling

1

u/PC__LOAD__LETTER Apr 25 '19

Is the “mod” part the only bit that’s confusing? If so, others have explained it. If not, let me know and I can give it another explanation.

1

u/Shubham_Garg123 Apr 25 '19

I don't know java at all, I am currently learning C++ but seeing the code, I feel that output should be 2 which is the last option. Sorry if my answer is wrong (I don't think it can be wrong but I am just 16 yrs old so can't guarantee the answer)

1

u/[deleted] Apr 25 '19

2 is the answer