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 trial

JavaScript

Piotr Manczak
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Piotr Manczak
Front End Web Development Techdegree Graduate 29,277 Points

I have a variable: let chartLength = 'Hourly'; Does anyone know how to change string 'Hourly' in not string Hourly?

I have a variable: let chartLength = 'Hourly'; Does anyone know how to change string 'Hourly' in not string Hourly? So I can use it as a variable in JS?

2 Answers

Steven Parker
Steven Parker
231,172 Points

the name "chartLength" is a variable in this code, one that holds a string.

I'm not sure what you mean by "in not string Hourly". Do you want the variable to hold something other than a string? If so, what?

It also might help a lot to see the actual code.

Steven Parker
Steven Parker
231,172 Points

Now that I see what you were doing, I might have suggested storing the data in a different format. Instead of named variables, you could have created an object where values were associated with keywords. Then the function itself would be both simple and flexible:

function okres(x) {
    return myObject[x];
}

For future questions, be sure to post the code along with the question.

Piotr Manczak
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Piotr Manczak
Front End Web Development Techdegree Graduate 29,277 Points

Thank you for taking your time to answer this question. I have came up with the solution. What I needed was transforming string into non string for example: 'Hello' => Hello So from the string that share this same name with variable into that variable. I have achieved that using function:

function okres(x) {

if ( x == 'Daily' ) {

return Daily;

} else if ( x == 'Hourly' ) {

return Hourly;

} else if ( x == 'Weekly' ) {

return Weekly;

} else if ( x == 'Monthly' ) {

return Monthly;

}

}

Using this function to transform string 'Daily' into Daily I just: okres('Daily'); result: Daily; This function is not flexible because it covers only four cases, but it was enough for my needs(FEWD-project7).