In: Computer Science
Write a function in JAVASCRIPT that accepts an array as argument. The function should loop through the array elements and accumulate the sum of ASCII value of each character in element and return the total. For example: function([‘A’, ‘bc’, 12]); // returns 361 which is the sum of 65 + 98 + 99 + 49 + 50
Use of any built in string functions or built in array functions is not allowed,
Any help would be much appreciated
function asciiValue(data) {
var result = 0;
for(var i=0; i<data.length; i++) {
var stringElem =
data[i].toString();
for(var j=0;
j<stringElem.length; j++) {
result
+= stringElem.charCodeAt(j);
}
}
return result;
}
alert(asciiValue(['A', 'bc', 12]));
**************************************************
Thanks for your question. We try our best to help you with detailed
answers, But in any case, if you need any modification or have a
query/issue with respect to above answer, Please ask that in the
comment section. We will surely try to address your query ASAP and
resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.