Skip to content

ToCode Solution: תרגול JavaScript Syntax#1

Open
shlomitha wants to merge 1 commit intoynonp:masterfrom
shlomitha:sol-html5-web-development.javascript-syntax-lab
Open

ToCode Solution: תרגול JavaScript Syntax#1
shlomitha wants to merge 1 commit intoynonp:masterfrom
shlomitha:sol-html5-web-development.javascript-syntax-lab

Conversation

@shlomitha
Copy link
Copy Markdown

No description provided.

Copy link
Copy Markdown
Owner

@ynonp ynonp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
Please see comments and submit fixed files

Thanks,
Ynon

function lcm(a, b) {
}

//function 1
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please leave the top comments in the file and functions (the ones inside /* */). They make it easier for everyone opening the file later to understand what's inside



function printBiggestNumber(){
var max=100;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re. var:

  1. Some people prefer to use just one var command, i.e. (note line ends with , not in ;):
var max = 100,
      min = 0,
      first_num = Math.floor((Math.random() * max) + min);
  1. Modern browsers (everything except IE) support 2 new keywords: let and const. Take a moment to read about both here:
    https://strongloop.com/strongblog/es6-variable-declarations/
  2. Style: JavaScript variables and functions are usually written in camel case (like Java), so firstNum is better than first_num
  3. Style: I'd define a function randomInRange(min, max), and then use that instead of repeating the calculation

var secound_num=Math.floor((Math.random() * max) + min);
var third_num=Math.floor((Math.random() * max) + min);

console.log("first_num ="+ first_num);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're using modern (non-IE) browsers, you can also use the newer string interpolation syntax:

console.log(`first num = ${first_num}`);

Notice the backticks (same button as ~ but without holding the shift)

console.log("secound_num ="+ secound_num);
console.log("third_num ="+ third_num);

var max_number=Math.max(first_num, secound_num,third_num);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

var max=1000;
var min=9999;
var num=Math.floor((Math.random() * max) + min);
num=""+num;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While that does convert num to a string, it indicates bad style and hurts readability (some people may even get a wrong impression about JS seeing such code).

Better to use the more readable num = String(num)


//Least Common Multiple
function printLCM(x,y) {
var lcm= (x*y) / calcGCD(x,y);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants