Skip to content

Commit

Permalink
Completed UTF16Value
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinctofel committed Apr 12, 2022
1 parent e6bd984 commit ab94b3f
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions UTF-16StringValue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Write a function that determines and returns the UTF-16 string value of a
// string passed in as an argument. The UTF-16 string value is the sum of the
// UTF-16 values of every character in the string. (You may use
// String.prototype.charCodeAt() to determine the UTF-16 value of a character.)

const utf16Value = (string1) => {
let sum = 0;
for (const element of string1) {
sum += element.charCodeAt(element);
}
return sum;
};

console.log(utf16Value('Four score')); // 984
console.log(utf16Value('Launch School')); // 1251
console.log(utf16Value('a')); // 97
console.log(utf16Value('')); // 0

const OMEGA = "\u03A9"; // UTF-16 character 'Ω' (omega)
console.log(utf16Value(OMEGA));

0 comments on commit ab94b3f

Please sign in to comment.