0% found this document useful (0 votes)
107 views

Javascript Excercises v2 v.1

The document contains 13 coding exercises involving writing functions to perform various string and number operations. The exercises include writing functions to: - Calculate average weeks in a lifetime given years, days in year, days in week variables - Return a passed parameter - Return a greeting with a passed name - Return the sum of calling two functions that return strings - Return a string duplicated and logged - Return the length of a passed string - Return the sum of two passed numbers - Return the sum of 4 numbers divided by 2 and multiplied by 10 - Return true if sum of two numbers is less than 100, else false - Return true if a number is less than or equal to zero, else false

Uploaded by

Nikola
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views

Javascript Excercises v2 v.1

The document contains 13 coding exercises involving writing functions to perform various string and number operations. The exercises include writing functions to: - Calculate average weeks in a lifetime given years, days in year, days in week variables - Return a passed parameter - Return a greeting with a passed name - Return the sum of calling two functions that return strings - Return a string duplicated and logged - Return the length of a passed string - Return the sum of two passed numbers - Return the sum of 4 numbers divided by 2 and multiplied by 10 - Return true if sum of two numbers is less than 100, else false - Return true if a number is less than or equal to zero, else false

Uploaded by

Nikola
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Exercise 1

Given three variables

var daysInYear = 365.25;

var daysInWeek = 7;

var yearsInLifetime = 80;

Calculate the average number of weeks in a human lifetime.

Exercise 2

Write a function echo that also returns the passed parameter. echo('Code Academy') should return 'Code
Academy' and echo('Students') should return 'Students'

Solution:

var give = function(input) {


return input;
};

Exercise 3

Write a function greet having one parameter and returning 'Hello <parameter>!'. greet('Ada') should return 'Hello
Ada!' and greet('Tayo') should return 'Hello Tayo!'.

Solution:

var greet = function(name) {


return 'Hello ' + name + '!';
};

Exercise 4

Which value does x have after execution of the following code?

Solution 1
function hi(name) {
return 'Hi ' + name + '!';
};

Solution 2

var hi = function (name) {


return 'Hi ' + name + '!';
};
var h1 = hi('Selva');
var h2 = hi('Pola');
var x = h1 + ' ' + h2;

Solution:

'Hi Selva! Hi Pola!'

Exercise 5

Write a function shout that takes a string and returns this string duplicated. In addition, the return should
be logged. The call shout('Fire') should return 'FireFire' and should log 'FireFire'.

Solution:

var shout = function(word) {


var result = word + word;
console.log(result);
return result;
};

function shout (word) {


var result = word + word;
alert(result);
};

Exercise 6

Write a function length that takes a string and returns the number of characters of the
string. length('sun') should return 3.

Exercise 7

Create a function that takes two numbers as arguments and return their sum.

Exercise 8

Create a function that takes 4 numbers as arguments and return their sum divided by 2, multiplied by 10.
Exercise 9

Given two numbers, return true if the sum of both numbers is less than 100. Otherwise return false.

Exercise 10

Create a function that takes a number as its only argument and returns true if it's less than or equal to
zero, otherwise return false.

Exercise 11

Write a JavaScript function to test whether the character at the provided (character) index is “a”.

Solution:

function checkCharacter (str, index, a) {


return str.charAt(index) === a;
}

Exercise 12

Extend the previous function in Exercise 11.


Extend the function with: if the character is “a” , replace it with “aaa”, if it is not “a”, then replace it with
“a”.

Exercise 13

Write a function to test whether the character at the provided (character) index is upper case
Solution

function isUpperCaseAt(str, index) {


return str.charAt(index).toUpperCase() === str.charAt(index);
}

You might also like