• Announcements

    • boen_robot

      Guidelines and Netiquette   03/28/2017

      Posting Problems:   Having problems posting your topic? Read through this: To join, you agree to our terms and conditions and fill out and submit a registration form. An activation email will be sent to your email adress, so you'll need to verify your account. After that the account has to be validated by one of the moderators. This will mean that it can take up to a day to be activated. A couple of things to remember to ensure approval: Don't use an email address in one of those $2 four character .com domains eg. xyds.com. These will be deleted and the domain added to the banned list. Don't use an email address that is within a domain with a bad reputation for spam. A Google search is run on every email address and email domain. Don't sign up with an email address that doesn't exist, doesn't work or requires the sender to answer a quiz before their email can get to you. Put your country and or state and city in the signup form. Blank forms will go to the botton of the "to do" list. And make sure that your email address and your country match, saying you're from Alabama and using a .ru email address is not going to get you activated. After a membership is activated the first few posts will be monitored. Posting spam or unapproved topics described in the agreement results in an immediate ban. The email provider and the IP addresses associated with the account will be banned and all posts will be deleted. These strict measures have been deemed necessary to hinder spam. Sorry for any inconvenience this causes, but it's not liable to change. If, after reading this, you still can't post and don't understand why, contact one of the Moderators listed here.   Topic Guidelines   Including the following information can expedite an accurate response from board members: Must be a Specific Problem or Question related to web design and development Include Code in Question (wrap with   for small blocks of code and for longer blocks   ) Include Code Author Include Extra Notes/Modifications/Attempts Include web link to page/file when possible Content Guidelines   You may not post, upload, link to, or email any Content that contains, promotes, gives instruction about, or provides prohibited Content. Prohibited Content includes any Content that breaks any local, state, county, national or international law. Prohibited Content also includes: No direct or indirect advertising or websites, forums, products, services No hijacking of posts (do not post your question in someone elses) Content that infringes upon any rights [ex. MP3s and ROMs] (including, but not limited to, copyrights and trademarks) Abusive, threatening, defamatory, racist, or obscene Viruses or any other harmful computer software False Information or libel Spam, chain letters, or Pyramid schemes Gambling or Illicit drugs Terrorism Hacking or cheating for internet/online games Warez, Roms, CD-Keys, Cracks, Passwords, or Serial Numbers Pornography, nudity, or sexual material of any kind Excessive profanity Invasive of privacy or impersonation of any person/entity Hacking materials or information Posting Tips   There are more BBcodes than there are buttons for on the reply menu. To get the full list, click "BB Code Help" underneath the clickable smilie face menu. Use   for small snippets of code Use   for lengthy snippets of code Use   if your snippet is HTML (optional) Use   if your snippet is SQL (optional) Rules of Conduct   Be nice. There's no need for calling someone stupid if they ask an 'easy' question. Keep your avatars and signatures absolutely child friendly. We have a younger audience on this forum. Keep your language appropriate for the same reason above. Do not PM moderators for help on the forum. Post on the topic, or create a new one.   Spam:   Recently, as you have all without doubt noticed, we have had lots of spam and advertisement on the forum. Therefore, we'd like to alert you as to what to do when you have found any of the aforementioned annoying messages: it. Immediately. Give a clear reason, please, if the advertising is not evident. DO NOT POST! Report, let the post stay as is, and we will get to it, meanwhile if you continue to post as normal in the other threads, it won't be on the top so long. Refrain from PMing the member. This won't help at all, as they are most likely spambots anyway. Thank you.       Images in signatures:   After thinking of users on dial-up, we have decided to enforce the following rules regarding signatures. Please pay heed to them. Respecting these rules is respecting the members on this forum with dial-up. Signature rules: No animated images AT ALL. No matter the amount of animation. Maximum image widthxheight: 300x150 Maximum image (file) size: 15kb Use calm colors. Do not use highly contrasting images in your signature, as this can get really annoying when seeing several posts from one member in the same thread. The same prohibited content goes for images as for posts. Lastly, use common sense. No lengthy signatures please. Save us some scrolling. Thank you.       Links in signatures:   Please understand that w3schools.com only exists because of voluntary work and is barely supported by the advertising littered throughout the tutorials and the forum. So, please, stop advertising other sites. DO not post links that drive traffic away from the w3schools domain - especially to a site that offers similar if not identical information. Please help support the site by keeping individuals on it. Thank you. Here are some guidelines as to what you can put in your signature: w3schools links --> allowed w3.org links --> allowed browser links --> allowed html editor links --> allowed personal sites --> allowed tutorial sites competing with w3schools --> NOT allowed sites completely irrelevant to webprogramming and this forum --> NOT allowed   Thanks for understanding, and for taking the time to read this. ~W3Schools Modstaff~
TheShadowGamer

Is there any way to do this?

21 posts in this topic

Posted (edited)

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

Posted (edited)

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

Posted (edited)

 

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

Posted (edited)

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

Posted (edited)

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

Posted (edited)

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

Posted (edited)

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

Posted (edited)

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

Posted (edited)

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

Posted (edited)

 

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

Posted (edited)

<!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

Posted (edited)

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

Posted (edited)

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

Posted (edited)

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