Skip to main content

Coding Challenge React Programming : String Manipulation Coding Challenge - 1!

String Manipulation Coding challenges using React-Typescript

Requirement:

Given a string may contain spaces, convert them to

  • All first character caps
  • All Even numbers caps
  • All Odd Numbers small
  • Consider the strings are starting with 0 - Even number

 Stack Blitz Console:

import React, { Component } from 'react';
import { render } from 'react-dom';
import Hello from './Hello';

import './style.css';

interface AppProps {}
interface AppState {
  namestring;
  inputstring;
  resultstring;
}

class App extends Component<AppPropsAppState> {
  bResultany;
  constructor(props) {
    super(props);
    this.state = {
      name: 'React Programming Coding Challenge: String Manipulation Coding Challenge - 1',
      input: '',
      result: 'Result: No Result, input string to process'
    };
    this.handleChange = this.handleChange.bind(this);
    this.convertClicked = this.convertClicked.bind(this);
    this.processString = this.processString.bind(this);
  }
  convertClicked = (eany=> {
    //alert(this.state.result);
    this.setState({
      result: this.processString(e)
    });
    e.preventDefault();
  };

  handleChange = (eany=> {
    this.setState({
      input: e.target.value
    });
  };

  processString = (eany=> {
    let returnValue = '';
    let splitted = this.state.input.split(' ');
    //alert(this.state.input);
    for (let i = 0i < splitted.lengthi++) {
      returnValue += this.applyLogic(esplitted[i]);
      if (i == splitted.length - 1) {
      } else {
        returnValue += ' ';
      }
    }
    return returnValue;
  };

  applyLogic = (eanystrstring=> {
    let res = '';
    for (let i = 0i < str.lengthi++) {
      if (i == 0) {
        res += str.charAt(i).toUpperCase();
      } else if (i == 1) {
        res += str.charAt(i).toLowerCase();
      } else if (i % 2 == 0) {
        res += str.charAt(i).toUpperCase();
      } else {
        res += str.charAt(i).toLowerCase();
      }
    }
    return res;
  };

  render() {
    return (
      <div>
        <Hello name={this.state.name} />
        <p>String Manipulation Coding challenges using React-Typescript</p>
        <div>
          <p>Given a string may contain spaces, convert them to</p>
          <ul>
            <li>All first character caps</li>
            <li>All Even numbers caps</li>
            <li>All Odd Numbers small</li>
            <li>Consider the strings are starting with 0 - Even number</li>
          </ul>
        </div>

        <div>
          <form>
            <label>
              Enter the Text:
              <input
                type="text"
                value={this.state.input}
                onChange={this.handleChange}
                name="inputValue"
              />
            </label>
            {/* <input type="submit" value="Submit" /> */}
            <button onClick={this.convertClicked}>Convert!</button>
            <br />
            <label>{this.state.result}</label>
          </form>
        </div>
      </div>
    );
  }
}

render(<App />document.getElementById('root'));


 

 The complete source code can be found here

Comments

Popular posts from this blog

Task Parallel Library (TPL) and Akka.NET: Differences

Task Parallel Library (TPL) and Akka.NET are both powerful tools in the .NET ecosystem for handling parallelism and concurrency, but they serve different purposes and use different models of computation. Here are some key differences:s 1.    Actor Model vs Task-Based Model: Akka.NET is built around the actor model, where actors are the fundamental units of computation and they communicate by exchanging messages. TPL, on the other hand, is task-based. It's designed to make developers more productive by simplifying the process of adding parallelism and concurrency to applications. TPL uses tasks (which are independently executing work units) and provides various ways to control and coordinate them. 2.    Fault Tolerance: One of the key features of Akka.NET is its built-in fault tolerance. It has a "let-it-crash" philosophy, where the system is designed to self-heal from errors. If an actor fails, its parent actor can decide on the supervision strategy: either to resta

Extension Methods - Advanced

Here we will see how can we use the Extension Methods in advanced manner in other types Imagine you often need to retrieve items from a List based on a custom criterion that the built-in LINQ methods don't cover. Extension Methods for Lists: Filtering based on Custom Criteria And the output would be   Extending Enums: Displaying Descriptive Strings Output: Extending DateTime: Calculating Age     Output: The code samples can be found at https://github.com/oneananda/C_Sharp_Examples/tree/main/ExtensionMethods