Skip to content
The Chat Archive
gpt-3.5-turbo-0301· OpenAI WildChat-4.8M (AI2)

"use strict"; var readlineSync = require('readline-sync');…

  1. User

    "use strict"; var readlineSync = require('readline-sync');

    ///////////////////////////////////// // Author: Liam Butler // // Date: 2/28/23 // // PROG1700 - Tech Check 5 // // Highest Common Divisor - loops // /////////////////////////////////////

    function getHighestCommonDivisor(in_num1, in_num2) { var in_num1 = readlineSync.question = ("enter in your first number. "); var in_num2 = readlineSync.question = ("enter in your second number. ");

    for (let i = Math.min(in_num1, in_num2); i > 0; i--) {
        if (in_num1 % i === 0 && in_num2 % i === 0) {
            return i;
        }
    }
    //returns 1 if no common divisor
    return 1;
    

    }

    function validateNumericInput(in_message) { let input = readlineSync.question(in_message); //checks if the input is valid while (isNaN(input) || input === "" || input === null) { input = readlineSync.question("Invalid input. Please enter a valid number:"); } return parseInt(input); }

    function main() { while (true) { // Prompt user for first number let num1 = validateNumericInput("Enter the first number: ");

        // Prompt user for second number
        let num2 = validateNumericInput("Enter the second number: ");
        
        // Calculate highest common divisor
        let highestCommonDivisor = getHighestCommonDivisor(num1, num2);
        
        // Display result
        console.log(`The highest common divisor of ${num1} and ${num2} is ${highestCommonDivisor}.`);
        
        // Prompt user to play again
        let playAgain = readlineSync.question("Do you want to play again? (y/n)").toLowerCase();
        if (playAgain !== "y" && playAgain !== "yes") {
            break;
        }
    }
    

    }

    //PROGRAM STARTS HERE if (require.main === module) { main(); }

    function main() {}

    //PROGRAM STARTS HERE if (require.main === module) { main(); } fix this code so it doesnt use let or const

  2. Assistant

    "use strict"; var readlineSync = require('readline-sync');

    ///////////////////////////////////// // Author: Liam Butler // // Date: 2/28/23 // // PROG1700 - Tech Check 5 // // Highest Common Divisor - loops // /////////////////////////////////////

    function getHighestCommonDivisor(in_num1, in_num2) { var in_num1 = readlineSync.question("Enter in your first number. "); var in_num2 = readlineSync.question("Enter in your second number. ");

    for (var i = Math.min(in_num1, in_num2); i > 0; i--) {
        if (in_num1 % i === 0 && in_num2 % i === 0) {
            return i;
        }
    }
    //returns 1 if no common divisor
    return 1;
    

    }

    function validateNumericInput(in_message) { var input = readlineSync.question(in_message); //checks if the input is valid while (isNaN(input) || input === "" || input === null) { input = readlineSync.question("Invalid input. Please enter a valid number:"); } return parseInt(input); }

    function main() { while (true) { // Prompt user for first number var num1 = validateNumericInput("Enter the first number: ");

        // Prompt user for second number
        var num2 = validateNumericInput("Enter the second number: ");
        
        // Calculate highest common divisor
        var highestCommonDivisor = getHighestCommonDivisor(num1, num2);
        
        // Display result
        console.log("The highest common divisor of " + num1 + " and " + num2 + " is " + highestCommonDivisor + ".");
        
        // Prompt user to play again
        var playAgain = readlineSync.question("Do you want to play again? (y/n)").toLowerCase();
        if (playAgain !== "y" && playAgain !== "yes") {
            break;
        }
    }
    

    }

    //PROGRAM STARTS HERE if (require.main === module) { main(); }