-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathascii-total.js
More file actions
37 lines (22 loc) · 840 Bytes
/
ascii-total.js
File metadata and controls
37 lines (22 loc) · 840 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//8 Kyu
//ASCII Total
//Fundamentals
// You'll be given a string, and have to return the sum of all characters as an int. The function should be able to handle all printable ASCII characters.
// Examples:
// uniTotal("a") == 97
// uniTotal("aaa") == 291
//Solution
function uniTotal (string) {
//set up a variable to store the sum of character codes
let sum = 0
//loop through the str, get the character code of each str element and add it to the result variable
for(i=0; i<string.length; i++){
sum += string[i].charCodeAt()
}
//return the result variable
return sum
}
//str -> str of ascii printable characters, can be empty, wont be null or undefined, will always be a str
//num -> the sum of the character code of each character str
console.log(uniTotal("a"), 97)
console.log(uniTotal("aaa"), 291)