JavaScript JavaScript Basics Creating Reusable Code with Functions Random Number Challenge Solution

Katiuska A
Katiuska A
8,260 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

Piotr Manczak
Piotr Manczak
11,829 Points

I share your pain. This is not fair.

Emmanuel C
Emmanuel C
7,578 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.

Katiuska A
Katiuska A
8,260 Points

How does that ( upper) thing works, anyway?

Piotr Manczak
Piotr Manczak
11,829 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
7,578 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.

Piotr Manczak
Piotr Manczak
11,829 Points

Emmanuel C is so right. Well done mate!

Katiuska A
Katiuska A
8,260 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.