-3

I want to match the case of a string to that of an example string. For example.

var str = "hello world"
var example1 = "My Text Is Great"
var example2 = "my Text Is Great"
var example3 = "My text is great" 
answer1 = someGreatFunction(str,example1)//should return Hello World
answer2 = someGreatFunction(str,example2)//should return hello World
answer3 = someGreatFunction(str,example3)//should return Hello world

I hope this helps you understand what I want. I don't want to worry about quotation marks or any form of punctuation. But if it includes a support for that it will be great.

P.S. I haven't tried doing anything till now because I am clueless.

P.S. If you think this question is duplicate please don't mark it so. Maybe I had already looked through the solutions but didn't found anything satisfying.

Chief VOLDEMORT
  • 89
  • 1
  • 15
  • `P.S. I haven't tried doing anything till now because I am clueless.` Than this is the wrong place for you. `P.S. If you think this question is duplicate please don't mark it so. Maybe I had already looked through the solutions but didn't found anything satisfying.` Your question isn't showing the slightest sign of effort – baao Nov 16 '17 at 12:35
  • 1
    So what did you try? Seems like first step is how do you break it into parts. Second determine if upper or lower, third, d the conversion.... We do not do your homework for you. – epascarello Nov 16 '17 at 12:35
  • 1
    Also your rules are not clear. First letter and some subsequent letter uppercase should make Hello World whereas First letter lowercase and some subsequent letter uppercase should result in hello World? What if you have "my text Is great"? – mplungjan Nov 16 '17 at 12:42
  • Sorry friends seems like my post wasn't liked much but I tried doing everything I can but can't figure out anything. Sorry but I am not a great developer. – Chief VOLDEMORT Nov 16 '17 at 12:45
  • Interesting question! In terms of working out the algorithm, I think the quickest solution has two steps: first test the first character of the first word, by comparing it to its lowercase version. If it matches, we have a case of example2. If it doesn't match, we go to second step: a regex that drops the first word of the example text, and compares the remaining with its lowercased version. If it matches, we have exampe3, if it doesn't match, we have example1. – Theo d'Or Nov 16 '17 at 12:46
  • What if `str` has more words than `example`? – gurvinder372 Nov 16 '17 at 12:49
  • @mplungjan I don't think question marked as original is answering OP's question. That question simply wants to check if first character of every word is capital or not. – gurvinder372 Nov 16 '17 at 12:53
  • 1
    The regex and code in https://stackoverflow.com/questions/40558842/a-regex-to-test-if-all-words-are-title-case is a useful start. BTW @gurvinder372 - the reopen cleared ALL flags – mplungjan Nov 16 '17 at 13:11
  • @mplungjan Sure, I am aware :).. Just wanted a second opinion before clearing all flags which other SO veterans have put. – gurvinder372 Nov 16 '17 at 13:26

1 Answers1

0

You can use the following approach

  1. Get an array of lowerCase and upperCase indicators first

Example

"My Text Is Great".split(" ").map( s => s.toLowerCase() == s ? "L" : "U" )
  1. Use the flags at their specific indices to convert them to appropriate case.

Example

"hello world".split(/\s/).map((s, i) => (indicators[i] == "U" ? s.charAt(0).toUpperCase() : s.charAt(0).toLowerCase() ) + s.substring(1) ).join(" ");

Demo

function matchCase(input, master) {
  var indicators = master.split(" ").map(s => s.toLowerCase() == s ? "L" : "U");
  return input.split(/\s/).map((s, i) => (indicators[i] == "U" ? s.charAt(0).toUpperCase() : s.charAt(0).toLowerCase() ) + s.substring(1) ).join(" ");
}

var example1 = "My Text Is Great"
var example2 = "my Text Is Great"
var example3 = "My text is great" 

console.log( matchCase( "hello world", example1 ) );
console.log( matchCase( "hello world", example2 ) );
console.log( matchCase( "hello world", example3 ) );
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • 1
    So you wanted this poorly written question reopened so you could indulge the OP and answer it? – mplungjan Nov 16 '17 at 13:27
  • I thought I understood the question. OP didn't answered my question on edge cases like *What if str has more words than example*. But there are example inputs and outputs given. I didn't wanted to reopen this so that I could answer this, I wanted this to be reopened **because it wasn't a duplicate**. – gurvinder372 Nov 16 '17 at 13:30