Results 1 to 2 of 2

Thread: Java script

  1. #1
    loca93 is offline Senior Member Always Around
    Join Date
    Feb 2007
    Location
    New York
    Posts
    1,172

    Default Java script

    well I don't know if anyone here knows Java coding, but if anyone does can you please help me with the following problem.

    "Given a String with a length >= 2 and even.
    If an 'a' occurs in the String thenn place a space on both sides of the 'a'. If the 'a' is not in the string then split the string in half."

    i have to use if and else


  2. #2
    Heylow is offline Member Frequent Poster
    Join Date
    Jan 2006
    Location
    London
    Posts
    53

    Default

    EDIT: Just now noticed, that this question is dated half a year ago , anyways. If you still needed the help, there ya go.

    Hi

    I don't really think programming issues was what this section of the forum was created for, but it is a computer related, so I'll answer it

    First off, what you are asking only requires basic knowledge of JS, and even though I haven't done JS in a few years, I was able to whip up the below code.
    I suggest you learn the language, as no one usually bothers answering such questions without the author at least trying to something on his/her own.
    If you want to learn JS, there are plenty on books and websites to explore.
    I would suggest W3Schools.com.


    [CODE]
    <script type="text/javascript">

    var txt = "cake"; // text to be manipulated, try writing a word with no "a"

    // txtNew for new word for a with spaces
    // txtNewP1 and txtNewP2 for the 2 halves of the text

    var txtNew, txtNewP1, txtNewP1;

    //checking text length
    if(txt.length>=2)
    {
    //checking if there is an "a" stored in txt
    if(txt.match("a") != null)
    {
    txtNew = txt.replace("a"," a "); //replacing "a"

    document.write("<br>" + txtNew);
    }
    else
    {
    txtNewP1 = txt.slice(0,txt.length/2); //storing first half of txt
    txtNewP2 = txt.slice(txt.length/2); //storing second half of txt

    document.write("<br>New text part 1: " + txtNewP1);
    document.write("<br>New text part 2: " + txtNewP2);
    }

    }

    </script>
    [/CODE]

    As soon as you trust yourself, you will know how to live. By Johann Wolfgang von Goethe.

 

 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
vBulletin Skin by: ForumThemes.com
Powered by vBulletin® Version 4.2.0
Copyright © 2014 vBulletin Solutions, Inc. All rights reserved.
SEO by vBSEO 3.6.0 PL2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162