开发者

Removing a string return after a function has been processed

I'm a beginner and a student and I'm hoping someone can help me out. I have an assignment where I need the program to be broken up into 3 functions. The first takes a sentence from the user, the second converts the sentence into a new "pig language" depending on the length of each word, and the third displays the results in the console. I have the heart of this program done, but I have a problem with clearing out the return string. Specifically, once the user has gone through all 3 steps, I don't want them to be able to enter into the 3rd part of the program and see the results again. I want them to have to go back to the beginning. Sorry for drawing this out so much, but I'm just not sure of how else to explain it.

Here's my code:

function prog1(){
var userLang = prompt("Type in your sentence");
//If the user enters an empty string
    if(userLang == ""){
        console.log("You must enter a sentence");
    }
//If the user presses cancel
    else if(userLang == null){
        wantToQuit = true;
    }
//If the user enters in a good string
    else {
        console.log("Thank you, now go to program 2");
        been2prog1 = true;
        return userLang;
    }
} 

function prog2(){
//sets newLang = userLang and splits the string
var newLang = prog1Lang.split(" ");

//enters loop to find length of each split word
var x = 0;
for( x = 0; x < newLang.length; x++ ){

//if it's 5 or less words, add -oink    
    if ((newLang[x].length) <= 5){
        new开发者_StackOverflow中文版Lang[x] += "-oink";
    }
//if it's more than 5 words, add -a
    else {
        newLang[x] += "-a";

    }       

}
**newLang.join(" ");**  

//put the string back together  

console.log("String converted");
been2prog2 = true;
return newLang;
}

 function prog3(){
var endLang = prog2Lang;
console.log(endLang);
**delete prog2Lang;**

}

I was thinking "delete" might work, as seen above, but I didn't do anything all all. Then I was thinking a Boolean, but I am not sure how to go about doing so. Any help would be much appreciated.

One last thing, I am also stuck on how to join my string back together. Currently it logs it in the console as being a part of the array and separates each word with quotes and a comma. I've looked up the .join(); and I thought it would do the trick, but it doesn't seem to work either. I put it inside of the if else statements in function 2 but, it just freaks out when I do that, so pointers on this issue would also be much appreciated.

Thank you!


Try assigning the newLang.join to itself..

newLang = newLang.join(" ");


I wasn't sure what the other bit was that you were having trouble with was, I was a bit confused.

if all you are trying to do is clear out a string variable then..

prog2Lang = null;

or 

prog2Lang = ""; 

null is a null object and "" is an empty string.

Is that what you were after?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜