23
loading...
This website collects cookies to deliver better user experience
functions
for the tutorial and hit Save new project.**num1**
and **num2**.
This means that the function should return the largest number that divides both **num1**
and **num2**.
(6, 10); // => 2
(4, 8); // => 4
(45, 30); // => 15
functions
folder on the left of the browser and selecting New endpoint file
.functions/greatestCommonFactor.js
as I have:line 1--6
defines the parameter
types that the function takes and the return
type. In this sample, the function is expecting a name
parameter of type string
and it will return a body
of type object.
line 7
is exporting our function with amodule.exports
method. Let's check it out. Change the name in line 7
to your name as I have in the screenshot.run
button to test out this sample function. You will see your code executed in the Endpoint viewer window with "Hello <yourName>
, welcome to Autocode!" message.name
parameter into your function via the URL and press enter to reload the page as I have: ?name=Student
**num1**
and **num2**.
This means that the function should return the largest number that divides both **num1**
and **num2**.
(6, 10); // => 2
(4, 8); // => 4
(45, 30); // => 15
//First we need to define the functions expected parameters and return types in a comment
//Then we use module.exports to export our function and pass in two numbers (num1, num2) as parameters
//We will set a for loop with the iterator (i) set to num1
//As long as i is greater or equal to 1 continue the for loop
//Everytime the for loop continues decrement i by 1
//As we decrement we check to see if num1 modulo i is equal to zero and if num2 modulo i is equal to zero
//If both conditions are met then we will return i
//If the conditions aren't met then we have our for loop continue decrementing until both are both are met
/**
* My greatestCommonFactor function
* @param {number} num1 First number
* @param {number} num2 Second number
* @returns {number} i
*/
module.exports = async(num1, num2) => {
for (let i = num1; i >= 1; i--) {
if (num1 % i === 0 && num2 % i === 0) {
return i;
}
}
}
num1
and num2
as your keys and give them any two numbers you'd like. I've chosen 1550 and 1000
.1550
and 1000
is in fact 50
! Great Job. ⭐️number
for num1
and num2
and a return type of number
for i
module.exports
and pass in our two parameters (num1, num2).
for loop
and declare num1
to be our iterator i
, as long as i
is greater or equal to 1
we run our for loop and decrement by 1.
if
statement checks to see if num1
modulo i
is equal to zero, and if num2
modulo i
equal to zero.num1 % i === 0
or num2 % i === 0
returns false
our loop continues and the following return
statement is ignored. When our if
condition returns true,
that means that both conditions are met, and then we return
the iterator i
. The iterator i
is our greatest common factor between num1
and num2
.(Jacob); // => 'JACO-JACO'
(pikachu); // => 'PIKA-PIKA'
(janeth); // => 'JANE-JANE'
//First define the functions expected parameter and return type in a comment above the function
//Use module.exports to export the function and pass in a a name as a string
//Create a variable that defines all vowels inside an array
//Create a variable to keep count of vowels and set it equal to 0
//Use for loop to count through every letter in the string (name)
//Create an intermediate variable to save every character in the string
//Inside the for loop condition use indexOf() method to check and count if character is in the index of vowels
//Use toUpperCase() method to convert string to uppercase
//return a string with characters from index 0 to i (including i)
functions
folder on the left of the browser and select New endpoint file
.functions/nickname.js
and translate your pseudocode to JavaScript Syntax./**
* My nickname function
* @param {string} name
* @returns {string} str
*/
module.exports = async(name) => {
let vowels = 'AEIOUaeiou'.split('');
let vowelCount = 0;
for (var i = 0; i < name.length; i += 1) {
let char = name[i];
if (vowels.indexOf(char) > -1) {
vowelCount += 1;
if (vowelCount === 2) {
break;
}
}
}
let str = name.slice(0, i + 1).toUpperCase();
return str + '-' + str;
}
name
parameter and select the green Run
button on Autocode to view the results on the console.string
for name
and a return type of string
module.exports
and pass in our (name)
parameterfor loop
to iterate through every character in name
vowels.indexOf(char)
and returns the char location in the vowels array. If the char isn't in the index of vowels, then a -1 is returned, and the loop continues to the next character. If its location is greater than -1, then the logic continues.-1
we increment our vowelCount
by one.vowelCount
is 2, if false
is returned, we skip the if
statement and move on to the next letter, if true
is returned that means that we have now counted two vowels, and we break out of the for loop.
slice()
method to grab the characters starting at index 0
and ending at i
, we add 1
to i
because the slice
method doesn't include the end value. We also use toUpperCase()
method to convert all the letters in our string to uppercase.return
the string plus a dash plus another string to have our result resemble the examples in the questionconst arr1 = ['a', 'b', 'b', 'c', 'd', 'c', 'c', 'd']\
oddOnesOut(arr1); // => [ 'b', 'd' ]
const arr2 = ['fish', 'fish', 'dog', 'cat', 'dog', 'dog']\
oddOnesOut(arr2); // => [ 'fish' ]
//Create a helper function to count our elements in an array
//The helper function has one parameter that takes in an array of strings
//Use a for loop in the helper function to count our elements in the array
//Create and export a second function with a for loop to count the even elements
//Push the even elements into a new array
//Return that new array
functions
folder on the left of the browser and select New endpoint file
.functions/oddOnesOut.js.
Delete the sample function and translate your pseudocode to JavaScript syntax as I have:/**
* My oddOnesOut function
* @param {array} array
* @returns {array} str
*/
function elementCount(array) {
let countObj = {};
for (let i = 0; i < array.length; i += 1) {
let key = array[i];
if (countObj[key] === undefined) {
countObj[key] = 1;
} else {
countObj[key] += 1;
}
}
return countObj;
}
module.exports = async(array) => {
let count = elementCount(array);
const result = [];
for (let key in count) {
let value = count[key]
if (value % 2 === 0) {
result.push(key);
}
}
return result;
}
array
parameter inside the payload window:{"array": ['fish', 'fish', 'dog', 'cat', 'dog', 'dog']}
Run
button on Autocode to view the results on the console:["fish"]
array
for array
and a return type of array
for str
elementCount
that takes in an array
of stringscountObj
variable and initialize it to an empty object. This is where we store the different counts of elements in an array.for loop
. We declare 0
to be our iterator (i
), as long as i
is smaller than the length of our array, we run our for loop and increment by 1
.key
as an intermediate variable that will give access to the element as we go through the for loop
.countObject
at index key
is undefined. This condition will return true
if the countobject
does not contain the Key
(element) that we're passing in.true
we set countObj[Key]
equal to 1
If the condition is false
which means that our key
is already in the object, then we ignore this statement and move on to the next.key
is already stored in the countObj
then we increment our key count by 1.
coutObj
count
and set it to the object returned from the previous helper function elementCount(array)
result
and initialize it to an empty array where we will be pushing the elements that show up an even number of timescount
object and check if the key has a value of modulo 0.value
to count[key]
key
has a value
modulo 0
key
to our result
variableresult