Exercises: JavaScript Loops
Standard Exercises
0. What is wrong with the following loop? What will happen if you try to run it?
var counter = 0;
while( counter < 3 ) {
console.log(counter);
}1. What would you do to fix the above loop? Try it out and run it in the console. It should print 0, 1, 2.
2. Write a while loop that prints the string "Hello World" 3 times.
3. Write a for loop that prints the string "Goodbye" 5 times.
4. Write a for or while loop that prints out the first 20 items of the "9x" table. (i.e. 9, 18, 27, ...).
5. Write a for loop to print out every variable in the 'names' array defined below;
var names = ["Chris", "Emily", "Tove"];
// print "Chris", "Emily, "Tove" on separate lines6. Write the function findHello(anArray); that takes an array and prints out the word "YES" if the array contains the string "Hello". Test it with the following code;
findHello( ["Good", "Bye", "Hello"] ); // Should print "YES"
findHello( [1, 2, 3, 4] ); // Should not print anything7. Write the function lastItem(anArray); which will print out the last element of an array. Test with the following code;
console.log( lastItem(["A", "B", "C"]) ); // Should print "C"8. Write the function removeZeros(anArray); which will remove all zeros from the given array. You will need to use anArray.splice([position], [count]) to do so.;
var anArray = [7, 9, 0, 1];
removeZeros(anArray);
console.log(anArray); // Should be "7,9,1"Advanced Exercises
Note. The next 3 questions were in the strings exercise this morning but need loops. Feel free to skip them if you have done them already!
9. Write the function countA(text); that counts the number of times the letter 'a' appears in a string and returns the result. Test it with the following code;
console.log( countA("This is a message.") ); // Should print 2
console.log( countA("Hello") ); // Should print 0
console.log( countA("Adam") ); // Should print 110. Notice how the previous function returned a 0 when passed the parameter 'Adam'. Rewrite your function so that it searches for both uppercase and lowercase 'a's. Test it with the following code;
console.log( countA("This is a message.") ); // Should print 2
console.log( countA("Hello") ); // Should print 0
console.log( countA("Adam") ); // Should print 211. Now make the generic function countLetters(text,letter); which should count all instances of a given letter in a string (upper or lower case). Tip. You might want to use .toUpperCase() or .toLowerCase() in your solution!
console.log( countLetters("Hello John!", "o") ); // Should print 2
console.log( countLetters("What a wonderful day!", "w") ); // Should print 2
console.log( countLetters("Hahahaha", "H") ); // Should print 412. Write the function getHighestValue(anArray) that returns the highest value from an array of numbers. Test it with the following code;
var highestValue = getHighestValue( [30, 50, 100, 10] );
console.log(highestValue); // Should print 100
console.log(getHighestValue([1, 5, 3, 2])); // Should print 5
console.log(getHighestValue([50, 70, 20, 90, 40])); // Should print 9013. Write a function or a script which selects the longest string from the array.
var names = ["Bob", "Brenda", "Alice", "Karen", "Björn"];
// Should print: Brenda14. Write a function or a script to print the values of this array in reverse, with one name printed per line. Try not to use the 'reverse' function but use a "backwards loop" instead.
var names = ["Bob", "Brenda", "Alice", "Karen", "Björn"];
// Should print: Björn, Karen, Alice, Brenda, Bob on separate lines.15. Write a function to rotate the elements of an array around (take the first element and put it on the end).
16. Write a function to randomly shuffy some of the elements of an array around (Tip. You might want to use "Math.random()" and "Math.floor()" to find a new position for the elements).
var i = Math.floor(Math.random() * 6); // Random number from 0, 1, 2, 3, 4, 5console.log("Hello World");