Let’s write some functions! Write these in the script
tag of a skeleton HTML file. If you’ve forgotten how to set it up, review our instructions on how to run JavaScript code.
For now, just write each function and test the output with console.log
.
Write a function called add7
that takes one number and returns that number + 7.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Some Function Exercises from The Odin Project</title>
</head>
<body>
<script>
function add7(number){
return number + 7;
}
</script>
</body>
</html>
Write a function called multiply
that takes 2 numbers and returns their product.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Some Function Exercises from The Odin Project</title>
</head>
<body>
<script>
function multiply(number1, number2){
return number1 * number2;
}
</script>
</body>
</html>
Write a function called capitalize
that takes a string and returns that string with only the first letter capitalized. Make sure that it can take strings that are lowercase, UPPERCASE or BoTh.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Some Function Exercises from The Odin Project</title>
</head>
<body>
<script>
function capitalize(string){
return string[0].toUpperCase();
}
</script>
</body>
</html>
Write a function called lastLetter
that takes a string and returns the very last letter of that string:
lastLetter("abcd")
should return "d"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Some Function Exercises from The Odin Project</title>
</head>
<body>
<script>
function add7(string){
return string[string.length()-1];
}
</script>
</body>
</html>
This is how you write a variable inside a string:
console.log(`It's a tie! You both chose ${humanChoice}.`);
This is how you write multiple conditions:
else if (
(humanChoice === "rock" && computerChoice === "scissors") ||
(humanChoice === "paper" && computerChoice === "rock") ||
(humanChoice === "scissors" && computerChoice === "paper")
)
Leave a Reply