TheShadowGamer

Is there any way to do this?

21 posts in this topic

So what I want to do is make it so the user enters a string. For example a prompt comes up and the user enters

 

"I want to do something"

 

into the text box. Is there a way to make each word into its own variable so that they would become

 

var I

var want

var to

var do

var something

 

so that I can then check length and change each individually?

 

Trying to find a way, but all I can find is checking length of an entered string, but not the ability to check each word individually.

Edited by TheShadowGamer

Share this post


Link to post
Share on other sites

Place each individual word into an array using split(). then by using index choose individual words to check length and adjust to your needs. https://www.w3schools.com/jsref/jsref_split.asp

<!DOCTYPE html>
<html>
<body>

<p>Click the button to display the array values after the split.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var str = "How are you doing today?";
    var res = str.split(" ");
    document.getElementById("demo").innerHTML = res;
    alert("'"+res[0]+"'" + " length is: "+res[0].length);
    alert("'"+res[1]+"'" + " length is: "+res[1].length);
    alert("'"+res[2]+"'" + " length is: "+res[2].length);
    alert("'"+res[3]+"'" + " length is: "+res[3].length);
    alert("'"+res[4]+"'" + " length is: "+res[4].length);
}
</script>

</body>
</html>
Edited by dsonesuk

Share this post


Link to post
Share on other sites

 

Place each individual word into an array using split(). then by using index choose individual words to check length and adjust to your needs. https://www.w3schools.com/jsref/jsref_split.asp

Currently what I can't figure out is how to record them rather than just saying the lengths.

 

I need to be able to do this

 

If a word is five or more letters long, add “-blip” to the end of the word

If a word is less than five letters long, add “-clang” to the end of the word.

 

It's the only part of the program that I'm completely dumbfounded on.

Edited by TheShadowGamer

Share this post


Link to post
Share on other sites

Every string has a length property. You can loop through the array check the length of the string and append something to it based on the length. Once you're done, you can turn the array back into a string using the join() method.

Share this post


Link to post
Share on other sites

Every string has a length property. You can loop through the array check the length of the string and append something to it based on the length. Once you're done, you can turn the array back into a string using the join() method.

I think I understand what you mean, but what do I replace this "alert" message with

 alert("'"+res[0]+"'" + " length is: "+res[0].length);

to just record them as an array?

Edited by TheShadowGamer

Share this post


Link to post
Share on other sites

var str = "How are you doing today?";
var res = str.split(" ");

 

str is the string of words, res is the variable name of array that stores individual words, when .split() is used to split the string of text. To access each word in array you can loop through each word with 'for' loop, as it loops through each word check length with 'if' condition, for example

 

if(res.length >= 5){

res = res+“-blip”;

}

 

Any word greater or equal to 5, force the current array item of 'res' with index ref of 'i' (which is variable used in 'for loop' which increment the value on each loop), now equal the current word with joining of text of “-blip”

Edited by dsonesuk

Share this post


Link to post
Share on other sites

var str = "How are you doing today?";

var res = str.split(" ");

 

str is the string of words, res is the variable name of array that stores individual words, when .split() is used to split the string of text. To access each word in array you can loop through each word with 'for' loop, as it loops through each word check length with 'if' condition, for example

 

if(res.length >= 5){

res = res+“-blip”;

}

 

Any word greater or equal to 5, force the current array item of 'res' with index ref of 'i' (which is variable used in 'for loop' which increment the value on each loop), now equal the current word with joining of text of “-blip”

I think I've got it but I can't figure out how to fix line 24 so that it will actually do it.

<!DOCTYPE html>
<html>
<head>
<title>Project 1 – Michael Fiorello</title>
    <script>
    do{
        //MAIN MENU
    var input = prompt ("Please enter 1, 2, 3, or exit.");{
    //PROGRAM 1-Enter the string to be converted to robot speak
        if(input == "1")
         do{
             var one = prompt ("Please enter a string.");{
              if (one == "") { console.warn("You need to enter something");}
                 }
                     
             }while (one == "")//keep repeating program 1 until something is entered, aka cannot be blank.
                 
    //PROGRAM 2-Convert the string into robot speak
        else if (input == "2")
            {
              if (one == null) {console.warn ("You need to first enter a String")}
                else {console.log ("String Converted")
                    var res = one.split(" ");{
    //Line 24, well technically now the problem is on Line 25          
                        for(i = 0, i<Arr.length, i++)
                         if(res[i].length >= 5)
                      {
                          Document.write(Arr.[i]+"-blip");
                      }
                      else{
                          Document.write(Arr.[i]+"-clang");
                      }
                    }
                     
                     }
                     }
        //Program 3 Robot Language version of the string will appear in the console
        else if (input == "3")
        {
                alert ("AWESOME!"); 
            }
        else if (input.toLowerCase() == "exit")
            {
            alert ("Thanks for using the ROBOT Language Converter!");
            }
        else 
            {
            alert ("Nope");
            console.warn("You need to enter something");
            }
        }
        }while(input.toLowerCase() != "exit");
</script>
</head>
<body>
<h1></h1>

</body>
</html>
Edited by TheShadowGamer

Share this post


Link to post
Share on other sites

Arr (array) is non existent so no length can never be gathered for the for loop to function, and DON'T USE document.write, anyone who use this, or suggest using this, should have electric prod applied to their gonads or force to listen to one direction.

Share this post


Link to post
Share on other sites

Arr (array) is non existent so no length can never be gathered for the for loop to function, and DON'T USE document.write, anyone who use this, or suggest using this, should have electric prod applied to their gonads or force to listen to one direction.

So what does the string default to if not arr? I tried one, Array, array, res, and i and still not working.

 

So what do I use instead of document write? Thats what we were taught in our tutorial videos

Edited by TheShadowGamer

Share this post


Link to post
Share on other sites

So what does the string default to if not arr? I tried one, Array, array, res, and i and still not working.

 

So what do I use instead of document write? Thats what we were taught in our tutorial videos

Or how do I declare that input as an array once it's split if that's the issue. That's the last thing I can think of.

Edited by TheShadowGamer

Share this post


Link to post
Share on other sites

The idea of getting the length of an array is to identify how many loops to perform variable 'Arr' refers to no array, only res the array of words exists. You should use a element with an id ref for instance

 

<p id="trythis"></p>

 

Then use document.getElementBtId('trythis').innerHTML ="whatever"; to add produced content to that element with same id ref.

Share this post


Link to post
Share on other sites

So I got program 2 to function, but it's not adding blip and clang to the words as well as keeping them as an array. I also can't figure out where to implement "<p id="trythis"></p>" to actually use "document.getElementBtId('trythis').innerHTML ="whatever"" and definitely need it because document.write seems to do f**k all.

 

//Removed code for updated version below.

Edited by TheShadowGamer

Share this post


Link to post
Share on other sites

The reason document.write() is not working in your code is because you've capitalized "Document". Javascript is case sensitive, "a" and "A" are two different variables. Your Javascript console should have shown an error indicating that "Document" is undefined.

 

You're still comparing one to null on this line:

if (one === null) {console.warn ("You need to first enter a String")}

Could you please describe what each symbol means in the following line of your code? If you don't understand what it's doing then you're not writing code properly:

for(i = 0; i<res.one; i++)

Please read about some of the following properties and methods:

Share this post


Link to post
Share on other sites

 

The reason document.write() is not working in your code is because you've capitalized "Document". Javascript is case sensitive, "a" and "A" are two different variables. Your Javascript console should have shown an error indicating that "Document" is undefined.

 

You're still comparing one to null on this line:

if (one === null) {console.warn ("You need to first enter a String")}

Could you please describe what each symbol means in the following line of your code? If you don't understand what it's doing then you're not writing code properly:

for(i = 0; i<res.one; i++)

Please read about some of the following properties and methods:

 

Just found the getElementById page a moment ago, was about to update I was going to attempt to implement.

For

for(i = 0; i<res.one; i++)

i = 0 is stating to start with the first array. i++ is adding to the counter. Trying to remember what i<res.one is I think it makes it so it can go on forever as long as var one exists. Only learned it for the first time yesterday. I know that if I did i<10 it would stop after the tenth interval

 

//Never mind that edit was stupid.

Edited by TheShadowGamer

Share this post


Link to post
Share on other sites
<!DOCTYPE html>
<html>
<head>
<title>Project 1 – Michael Fiorello</title>
    <p id="demo"></p>
    <script>
    do {
        //MAIN MENU
    
        var input = prompt("Please enter 1, 2, 3, or exit.");
        {
    //PROGRAM 1-Enter the string to be converted to robot speak
        if (input === "1")
        do {
            var one = prompt ("Please enter a string.");{
              if (one === "") { console.warn("You need to enter something");}
                 }
                     
             }while (one === "")//keep repeating program 1 until something is entered, aka cannot be blank.
                 
    //PROGRAM 2-Convert the string into robot speak
        else if (input === "2")
            {
              if (one == null) {console.warn ("You need to first enter a String")}
                else {console.log ("String Converted")
                    var res = one.split(" ");
                        for(i = 0; i<res.length; i++)
                         if(res[i].length >= 5)
                     {
                        document.write(res[i]+"-blip ");
                      }
                      else{
                         document.write(res[i]+"-clang ");
                     }
                 }
                     
                     }
        //Program 3 Robot Language version of the string will appear in the console
        else if (input === "3")
        {var output = res.join(" ");
                alert ("AWESOME!"); 
                console.log (output)
            }
        else if (input == null|| input.toLowerCase() == "exit")
            {
            alert ("Thanks for using the ROBOT Language Converter!");
            }
        else 
            {
                alert ("Nope");
            console.warn("You need to enter something");
            }
        }
        }while(input.toLowerCase() != "exit");
</script>
</head>
</html> 

Okay, so I have everything technically working, but I don't want it to "write" out the string to the page, just in the console when called upon in PROGRAM 3. I don't think innerHTML would work for that one, but I'm giving it a shot now.

Edited by TheShadowGamer

Share this post


Link to post
Share on other sites

If you just want to write to the console then console.log() is all you need. innerHTML is just for putting content on the page.

 

You should use curly braces { } on all of your code blocks and properly indent them, otherwise your code is hard to read and is more likely to have mistakes since it's not clear where the blocks of code end.

 

For proper indentation:

  • Every opening brace must be followed by a line break
  • Closing braces must be the first printable character on their line
  • Closing braces must be at the same level of indentation as the opening brace that it belongs to
  • Content between braces is indented one level further

 

You don't need to use the === operator for comparison unless you really care that the type of data is a string.

Share this post


Link to post
Share on other sites

working on fixing the indentation, but what I'm trying to do is have it converted in PROGRAM 2: adding -blip to words 5 characters or more and adding clang to less than character words, but then I need to actually call upon the converted versions in PROGRAM 3, so what do I use to permanently change the strings within the array* for when I call on them?

 

Better looking now? Maybe?

<!DOCTYPE html>
<html>
<head>
<title>Project 1 – Michael Fiorello</title>
    <p id="demo"></p>
    <script>
    do {
        //MAIN MENU
    
        var input = prompt("Please enter 1, 2, 3, or exit.");
        {
    //PROGRAM 1-Enter the string to be converted to robot speak
        if (input === "1")
            do {
                var one = prompt ("Please enter a string.");
                    {
                        if (one === "") 
                            { 
                                console.warn("You need to enter something");
                            }
                    }
                     
            } while (one === "")//keep repeating program 1 until something is entered, aka cannot be blank.
                 
    //PROGRAM 2-Convert the string into robot speak
        else if (input === "2")
            {
              if (one == null) {
                                    console.warn ("You need to first enter a String");
                               }
                else 
                    {
                        console.log ("String Converted")
                            var res = one.split(" ");
                                for(i = 0; i<res.length; i++)
                                 if(res[i].length >= 5)
                                            {
                                        document.write(res[i]+"-blip ");
                                            }
                                    else
                                            {
                                        document.write(res[i]+"-clang ");
                                            }
                      }
                     
             }
        //Program 3 Robot Language version of the string will appear in the console
        else if (input === "3")
            {
                var output = res.join(" ");
                    alert ("AWESOME!"); 
                    console.log (output);
                        }
                            else if (input == null|| input.toLowerCase() == "exit")
                        {
                            alert ("Thanks for using the ROBOT Language Converter!");
                        }
        else 
            {
                alert ("Nope");
                console.warn("You need to enter something");
            }
        }
        } while(input.toLowerCase() != "exit");
</script>
</head>
</html>
Edited by TheShadowGamer

Share this post


Link to post
Share on other sites

I got it to work, I feel like an idiot because it was as simple as changing

document.write(res[i]+"-blip ");

to

res[i] = (res[i]+"-blip ");

It took me so long to figure out something so simple. For f**ks sake brain, work with me here!

 

Okay so I have one last problem. How do I get cancel to work at the main menu without causing an error? I already tried doing || null at the beginning or end. But I need the "Thanks using the..." message and I can't get it to pop of if I don't include "null" in that statement.

<!DOCTYPE html>
<html>
<head>
<title>Project 1 – Michael Fiorello</title>
    <p id="demo"></p>
    <script>
    do {
        //MAIN MENU
    
        var input = prompt("Please enter 1, 2, 3, or exit.");
        {
    //PROGRAM 1-Enter the string to be converted to robot speak
        if (input === "1")
            do {
                var one = prompt ("Please enter a string.");
                    {
                        if (one === "") 
                            { 
                                console.warn("You need to enter something");
                            }
                    }
                     
            } while (one === "")//keep repeating program 1 until something is entered, aka cannot be blank.
                 
    //PROGRAM 2-Convert the string into robot speak
        else if (input === "2")
            {
              if (one == null) {
                                    console.warn ("You need to first enter a String");
                               }
                else 
                    {
                        console.log ("String Converted")
                            var res = one.split(" ");
                                for(i = 0; i<res.length; i++)
                                 if(res[i].length >= 5)
                                            {
                                                res[i] = (res[i]+"-blip ");
                                            }
                                    else
                                            {
                                                res[i] = (res[i]+"-clang ");
                                            }
                      }
                     
             }
        //Program 3 Robot Language version of the string will appear in the console
        else if (input === "3"){
            if (res == null)
                { 
                    console.warn("You need to first convert your String");
                
                }
            else{
                    var output = res.join(" ");
                    alert ("AWESOME!"); 
                    console.log (output);{
                                            var one = null;
                                            var res = null;
                                         }
                }
                                }
                            
            else if (input == null|| input.toLowerCase() == "exit")
                        {
                            alert ("Thanks for using the ROBOT Language Converter!");
                        } 
        else 
            {
                alert ("Nope");
                console.warn("You need to enter something");
            }
        }
        } while(input.toLowerCase() != "exit");
</script>
</head>
</html>
Edited by TheShadowGamer

Share this post


Link to post
Share on other sites

I also tried creating a separate else if statement if the variable is null at the main menu and I still get an error from it.

Edited by TheShadowGamer

Share this post


Link to post
Share on other sites

Your problem is that you're specifically telling it to show an error when the input is not "1", "2" or "3". That also happens when input is "exit"

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now