Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ================================================================================
- // You will be writing a series of functions beneath each block of comments below.
- // Tackle each function one at a time. You may move on to a new exercise and return
- // later. Run the code and the console to the right will tell you if your function
- // is successfully passing the tests.
- // ================================================================================
- /*
- Define a function named `kmToMiles` that receives one parameter:
- 1) km - type: number
- The function should return a number in miles, rounded down to nearest integer.
- HINT: There are 1.6km in 1 mile.
- HINT: What method on Javascript's Math object can round decimals down to nearest whole number?
- */
- function kmToMiles(km){
- var miles = km/1.6;
- return Math.floor(miles);
- }
- /*
- Define a function named `countLetters` that receives two parameters:
- 1) sentence - type: string
- 2) character - type: string
- The function should return a count of every `character` present in `sentence`.
- HINT: You can access a single character in a string just like accessing an element in an array - `myString[3]` would access the fourth character
- */
- function countLetters (sentence,character){
- var counter=0;
- for(var i=0;i < sentence.length; i++){
- if(sentence[i] === character){
- counter+=1;
- }
- }
- return counter;
- }
- /*
- Define a function named `middleElement` that receives one parameter:
- 1) items - type: array
- The function should return the element present in the middle index. If
- the array contains an even number of elements, return the item in the
- first half of the array closest to the middle.
- HINT: You can use the expression `n % 2 === 0` to check if `n` is an even number
- */
- function middleElement(items){
- if(items.length%2 === 0){
- return items[(items.length*0.5)-1];
- } else {
- return items[Math.round(items.length*0.5)-1];
- }
- }
- /*
- Define a function named `rightToVote` that receives one parameter:
- 1) person - type: object
- The function should check the `age` property on `person`. If it is 18 or older,
- return true. Otherwise, return false. If `age` is not a number, throw an error.
- HINT: The `typeof` operator can tell you what data type a variable is
- */
- function rightToVote(person){
- if (typeof person.age !== 'number'){
- throw 'not a number!';
- }
- if(person.age >= 18){
- return true;
- } else {
- return false;
- }
- }
- /*
- Define a factory function `createStudent` that receives two parameters:
- 1) name - type: string
- 2) location - type: string
- This factory should return an object that includes the provided `name` and `location`
- properties and also has a `describe` method that returns the string: "Hello, I
- am {name} and I live in {location}."
- HINT: You will want to use the `this` keyword
- */
- function createStudent(name,location){
- var student = {
- name: name,
- location: location,
- describe: function(){
- return `Hello, I am ${this.name} and I live in ${this.location}.`;
- }
- }
- return student;
- }
Add Comment
Please, Sign In to add comment