-4

I'm gonna write a function that do this thing:

foo("abcdefgh") --> "A-Bb-Ccc-Dddd-Eeeee-Ffffff-Ggggggg-Hhhhhhh" // and so on

function foo(bar) {
    return(
        bar.charAt(0).toUpperCase(),"-",
        bar.charAt(1).toUpperCase(),bar.charAt(1).toLowerCase(),"-",
        bar.charAt(2).toUpperCase(),bar.charAt(2).toLowerCase(),bar.charAt(2).toLowerCase(),"-",
        bar.charAt(3).toUpperCase(),bar.charAt(3).toLowerCase(),bar.charAt(3).toLowerCase(),bar.charAt(3).toLowerCase(),"-",
        bar.charAt(4).toUpperCase(),bar.charAt(4).toLowerCase(),bar.charAt(4).toLowerCase(),bar.charAt(4).toLowerCase(),bar.charAt(4).toLowerCase()
        // and so on infinitely
    );
}

foo("sadfl");

which code result is:

S-Aa-Ddd-Ffff-Lllll

However, I need to write a code like a code above using Loop but I don't know how code it. bar is not limited and it can be a number infinitely. like this:

for (i = 0; i < bar.length; i++) {
    // a code like that
}
zondo
  • 19,901
  • 8
  • 44
  • 83
Mahdir
  • 121
  • 1
  • 3
  • 8
  • Is the input always going to be **all lowercase?** –  Jul 11 '16 at 03:05
  • No. the first letter must be uppercase then another letters must be lowercase. @The_Grits – Mahdir Jul 11 '16 at 03:37
  • 2
    Whoever is serial downvoting the answers (@KeithNicholas ?) should realize that it will do nothing to motivate deletion. If the post isn't deleted, it will only confuse future readers and make future answers stand out above current ones. – 4castle Jul 11 '16 at 03:47

7 Answers7

1

Try like this

var str = "ASDSFhd";
var fStr = "";
for (i = 0; i < str.length; i++) {
  for (var j = 0; j <= i; j++) {
      fStr+= j == 0 ? str[i].toUpperCase() : str[i].toLowerCase() ;
  }
  fStr+="-"
}

console.log(fStr.slice(0, -1));

demo

Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
  • Thank you. But when we get it "ASDSFhd", we give this : A-SS-DDD-SSSS-FFFFF-Hhhhhh-Dddddd but i need this : A-Sa-Ddd-Ssss-Fffff-Hhhhhh-Dddddd – Mahdir Jul 11 '16 at 03:08
  • 1
    @user3640616, The detail about always needing 1 upper case letter followed by all lower case letters regardless of the input should be placed in your question, not just in a comment on one of the answers (or even in a comment on the question). When you have realized that your question does not cover some situations, the new information needs to be placed **in the question** (in addition to the comment). Other people answering your question should not need to go reading the comments on answers to fully understand the requirements of your question. **All information should be in the question.** – Makyen Jul 11 '16 at 03:52
  • Yeah, you're right, but i told some examples and it was really cretain to understand what my question and purpose is. Although, I should tell what exactly I needed without give examples. Thanks for mention @Makyen – Mahdir Jul 11 '16 at 04:04
  • @Mewtei, Please use words *and* examples. Sometimes it is difficult to describe a situation in words or provide complete examples. It is *often* helpful to have both. Having both provides additional clarity and a way to cross-check to verify that the intended information was communicated. – Makyen Jul 11 '16 at 04:21
  • Rather than `fStr.substr(0,fStr.length-2)` you could just do `fStr.slice(0, -1)` – 4castle Jul 11 '16 at 05:39
0

A highly functional way to do it using ES6 would be:

let expand = str =>
  Array.from(
    str.toLowerCase(),
    (char, index) => char.toUpperCase() + char.repeat(index)
  ).join("-");

console.log(expand("abcDEF"));

And if you like golfing (73 bytes):

f=s=>[...s.toLowerCase()].map((c,i)=>c.toUpperCase()+c.repeat(i)).join`-`
4castle
  • 32,613
  • 11
  • 69
  • 106
0

This is probably what your looking for. Although code could be optimized a bit more.

function foo(bar) {
  var finalStringArray = [];
  for (i = 0; i <= bar.length - 1; i++) {
    var composedString = "";

    for (var b = 0; b <= i; b++) {
      if (b !== 0) {
        composedString += bar.charAt(i).toLowerCase();
      } else {
        composedString += bar.charAt(i).toUpperCase();
      }

    }
    finalStringArray.push(composedString);


  }

  return finalStringArray.join("-");
}
David Yue
  • 713
  • 7
  • 24
-1
function foo(bar) {
  var answer = "";
  for (i = 0; i < bar.length; i++) {
    answer += bar.charAt(i).toUpperCase();
    for (n = 0; n < i; n++) {
      answer += bar.charAt(i);
    }
    if (i != bar.length - 1)
      answer += "-";
  }
  return answer;
}
brianxautumn
  • 1,162
  • 8
  • 21
-1

If you can use ES6 syntax (depends on your needs/requirements), then this can be done with a single for loop.

function foo(bar) {
  var i, char;
  var output = '';
  
  // Loop through all characters of input string
  for(i = 0; i < bar.length; i++) {
    char = bar.charAt(i);
    // Add current character to output (uppercased) followed by i copies (lowercased) and ending with a hyphen/dash
    output += char.toUpperCase() + char.toLowerCase().repeat(i) + '-';
  }

  // Return output, after removing trailing hyphen/dash
  return output.replace(/-$/, '');
}


$('#submit').click(function() {
  $('#output').text(foo($('#input').val()));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="input" vlue= "" />
<button id="submit">Submit</button>
<pre id="output"></pre>
rexmac
  • 1,584
  • 10
  • 14
-1

Hope that helps you:

function foo(bar) {
    var result = "", i, j;

    for(i = 0; i < bar.length; i++) {

        result += bar.charAt(i).toUpperCase();

        for(j = 1; j < i + 1; j++) {
            result += bar.charAt(i).toLowerCase();
        }

        if (i < bar.length - 1) {
            result += "-";
        }
    }

    return result;
}

console.log(foo("sadfl"));
-1

Something like this?

function foo(bar) {
    var output = '';

    for (i = 0; i < bar.length; i++) {
        output += bar.charAt(i).toUpperCase() + Array(i+1).join(bar.charAt(i).toLowerCase()) + '-';
    }
    return output.slice(0, -1);
}

console.log(foo("sadfl"));
Andrew
  • 42
  • 5
  • Downvoting a solution and commenting that someone else's answer is right makes it sound like this solution is wrong; this solution works based on what you asked and is done with fewer lines of code. – Andrew Apr 14 '18 at 22:03