Define a function named firstLetterName. It should take a string name as a parameter and alert the message "The name <NAME> starts with the letter <FIRSTLETTEROFNAME>
HINT: to get a single character of a string, you can use bracket syntax like you used to access array elements
Define a function divisibleByTwo. It should take a single parameter number and determine whether the number input is an even or odd number. If it's even, the function should return true. If it's odd, the function should return false.
HINT: You'll need to use the % operator.
Define a function named largestNum. It should take an array arr as a parameter and loop through that array. The function should return the largest number in the array.
After defining all the functions, call each function at the bottom of the JavaScript file with the appropriate arguments.
#learning functions.. please help!
38 messages · Page 1 of 1 (latest)
let firstName = prompt("What is your first name?")
let firletter = firstname.charAt(0);
function firstLetterName(firstName) {
alert("The name " + firstName + "begins with the letter " + firletter);
}
function divisibleByTwo() {
}
function largestNum() {
}
/* calling functions below */
firstLetterName();
divisibleByTwo();
largestNum();```
this is what i have so far but it doesn't run?
function firstLetterName(firstName)
Need to pass the required argument to the function you're trying to call
firstLetterName(requiredArgument);
Is how you'd need to call it
let firstName = prompt("What is your first name?")
You'd need to pass this into the function when you're calling it
function functionName(argument) {
/* do something */
};
/* calling the function */
functionName(argumentPassedIntoFunction);
let firletter = firstname.charAt(0);
function firstLetterName(firstName) {
let firstName = prompt("What is your first name?")
alert("The name " + firstName + "begins with the letter " + firletter);
}
function divisibleByTwo() {
}
function largestNum() {
}
/* calling functions below */
firstLetterName(firstName);
divisibleByTwo();
largestNum();```
like this?
Does it run now?
no
Oh, you removed the firstName variable
Why'd you remove that?
You need that
Or rather, why did you move it into the function?
That's the variable you're trying to pass into the function
Now you have no variables you're trying to pass anywhere
let firletter = firstname.charAt(0);
function firstLetterName(firstName) {
let firstName = prompt("What is your first name?")
/* This is the variable you're trying to pass into the function */
alert("The name " + firstName + "begins with the letter " + firletter);
}
function divisibleByTwo() {
}
function largestNum() {
}
/* calling functions below */
firstLetterName(firstName); /* You can't pass variables from inside the functions */
divisibleByTwo();
largestNum();
ah so keep it as before?
let firstName = prompt("What is your first name?")
let firletter = firstname.charAt(0);
function firstLetterName(firstName) {
alert("The name " + firstName + "begins with the letter " + firletter);
}
function divisibleByTwo() {
}
function largestNum() {
}
/* calling functions below */
firstLetterName();
divisibleByTwo();
largestNum();```
sorry i dont get what youre saying
do i put it like
firstLetterName(firstName);```
Yup
Might make it a bit more clear if you renamed the required arguments for the functions, instead of having those as the exact same names as the variables you're using
okay ill change them
let firstName = prompt("What is your first name?")
let letter = firstname.charAt(0);
function findFirstLetter(firstName) {
alert("The name " + firstName + "begins with the letter " + letter);
}
function divisibleByTwo() {
}
function largestNum() {
}
/* calling functions below */
findFirstLetter(firstName);
divisibleByTwo();
largestNum();``` is this better?
im not exactly sure how to get it to run though, and find the first letter of the inputted name?
does this look better?
let firstName = prompt("What is your first name?")
let letter = firstname.charAt(0);
let number = prompt("Enter a number:")
function findFirstLetter(firstName) {
alert("The name " + firstName + "begins with the letter " + letter);
}
function divisibleByTwo() {
if(number % 2==0){
alert("The number is even");
}
else {
alert("The number is odd");
}
}
function largestNum() {
}
/* calling functions below */
findFirstLetter(firstName);
divisibleByTwo();
largestNum();```
added a few things
I meant more like this:
let firstName = prompt() /* This is a variable, not an argument */
/* fName here is an argument to the function */
function findFirstLetter(fName) {
alert("Name: " + fName)
}
/* Here you'd pass the firstName variable into the function */
findFirstLetter(firstName)
/* Variables */
let number1 = 12;
let number2 = 22;
/* Function with arguments */
function addNumbers(argumentNumber1, argumentNumber2) {
return argumentNumber1 + argumentNumber2
}
/* Passing the variables into the function */
addNumbers(number1, number2)
That is why I said to maybe rename the arguments for the functions, not the variables
Either remove the argument from the function, and the call, and use the globals, or pass in both of the variables into the function
This might be a good read for you
What is confusing you are the differences between function arguments, and variables due to how you've named things in your code