0

Check the divisibility of 2, 3, and both 2 and 3 Divisible by 2 - print x Divisible by 3 - print y Divisible by 2 and 3 - print xy

let num = prompt('1-1000');
if (num %2 == 0 )
  {
    console.log("X")
  }

if (num %3 == 0 )
  {
    console.log("Y")
  }


if (num %3 == 0 && num %2 ==0)
  {
    console.log("XY")
  }

Example: Input: 6 Output:

X
Y
XY
  1. How to make it not print X, Y but XY?
  2. What needs to be done so that several digits to be checked after the decimal point can be entered and then converted to the appropriate letters?
bloodyKnuckles
  • 11,551
  • 3
  • 29
  • 37
greenuser
  • 1
  • 1

1 Answers1

0

There are a couple of ways you could do this.

One way would be to use a ternary operator to output the values X and/or Y. This will have X logged only when divisible by 2, and Y only logged when divisible by 3. If it is divisible by both, both are shown but their conditions are independent of each other. And using template literals it is concatenated into 1 value.

let num = prompt('1-1000');
console.log(`${(num%2==0)?"X":""}${(num%3==0)?"Y":""}`);

Alternatively, you could use something like what was mentioned in the comments. If you convert the code to a function, you can move the final if statement to the top and use exit/return statements to return only 1 of the values (whichever applies).

const _Check = () => {
  let num = prompt('1-1000');
  
  if(num % 3 == 0 && num % 2 == 0) return "XY";
  if(num % 2 == 0 ) return "X";
  if(num % 3 == 0 ) return "Y";
  return "";
}
console.log(_Check());

EDIT

In reponse to a comment by OP: checking for multiple numbers could be done by splitting the value input by the user and then looping through those values.

If the user will be inputting each number separated by a space, you can use the split() method with a space as the separator. And then use forEach() to loop through each number and apply the code/logic we have used earlier.

Example Using ternary operator and template literals:

let num = prompt('1-1000')
num.split(" ").forEach(n => {
  console.log(`${(n%2==0)?"X":""}${(n%3==0)?"Y":""}`);
})

Example using a function and return statements:

let num = prompt('1-1000')

const _Check = n => {
  if(n %3 == 0 && n %2 ==0) return "XY";
  if(n %2 == 0 ) return "X";
  if(n %3 == 0 ) return "Y";
  return "";
}

num.split(" ").forEach(n => {
  console.log(_Check(n));
})
EssXTee
  • 1,783
  • 1
  • 13
  • 18
  • how to do so that you can check several numbers? By typing them after a space. Input: 4 2 6 Output: X X XY – greenuser Oct 10 '22 at 16:35
  • @greenuser I have edited my answer to include a version that loops through multiple numbers (separated by a space). This checks each number and logs a result for each (each on their own line). If you need all results on the same line (separated by a space), you could just store each result in a string instead of logging to the console, then use `console.log` after the loop. – EssXTee Oct 10 '22 at 17:08
  • what is the best way to make exceptions? for example, not handling the number 0, special characters, letters when I type "1" it returns undefined; and a digit restriction from 1 to 1000 only – greenuser Oct 10 '22 at 17:21
  • @greenuser Seeing `undefined` means you are using the function/return version. This only returns if it matches of the conditions, which means it can be fixed by adding one final return statement at the end like `return ""`, which returns an empty string (nothing) if the number is not divisible by 2 or 3. The first version/example I gave with template literals does not display undefined for 1. – EssXTee Oct 10 '22 at 18:19
  • As for how to restrict the values to digits 1-1000 only, this is turning into a separate question. There are numerous ways to validate/filter inputs. I would probably look into [regular expressions](https://stackoverflow.com/questions/19715303/regex-that-accepts-only-numbers-0-9-and-no-characters) to filter out non-numeric values. As for the range, you would use some `if` statement logic to determine if the number was outside of the range or not and either continue or return a blank value (or an error message). Typically, validation like this would be for an `` and not a `prompt` – EssXTee Oct 10 '22 at 18:22