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 JavaScript Basics (Retired) Creating Reusable Code with Functions Random Number Challenge Solution

Katiuska Alicea de Leon
Katiuska Alicea de Leon
10,341 Points

Can anyone create a challenge that tests the knowledge for this section but without including math?

Is it necessary to add math? Can't these concepts be illustrated using some other subject matter? Can't logic be applied to something else? Can we go back to the assistant at the coffee shop? Or the grocery store, maybe? There are more items there!

This random numbers thing is pretty frustrating. I can be frustrated without paying a monthly subscription.

7 Answers

Emmanuel C
Emmanuel C
10,636 Points

For a simple challenge, try creating a function where the user can pass in their name and it would return " hello, theirName"

input = greetPerson("Emmanuel")
output=  "Hello, Emmanuel"

For something more difficult how about making a function that tells you how many times a certain letter shows up in word or sentence(string).

This would require a function taking 2 arguments, the particular letter to check for and the sentence to check if the letter is in it. You can use a for loop, a counter to keep track of how many times that letter occured, and a built in JS function called charAt(number), which is called from a string, takes a number and returns the character from that string at the specified number, or index.

input = getCharOccurence("e", "Test Sentence")
output = 4

Bonus points if you can make it case insensitive.

If you need help, feel free to shout out. Good luck.

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

The upper thing looks like a function but it has not been explained. I think he meant:

function greetPerson(name) { var html = "Hello" + person + "!"; document.write(html); }

so when you call a function greetPerson() with 'name' as a parameter you get: Hello 'name'!

Emmanuel C
Emmanuel C
10,636 Points

For the first one, greetPerson would take 1 argument, the name, and then concatenate it to the string "Hello" then return it. While writing it to the document may also work, return is a more common use for a function, especially in other languages that are not as closely connected to a webpage document. So...

function greetPerson(name){
    let greeting = "Hello, " + name;
    return greeting; 
}

For the second challenge. You can have a function that takes the letter you want to test for and the sentence you want to search in. Then using a loop you can loop through each letter/char in the sentence/string using the string function charAt(), then compare if its logically equal to the letter passed in. Incrementing a counter if it is. Like so...

function getCharOccurence(letter, sentence){
    let counter = 0;

    for(let i = 0; i < sentence.length; i++){
        if(letter.toLowerCase() === sentence.charAt(i).toLowerCase()){
            counter++;
        }
    }   
    return counter;
}
console.log(getCharOccurence("e", "Test SEntence"));

Adding the toLowerCase(), makes the letter/char lowercase when it gets compared, making it case insensitive. So that console.log would return 4.

Katiuska Alicea de Leon
Katiuska Alicea de Leon
10,341 Points

Thanks, Emmanuel. I ended up doing the math ones a few days ago. I guess I posted the question out of frustration, but it's good to know that there are the resources in this community willing to offer alternatives to those of us who might not have been to a school district with good math programs.