Skip to content

JavaScript Test 1

Asabeneh edited this page May 13, 2019 · 4 revisions
  1. Looping a triangle Write a loop that makes seven calls to console.log to output the following triangle:
#
##
###
####
#####
######
#######

It may be useful to know that you can find the length of a string by writing .length after it.

  1. FizzBuzz Write a program that uses console.log to print all the numbers from 1 to 100, with two exceptions. For numbers divisible by 3, print "Fizz" instead of the number, and for numbers divisible by 5 (and not 3), print "Buzz" instead. When you have that working, modify your program to print "FizzBuzz" for numbers that are divisible by both 3 and 5 (and still print "Fizz" or "Buzz"for numbers divisible by only one of those).

  2. Maximum Math.max returns its largest argument. We can build something like that now. Write a function findMax that takes three arguments and returns their maxiumum. Without method Math.max method.

console.log(findMax(0, 10, 5));
10
console.log(findMax(0, -10,-2));
0
  1. Reversing an array Arrays have a reverse method which changes the array by inverting the order in which its elements appear. For this exercise, write a function, reverseArray. The reverseArray, takes an array as argument and produces a new array that has the same elements in the inverse order. Without reverse method.
console.log(reverseArray(["A", "B", "C"]));
 ["C", "B", "A"]
  1. Modifying an array Write a function called modifyArray takes array as parameter and modifies the fifth item of the array and returns the array. If the array length is less than five it return ‘item not found’.
console.log(modifyArray(["Avocado", "Tomato", "Potato","Mango", "Lemon","Carrot"]);
// →["Avocado", "Tomato", "Potato","Mango", "LEMON", "Carrot"]
console.log(modifyArray(["Google", "Facebook","Apple", "Amazon","Microsoft",  "IBM"]);
// →["Google", "Facebook","Apple", "Amazon","MICROSOFT",  "IBM"]
console.log(modifyArray(["Google", "Facebook","Apple", "Amazon"]);
// →"Not Found"

6.Write a function which returns array of seven random numbers in a range of 0-9. All the numbers must be unique

sevenRandomNumbers()
[1,4,5,7,9,8,0]
  1. Write a funch which takes any number of arguments and return the sum of the arguments
sum(1,2,3) 
 6
sum(1,2,3,4) 
10
  1. Write a function which removes items from the middle of an array and replace with three items.
removeMiddleItem([1,2,3], 4,5,6)
[1,4,5,6,3]
removeMiddleItem([1,2,3,4], 4,5,6) 
 [1,4,5,6,4]
  1. Calculate the total annual income of the person by extracting the following text. 'He earns 5000 euro from salary per month, 10000 euro annual bonus, 15000 euro online courses per month.'

  2. Create a function that takes two strings and returns true if the first argument ends with the second argument; otherewise return false . Take two strings as arguments. Determine if second string matches ending of first string. Return boolean value. Example

yourFunc("integrity", "ity")
true
yourFunc("integration", "tio")
false
yourFunc("connection", "sion")
false
yourFunc("connection", "tion")
true
Clone this wiki locally