-5

I'm a beginner in JavaScript, and for my homework I was asked to make a code which converts uppercase in strings into lowercase, and vice versa. So if I input "HellO", the output would be "hELLo".

I am quite quite confused how to do this. Anyone have any ideas or at least any clues which functions I can use for this?

Pravesh Agrawal
  • 869
  • 1
  • 11
  • 27
  • `which functions I can use for this` try `toLowerCase()` and `toUpperCase()`. – VLAZ Sep 19 '16 at 07:25
  • 1
    Have you even done the minimal amount of work for your homework and tried to search for javascript function to change toupper and tolower case? (hint: there's a hint there!) – Jamiec Sep 19 '16 at 07:26
  • @vlaz that wont work as `toLowerCase` or `toUpperCase` will convert the whole string. – Deendayal Garg Sep 19 '16 at 07:26
  • 1
    @DeendayalGarg they're still the functions you'd need to use....How to use them is the homework itself! – Jamiec Sep 19 '16 at 07:27
  • 1
    @DeendayalGarg I didn't say to run in on the whole string. Isn't part of homework, you know, working out how to do it? – VLAZ Sep 19 '16 at 07:27
  • @vlaz Jamiec Agree. – Deendayal Garg Sep 19 '16 at 07:48
  • agreed, OP won't help him/herself without understanding what's happening and why. The tool to convert lower to upper and vice versa is given, now try to solve it yourself – Nelson Sep 26 '16 at 12:24

5 Answers5

0

var testString = 'HellO',
      output;

      output= testString.replace(/([a-zA-Z])/g, function(a) {
      return String.fromCharCode(a.charCodeAt() ^ 32);
       })

     document.body.innerHTML= output;
     
     var testString = 'HellO',
      output;

      output= testString.replace(/([a-zA-Z])/g, function(a) {
      return String.fromCharCode(a.charCodeAt() ^ 32);
       })

     document.body.innerHTML= output;

check this link Reference

Community
  • 1
  • 1
Nikhil Ghuse
  • 1,258
  • 14
  • 31
0

You can try something like this

var variable ="myStrinG";
var newArray=[]
var getArray = variable.split('');
getArray.forEach(function(item){
 if(item == item.toUpperCase()){
    var conCharacter = item.toLowerCase();
    newArray.push(conCharacter);
 }
else{ 
   var conCharacter = item.toUpperCase();
   newArray.push(conCharacter);
}
})
var getNewString = newArray.join('')
document.write('<pre>'+getNewString+'</pre>')

JSFIDDLE

brk
  • 48,835
  • 10
  • 56
  • 78
0

Sample code snippet that solves your problem :)

function strcon() {
        var b = '';
        var a = "This Is A Sample String";
        for (i = 0; i < a.length; i++) {
            if (a.charCodeAt(i) >= 65 && a.charCodeAt(i) <= 90) {
                b = b + a.charAt(i).toLowerCase();
            }
            else
                b = b + a.charAt(i).toUpperCase();
        }
        alert(b);
    }
g.005
  • 396
  • 1
  • 12
0

I would split the string into an array, then make a for loop that has an if statement that checks if the uppercase/lowercase is true. Then stick it back together.

var input_string = "HellO";
var string_arr = input_string.split("");
var output = [];

for(i = 0;i < string_arr.length;i++){
    if(string_arr[i] == input_string[i].toUpperCase()){
        string_arr[i] = string_arr[i].toLowerCase();
        }
    else{
        string_arr[i] = input_string[i].toUpperCase();
        }
} 

output = string_arr.join("");
dwellsdev
  • 11
  • 1
0

Here is my code for this question

const s = (str) =>{
    let error = false;
    let errorMessage = ""
    let total = "";
    if(str != null && str != ""){
        error = false;
        let array = str.split("")
        array.forEach((element=>{
            if(element != element.toUpperCase()){
                total += element.toUpperCase();
            }else{
                total += element.toLowerCase();
            }
        }))
    }else{
        error = true;
        errorMessage = "Please put a sentance or word inside str perimeter"
        return errorMessage;
    }
    return total;
}

// Calling Function
console.log(s("Hello World"));
Evil-Coder
  • 11
  • 1
  • 4