Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Andre Romano
Andre Romano
8,809 Points

function parameters question(basics)

This is my first post here as i'm kinda new around here so first of all,

Hi everyone!,

On to the question, i was following a JavaScript Foundations lesson and tried to do something clever, and i failed :( and i can't seem to figure out why it doesn't work.

Here's the code:

var part1 = "Hello ",
      part2 = "World!",
      whole = part1 + part2;

// making a function with that like a bauss

var stringChecker = function(targetString, targetCharacters) {
  if(targetString.indexOf(targetCharacters) !== -1) {
    console.log(targetCharacters + " exists in " + targetString);
  } else {
    console.log(targetCharacters + " does not exist is " + targetString);
  }
  console.log("DEBUG: " + targetString);
  console.log("DEBUG: " + targetString.indexOf("W"));
}

// stringChecker(whole, prompt("Target Characters"));   THIS WORKS
stringChecker(prompt("Target String"), prompt("Target Characters"));

When i console.log targetString parameter is gives me the the value i inputted as a string instead of giving me the variable i wanted.

Is there anyway for me to call a variable through a prompt, or any other user input?

Thanks.

Dave McFarland
Dave McFarland
Treehouse Teacher

Hi Andre Romano

I'm not sure what you are asking.

When i console.log targetString parameter is gives me the the value i inputted as a string instead of giving me the variable i wanted.

What do you mean by "instead of giving me the variable I wanted" -- what variable do you want?

3 Answers

Andre Romano
Andre Romano
8,809 Points

Thanks for the answers

@Dave McFarland

For instance:

stringChecker(whole, prompt("Target Characters"));

Works at selecting the String i want to work on, because im parsing the name of the variable holding the string as a parameter.

But when i do the following it doesn't work even if i input the name of the variable i want to target on the prompt.

stringChecker(prompt("Target String"), prompt("Target Characters"));

@Michael Soule

var stringChecker = function(targetString, targetCharacters) {
  if(targetString.indexOf(targetCharacters) !== -1) {
    // Returns the integer value for the index of the targetCharacters within the targetString
    return(targetString.indexOf(targetCharacters));
  } else {
    console.log(targetCharacters + " does not exist is " + targetString);
  }
}

stringChecker(prompt("Target String"), prompt("Target Characters"));

// input stringChecker("abcd","bc")
// output 1
console.log(stringChecker);

The value assigned to "stringChecker" is now the integer value of the index.

Dave McFarland
Dave McFarland
Treehouse Teacher

So you want someone to type in the name of a variable that exists in your program? For example, type "whole" in the first prompt dialog, then have JavaScript search the value inside the whole variable instead of search the string 'whole'. Is that right?

Andre Romano
Andre Romano
8,809 Points

Yes that's exactly it Dave.

Dave McFarland
Dave McFarland
Treehouse Teacher

I guess I don't understand WHY you'd want to do that. Anyone who loaded the page would have no idea what the variables were in your code, so they'd have no idea what to type into that first prompt dialog. Is this just an exercise or do you have a real world case where you want to use the feature you describe?

Andre Romano
Andre Romano
8,809 Points

You are absolutely right, the more i think about it the more I realise it has no real world purpose.

I was trying to make that function as an exercise to consolidate the knowledge I got during the lessons, and when I got stuck with that issue I got curious if it was something simple I was doing wrong or if it would require a trickier solution.