I'm looking to convert a string containing a name into numerical values for each character, ultimately finding the sum of all characters' numerical values. Currently, only the first character's value is being summed using .charAt(). To achieve this goal, I need to:
- Create a function with a loop to iterate through each character in the string.
- Implement conditionals to determine the numerical value of each character and aggregate it in the variable nameSum.
- Use console.log to display both the name and its corresponding numerical value.
- Handle cases of uppercase and lowercase letters later on.
- In the end, I aim to retrieve names from an array containing variables with strings as names.
var name1 = 'bill';
const findNameVal = (name) => {
let nameSum = 0;
let cardCheck = name;
for (let i=0; i < cardCheck.length; i++) {
if (name1.charAt(i) === 'a') {
return nameSum += 1;
} else if (name1.charAt(i)=== 'b') {
return nameSum += 2;
} else if (name1.charAt(i)=== 'i') {
return nameSum += 9;
} else if (name1.charAt(i)=== 'l') {
return nameSum += 12;
}
}
}
console.log(`The name is: ${name1} and the numerical value is ${findNameVal(name1)}`);