Skip to content

Instantly share code, notes, and snippets.

@ivanji
Created May 30, 2019 10:14
Show Gist options
  • Save ivanji/9ad10562e0de01a71fdffb0e4f3bd0fe to your computer and use it in GitHub Desktop.
Save ivanji/9ad10562e0de01a71fdffb0e4f3bd0fe to your computer and use it in GitHub Desktop.
// source https://jsbin.com
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
function accum(s) {
/*
a place to store result
iterate over s
// append the current letter times i+1 to the result
// capitalise first letter
// append a dash if not last letter
*/
let result = "";
for (let i = 0; i < s.length; i++) {
const counter = i +1;
result += s[i].toLowerCase().repeat(counter)
if (i !== s.length -1) {
result += "-"
}
}
return result.replace(/(^\w)|\-\w/g, function(c) { return c.toUpperCase(); })
}
console.log(accum("hola"))
</script>
<script id="jsbin-source-javascript" type="text/javascript">function accum(s) {
/*
a place to store result
iterate over s
// append the current letter times i+1 to the result
// capitalise first letter
// append a dash if not last letter
*/
let result = "";
for (let i = 0; i < s.length; i++) {
const counter = i +1;
result += s[i].toLowerCase().repeat(counter)
if (i !== s.length -1) {
result += "-"
}
}
return result.replace(/(^\w)|\-\w/g, function(c) { return c.toUpperCase(); })
}
console.log(accum("hola"))</script></body>
</html>
function accum(s) {
/*
a place to store result
iterate over s
// append the current letter times i+1 to the result
// capitalise first letter
// append a dash if not last letter
*/
let result = "";
for (let i = 0; i < s.length; i++) {
const counter = i +1;
result += s[i].toLowerCase().repeat(counter)
if (i !== s.length -1) {
result += "-"
}
}
return result.replace(/(^\w)|\-\w/g, function(c) { return c.toUpperCase(); })
}
console.log(accum("hola"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment