JSFiddle - React, Tailwind, and code Playground

by fullyslick

JavaScript

/**
 * Lesson 4: Functions
 * Task 2
 * URL: https://confluence.ontrq.com/display/KB/_DRAFT_Basic+level/#id-_DRAFT_Basiclevel-Lesson4:(Functions)
 *
 * Write a function, which will get a number and determinate:
 *
 * Is this number positive or negative?
 * Is this number simple?
 * Can this number be divided by 2, 5, 3, 6, 9 without the remainder?
 * Paste in console:
 */
(function() {

    var userInput,
        dividers = [2, 5, 3, 6, 9],
        isPrime = true,
        isDividedByDivider = false;

    (function promptUser() {

        userInput = prompt('Enter any number ', '');

        /**
         * Check if user has clicked on "Cancel" button of the prompt or
         * if the user clicked on "Ok" without entering a set of digit
         */
        if (userInput == null || userInput === '') {
            alert('You have not entered any number. Quit program!');
        } else {
            /**
             * Check if input is a number
             */
            if (isNaN(userInput)) {
                alert('Please use numbers!');
                promptUser();
            } else {
                userInput = Number(userInput);
                determineNum();
            }
        }
    }());

    function determineNum() {

        /**
         * Positive-negative check
         */
        if (userInput > 0) {
            alert('Your number ' + userInput + ' is positive');
        } else if (userInput < 0) {
            alert('Your number ' + userInput + ' is negative');
        } else {
            alert('You have entered 0 !');
        }

        /**
         * Prime-composite check
         */
        if (userInput > 1 && checkInt()) {
            for (var i = 2; i < userInput; i++) {
                if (userInput % i === 0) {
                    isPrime = false;
                }
            }

            if (isPrime){
                alert('Your number ' + userInput + ' is prime (simple).' );
            } else{
                alert('Your number ' +...