Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Help Fix

  1. #1
    New Coder
    Join Date
    Aug 2016
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Help Fix

    Hey! Could anyone help me fix this code?It shoud save a letter inputed from an user and after sheach that letter in "hello" word.

    <!DOCTYPE html>
    <html>
    <body>
    <button onclick="functie ()">Age</button>
    <p id="demo"></p>

    <script>function functie () { x=prompt ("varsta");}


    var str = "Hello";
    var arr = str.search(x);
    if (arr == -1) {window.alert ("elementul nu a fost gasit")}
    else {window.alert ("elemntul a mai fost scris ")}
    document.getElementById("demo").innerHTML = arr;
    </script>

    </body>
    </html>

  2. #2
    Regular Coder
    Join Date
    Aug 2012
    Posts
    280
    Thanks
    0
    Thanked 49 Times in 49 Posts
    Not sure if this is what you're trying to do, i just encapsulated your function with all the lines you have. Note that your code will not account for the 2nd 'l'
    PHP Code:
    <!DOCTYPE html>
    <
    html>
    <
    head>
    </
    head>
    <
    body>
    <
    button onclick="functie()">Age</button>
    <
    p id="demo"></p>
    <
    script>
    "use strict";
    var 
    x;
    function 
    functie() { prompt ("varsta");
    var 
    str "Hello";
    var 
    arr str.search(x); // this is only looking at your string
    if (arr == -1) {window.alert ("elementul nu a fost gasit")}//will always show up if you don't use any letter other than "H","e","l","o" - case sensitive
    else {window.alert ("elemntul a mai fost scris ")}
    document.getElementById("demo").innerHTML arr;
    }
    </script>

    </body>
    </html> 
    Here is another version of your code that should account for multiple character index (Aici este o altă versiune a codului pentru indexul de caractere multiple)
    PHP Code:
        function functie() {
            
    "use strict";
            var 
    prompt('enter letter');
            var 
    str "Hello";
            var 
    location = [];
            var 
    showAlert true;//Alertă apel o dată
            
    for (var 0str.lengthi++) {
                if (
    str[i] === x) {
                    
    location.push(i);
                    if (
    showAlert === true) {
                        
    showAlert false;
                        
    alert("elemntul a mai fost scris");
                    }
                }
            }
            
    document.getElementById("demo").innerHTML location.join(" | ");
            if (
    str.search(x) === -1) {
                
    alert("elementul nu a fost gasit");
                
    document.getElementById("demo").innerHTML = -1;
            }
        } 
    Last edited by Brandnew; 08-03-2016 at 12:54 AM.
    1 Corinthians 15:3-4 / Ephesians 2:8-9 - What or Who are you living for? Jesus The Christ is returning very soon. All are welcomed by God...that's good news!!!


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •