Boby Marley
Boby Marley

Reputation: 43

Convert Letters to Numbers with space

Hi i will be using multiple sequences so i used this method. Right now it gets the appropriate number for the letters and i can type words and it sums the letters but i get NaN when using space. How can i make it possible to type words with spaces without getting NaN?

<html>
<body>
Word: <input type="text" name="fname" id="txt" value="Type a word..."><br><br>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
</body>
</html>

function myFunction() {
var alphabet = {
a: 1, b: 2,
c: 3, d: 4,
e: 5, f: 6,
g: 7, h: 8,
i: 9, j: 10,
k: 11, l: 12,
m: 13, n: 14,
o: 15, p: 16,
q: 17, r: 18,
s: 19, t: 20,
u: 21, v: 22,
w: 23, x: 24,
y: 25, z: 26 
}
var str = document.getElementById("txt").value;
var total = 0;
for (var i = 0; i < str.length; i++)
total += alphabet[str[i]];
alert(total);
document.getElementById("demo").innerHTML = total;
}

Upvotes: 1

Views: 153

Answers (3)

Nina Scholz
Nina Scholz

Reputation: 386570

You could use a zero as default value for not defined letters, like space or other characters.

This works with a logical OR ||, which test the first operand and if it is a falsy, like undefined or zero or empty space '' (as some others), it take the other operand (in this case zero) as value for adding to total.

total += alphabet[str[i]] || 0;
//                        ^^^^

function myFunction() {
    var alphabet = { a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9, j: 10, k: 11, l: 12, m: 13, n: 14, o: 15, p: 16, q: 17, r: 18, s: 19, t: 20, u: 21, v: 22, w: 23, x: 24, y: 25, z: 26 },
        str = document.getElementById("txt").value,
        total = 0;

    for (var i = 0; i < str.length; i++) {
        total += alphabet[str[i]] || 0;
    }

    document.getElementById("demo").innerHTML = total;
}
Word: <input type="text" name="fname" id="txt" placeholder="Type a word..."><br><br>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>

Upvotes: 2

pretzelhammer
pretzelhammer

Reputation: 15105

You can add a ' ' (space key) to your alphabet map and give it a value of 0.

function myFunction() {
var alphabet = {
a: 1, b: 2,
c: 3, d: 4,
e: 5, f: 6,
g: 7, h: 8,
i: 9, j: 10,
k: 11, l: 12,
m: 13, n: 14,
o: 15, p: 16,
q: 17, r: 18,
s: 19, t: 20,
u: 21, v: 22,
w: 23, x: 24,
y: 25, z: 26,
' ': 0
};
var str = document.getElementById("txt").value;
var total = 0;
for (var i = 0; i < str.length; i++)
total += alphabet[str[i]];
alert(total);
document.getElementById("demo").innerHTML = total;
}
Word: <input type="text" name="fname" id="txt" value="Type a word..."><br><br>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>

Upvotes: 1

Bucket
Bucket

Reputation: 7521

Map a value for ' ' in your alphabet object:

var alphabet = {
    a: 1, b: 2,
    c: 3, d: 4,
    e: 5, f: 6,
    g: 7, h: 8,
    i: 9, j: 10,
    k: 11, l: 12,
    m: 13, n: 14,
    o: 15, p: 16,
    q: 17, r: 18,
    s: 19, t: 20,
    u: 21, v: 22,
    w: 23, x: 24,
    y: 25, z: 26,
    ' ': 42 
}

Upvotes: 2

Related Questions