Functions and Arrays Exercise

Hey everyone, I’ve been making my way through some practice exercises and I’m stuck on the last exercise. The exercises below ask to not use console.log when calling the function, and I have no clue on how to call the function so that it produces an outcome:

  1. Write a function that returns the number of digits in a number. For example, there are 3 digits in the number 104.
  2. Write a function that gives how many of a certain letter there are in a word. For example, there are 3 occurrences of the letter “a” in the word “cake spatula”.
  3. Write a function that takes one string and one number, and that returns True or False based on whether the length of the string is bigger than the number.

Thanks in advance for any help!

2 Likes

Hi @MoeezMalik, hope you are great.

Maybe you should watch again the function video from the course, im sure ivan explain how to initialize functions.
Here is a basic example for it:

//defining the function
function someName(arguments){
 //do something with the argument
}
//invoke call the function
someName(variable)
Ex:
function add(a,b){
return a+b
}
//then
console.log(add(2,4)) //should show 6

Carlos Z

1 Like

Thanks for your help Carlos, managed to figure it out as soon as I woke up today, just had to sleep on it :+1:

1 Like

in question 33. Given we are learning arrays. This works too right rather than writing a function?

var sheep = [‘Sheep produce a lot of wool’, ‘Sheep are soft animals.’, ‘Sheep can lift 12 times their own weight.’, ‘Sheep smell a bit like goats.’, ‘The sheep are your friends.’]

console.log(sheep.join(’\r\n’));

1 Like