JavascriptFizzBuzz: Difference between revisions

From Traxel Wiki
Jump to navigation Jump to search
(Created page with " <nowki> const JSNum = Number(process.argv[2]); // reads in the value of the argument passed in after // the name of the script and converts it to a number const remainderJav...")
 
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
  <nowki>
== Use "else if" ==
 
  <nowiki>
const JSNum = Number(process.argv[2]); // reads in the value of the argument passed in after
const JSNum = Number(process.argv[2]); // reads in the value of the argument passed in after
// the name of the script and converts it to a number
// the name of the script and converts it to a number
Line 14: Line 16:
} else {
} else {
     console.log (JSNum);  // checks if JSNum is not evenly divisible by either 3 or 5 and reports
     console.log (JSNum);  // checks if JSNum is not evenly divisible by either 3 or 5 and reports
}
</nowiki>
== Better Formatting of Nested Conditionals ==
If you do have to have nested conditionals, put the "if" on a new line, so you can align the closing curly brace with the if:
<nowiki>
const JSNum = Number(process.argv[2]); // reads in the value of the argument passed in after
// the name of the script and converts it to a number
const remainderJava = JSNum % 3; // calcs JSNum remainder when divided by 3
const remainderScript = JSNum % 5; // calcs JSNum remainder when divided bt 5
if (remainderJava === 0 && remainderScript === 0) {
    console.log ('JavaScript');  // checks if JSNum is evenly divisible by both 3 and 5 and reports
} else {
    if (remainderJava === 0) {
        console.log ('Java');  // checks if JSNum is evenly divisible only by 3 and not 5 and reports
    } else {
        if (remainderScript === 0) {
            console.log ('Script');  // checks if JSNum is evenly divisible only by 5 and not 3 and reports
        } else {
            console.log (JSNum);  // checks if JSNum is not evenly divisible by either 3 or 5 and reports
        }
    }
}
}
</nowiki>
</nowiki>

Latest revision as of 19:24, 18 October 2020

Use "else if"

const JSNum = Number(process.argv[2]); // reads in the value of the argument passed in after
// the name of the script and converts it to a number

const remainderJava = JSNum % 3; // calcs JSNum remainder when divided by 3
const remainderScript = JSNum % 5; // calcs JSNum remainder when divided bt 5

if (remainderJava === 0 && remainderScript === 0) { 
    console.log ('JavaScript');  // checks if JSNum is evenly divisible by both 3 and 5 and reports
} else if (remainderJava === 0) {
    console.log ('Java');  // checks if JSNum is evenly divisible only by 3 and not 5 and reports
} else if (remainderScript === 0) {
    console.log ('Script');  // checks if JSNum is evenly divisible only by 5 and not 3 and reports
} else {
    console.log (JSNum);  // checks if JSNum is not evenly divisible by either 3 or 5 and reports
}

Better Formatting of Nested Conditionals

If you do have to have nested conditionals, put the "if" on a new line, so you can align the closing curly brace with the if:

const JSNum = Number(process.argv[2]); // reads in the value of the argument passed in after
// the name of the script and converts it to a number

const remainderJava = JSNum % 3; // calcs JSNum remainder when divided by 3
const remainderScript = JSNum % 5; // calcs JSNum remainder when divided bt 5

if (remainderJava === 0 && remainderScript === 0) { 
    console.log ('JavaScript');  // checks if JSNum is evenly divisible by both 3 and 5 and reports
} else {
    if (remainderJava === 0) {
        console.log ('Java');  // checks if JSNum is evenly divisible only by 3 and not 5 and reports
    } else {
        if (remainderScript === 0) {
            console.log ('Script');  // checks if JSNum is evenly divisible only by 5 and not 3 and reports
        } else {
            console.log (JSNum);  // checks if JSNum is not evenly divisible by either 3 or 5 and reports
        }
    }
}