JavaScript Alphabet values

Toggle class name on click in jQuery

by jimmyt1001

HTML

<h1>Alphabet Values</h1>
<pre id="output"></pre>

<p>
The JavaScript code consists of two parts: a function called assignValuesToAlphabet, 
this creates an object mapping each letter of the English alphabet 
to its corresponding position (1 for 'a', 2 for 'b', ..., 26 for 'z'), 
and a set of unit tests using a testing framework to verify that the 
function will work as expected.</p>

<p>
Function Definition: assignValuesToAlphabet:
</p>
<p>
This function initializes an empty object alphabetValues.<br/>
A variable value is initialized to 1.<br/>
A for loop iterates from 0 to 25 (26 iterations):<br/>
<p>
For each iteration, it calculates the corresponding letter by 
using String.fromCharCode(97 + i), 
<br/>
where 97 is the ASCII value of 'a'. 
In each iteration, i is incremented, which  generates the letters from 'a' to 'z'.
<br/>
Each letter is added to the alphabetValues object with its corresponding value 
(1 for 'a', 2 for 'b', ..., 26 for 'z').
Finally, the function returns the alphabetValues object.
</p>

<p>
Calling the Function:
</p>
<p>
The result of the function call assignValuesToAlphabet() is stored in the alphabetValues variable.
</p>

<p>
Unit Tests:

<ul>


<li>The describe block defines a test suite for the assignValuesToAlphabet function.</li>

<li>Two tests are defined using it:</li>

<li>The first test checks if the value for the letter 'a' is 1 using expect(alphabetValues.a).toBe(1).</li>

<li>The second test checks if the value for the letter 'z' is 26 using expect(alphabetValues.z).toBe(26).</li>

<li>What the Code Produces</li>
<li>Output of assignValuesToAlphabet(): When the function is called, it produces the example object above:</li>
</ul>
</p>

CSS

body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        pre {
            background-color: #f4f4f4;
            padding: 10px;
            border: 1px solid #ccc;
        }

JavaScript

function assignValuesToAlphabet() {
        const alphabetValues = {};
        let value = 1;

        for (let i = 0; i < 26; i++) {
            const letter = String.fromCharCode(97 + i); // 97 is the ASCII code for 'a'
            alphabetValues[letter] = value;
            value++;
        }

        return alphabetValues;
    }

    const alphabetValues = assignValuesToAlphabet();

    // Displaying the output in the HTML page
    const outputElement = document.getElementById('output');
    outputElement.textContent = JSON.stringify(alphabetValues, null, 2); // Formatting the output for readability values directly on the web page.

    outputElement.textContent += '\n\nValue for "a": ' + alphabetValues.a;
    outputElement.textContent += '\nValue for "z": ' + alphabetValues.z;