Johan Kohlin
Lecturer at School of engineering, Jönköping University.
You will get an email, not from me,
asking to do a course evaluation for
Web and user interface design.
PLEASE, do it!
Don't edit this code!!
Write your code here
optional
optional
Run your code and compare the output to the expedted values
optional
Ingredients:
Create a function that takes a number as input (portions) and calculates the recipe for spaghetti. The return value should be an object.
{
spaghetti: "180 g",
salt: "1 tsp",
water: "15 dl"
}
Create the function wrapper
function pasta(portions) {
}
make room for incoming value
create variables and calculate each ingredient
function pasta(portions) {
var spaghetti = portions * 90 // g
var water = portions * 7.5 // dl
var salt = portions * 0.5 // tsp
}
create the object
function pasta(portions) {
var spaghetti = portions * 90; // g
var water = portions * 7.5; // dl
var salt = portions * 0.5; // tsp
var output = {
"spaghetti": spaghetti,
"water": water,
"salt": salt
};
}
return the object
function pasta(portions) {
var spaghetti = portions * 90; // g
var water = portions * 7.5; // dl
var salt = portions * 0.5; // tsp
var output = {
"spaghetti": spaghetti,
"water": water,
"salt": salt
};
return output;
}
In pairs
star: make it case insensitive
Create a function that takes an array of strings as input and returns the length of the longest string.
Create the function wrapper
function longestWordLength(words) {
}
make room for incoming value
figure out what we need to do
figure out what we need to do
code your ideas
function longestWordLength(words) {
var longestWord = ""
}
code your ideas
function longestWordLength(words) {
var longestWord = "";
for (var i = 0; i < words.length; i++) {
}
}
code your ideas
function longestWordLength(words) {
var longestWord = "";
for (var i = 0; i < words.length; i++) {
if (words[i].length > longestWord-length) {
longestWord = words[i];
}
}
}
code your ideas
function longestWordLength(words) {
var longestWord = "";
for (var i = 0; i < words.length; i++) {
if (words[i].length > longestWord-length) {
longestWord = words[i];
}
}
return longestWord.length
}
in pairs
tip: check out the Modulo operator, %
❤️
By Johan Kohlin
Writing functions with parameters that return values