// Function that scrambles a word passed as the parameter
function wordScrambler(word) {

    // Store the length of the string at the time the function is called
    let length = word.length;

    // Create a temporary variable to store the scrambled string
    let scrambledWord = "";

    // Loop through every character in the original string
    for (let i = 0; i < length; i++) {

        // Output the current original string and the length
        console.log("Original string: " + word);
        console.log("# of letters remaining: " + word.length);
        
        // Get a random index in the string
        let index = Math.floor(Math.random() * word.length);
        
        // Store the letter to be removed according to the random index
        let letter = word[index];

        // Add the letter to the scrambled word
        scrambledWord += letter;

        // Remove the letter from the original string
        word = word.substring(0, index) + word.substring(index + 1, word.length);

        // Output the modified strings
        console.log("Newly constructed string: " + scrambledWord);
        console.log("Original string (letter removed): " + word);
        
        // Output a new line for spacing
        console.log("\n");

    }

    // Return the scrambled word
    return scrambledWord;

}

// Store a word to be scrambled
var word = "javascript";

// Call the wordScrambler function and output the return value
console.log("Function return value: " + wordScrambler(word))