Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAndre Romano
8,809 Pointsfunction 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.
3 Answers
Andre Romano
8,809 PointsThanks 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
2,211 Pointsvar 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
Treehouse TeacherSo 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
8,809 PointsYes that's exactly it Dave.
Dave McFarland
Treehouse TeacherI 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
8,809 PointsYou 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.
Dave McFarland
Treehouse TeacherDave McFarland
Treehouse TeacherHi Andre Romano
I'm not sure what you are asking.
What do you mean by "instead of giving me the variable I wanted" -- what variable do you want?