2

I want to split lower, upper & also the value of textBox without using .split() and also I want to find the length of the string without using .length. Can anybody solve my problem I am tried but I cannot find the exact logic for this problem.

var lowercase = "abcdefghijklmnopqrstuvwxyz";
var uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

function Print() {
  var input = document.getElementById('demo').value;
  document.write(document.getElementById('demo1').innerHTML = toUpper(input));

}

function toUpper(input) {
  var upperCase = uppercase.split(""); //other way to split uppercase
  var lowerCase = lowercase.split(""); //other way to split lowercase
  var inputText = input.split(""); //other way to split input
  var newText = "";
  var found;

  for (var i = 0; i < inputText.length; i++) { //not using .length to other way to find the size of inputText
    found = false;
    for (var ctr = 0; ctr < lowerCase.length; ctr++) { //not using .length other way to find the size of lowerCase
      if (inputText[i] == lowerCase[ctr]) {
        found = true;
        break;
      }
    }
    if (found) { //true
      newText = newText + upperCase[ctr];

    } else {
      newText = newText + inputText[i];
    }
  }
  return newText;
}
Eddie
  • 26,593
  • 6
  • 36
  • 58

4 Answers4

1

You can count the length of a string using the array function reduce.

Reduce loops over all elements in an array and executes a function you give it to reduce it to one value, you can read more here. To get reduce working on strings, you need to use Array.from, like this:

Array.from(lowerCase).reduce((sum, carry) => sum + 1, 0) // 26

Reduce accepts a starting argument, which we set to zero here.

This way you do not need to use the split or length functions.

You don't need to check if the input is in a string either, you can use charCodeAt() and fromCharCode().

If you take your input and loop through it using Array.from() then forEach, you can get something which looks like this:

    function print() {
        const input = document.querySelector('#input').value;

        document.querySelector('#target').value = stringToUpper(input);
    }

    function stringToUpper(input) {
        let output = "";

        Array.from(input).forEach(char => output += charToUpper(char));

        return output;
    }

    function charToUpper(char) {
        let code = char.charCodeAt(0);

        code >= 97 && code <= 122 ? code -= 32 : code;

        return String.fromCharCode(code);
    }
<div>
    <input id="input" placeholder="enter text here">
</div>
<button onclick="print()">To Upper</button>
<div>
    <input id="target">
</div>

The key line is where we take the output and add the char (as upper) to it:

output += charToUpper(char)

If you don't know about arrow functions, you can read more here

This line:

        code >= 97 && code <= 122 ? code -= 32 : code;

is just checking if the char is lower case (number between 97 and 122) and if so, subtracting 32 to get it to upper case.

The reason it is subtract not add is in utf-16, the chars are laid out like this:

ABCDEFGHIJKLMNOPQRTUWXYZabcdefghijklmnopqrtuwxyz

See here for more

Leon Segal
  • 679
  • 7
  • 28
0

I don't know what you mean by "split the value of textBox", but one way to determine the length of a string without using .length would be to use a for...of loop and have a counter increment each time it runs to keep track of the number of characters in the string.

let string = 'boo'
let lengthCounter = 0
for (let char of string) {
  lengthCounter++
}
//lengthCounter = 3

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of

Emilio
  • 1,814
  • 3
  • 9
  • 11
0

You can define your own split and length functions:

function mySplit(a){
  var counter = 0;
  rslt = [];
  var val = a[counter];
  while(typeof val != "undefined"){
    rslt.push(a[counter]);
    counter ++;
    val = a[counter];
  }
  return rslt;
}

function myLength(a){
    var counter = 0;
    var val = a[counter];
    while(typeof val != "undefined"){
      counter ++;
      val = a[counter];
    }
    return counter;
}

Your function now should be like:

function toUpper(input) {
  var upperCase = mySplit(uppercase);
  var lowerCase = mySplit(lowercase);
  var inputText = mySplit(input);
  var newText = "";
  var found;

  for (var i = 0; i < myLength(inputText); i++) { 
    found = false;
    for (var ctr = 0; ctr < myLength(lowerCase); ctr++) { 
      if (inputText[i] == lowerCase[ctr]) {
        found = true;
        break;
      }
    }
    if (found) { //true
      newText = newText + upperCase[ctr];

    } else {
      newText = newText + inputText[i];
    }
  }
  return newText;
}
Said
  • 473
  • 7
  • 11
  • I have a question what is the purpose of this variable rslt = []; ? – LizQuen Update Jul 08 '18 at 00:08
  • rslt is an array variable I used it to store results of splitted string and this is just a way of defining the array, you might find this question helpfull. https://stackoverflow.com/questions/931872/what-s-the-difference-between-array-and-while-declaring-a-javascript-ar – Said Jul 08 '18 at 00:19
0

The simplest way would be to just use the build in function of javascript .toUpperCase() (see example 1). https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase

Else if you insist on using a for.loop you may do so aswell (see example two). You do not need the split() function since a string already is an arrayof characters. Also be aware that not all characters in the web have lowercase counterparts, so the logic itself is flawed.

//REM: This lines are not required.
/*
var lowercase = "abcdefghijklmnopqrstuvwxyz";
var uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

function Print() {
  var input = document.getElementById('demo').value;
  document.write(document.getElementById('demo1').innerHTML = toUpper(input));

}
*/

//REM: Version 1 (using string.toUpperCase())
(function toUpper1(input){
  var tReturn = (input || '').toUpperCase();
  console.log('toUpper1', tReturn);
  return tReturn
}('abcDEFghiJKL'));

//REM: Version 2 (using your way)
(function toUpper2(input){
  var tReturn = '';
  
  if(input && input.length){
    for(let i=0, j=input.length; i<j; i++){
      tReturn += (input[i] === input[i].toLowerCase()) ? input[i].toUpperCase() : input[i]
    }
  };
  
  console.log('toUpper2', tReturn);
  return tReturn
}('abcDEFghiJKL'));
Lain
  • 3,657
  • 1
  • 20
  • 27