//Exercises Level 1 //Exercise 1 -- Three parts //Iterate 0 to 10 using for loop for(var a=0; a<11; a++) console.log(`For loop: ${a}`); //Iterate 0 to 10 using while a=0; while(a<11) { console.log(`While loop: ${a}`); a++; } //Iterate 0 to 10 using do while loop a=0; do { console.log(`Do while: ${a}`); a++; }while(a<11) //Exercise 2 -- Three parts //Iterate 10 to 0 using for loop for(var a=10; a>-1; a--) console.log(`For loop: ${a}`); //Iterate 10 to 0 using while a=10; while(a>-1) { console.log(`While loop: ${a}`); a--; } //Iterate 10 to 0 using do while loop a=10; do { console.log(`Do while: ${a}`); a--; }while(a>-1) //Exercise 3 -- Iterate 0 to n using for loop var n = 100; for(a=0; a<=n; a++) console.log(a); //Exercise 4 -- Write a loop that makes the following pattern using console.log(): (See pattern provided in .md file) for(a=1; a<=7; a++) console.log("#".repeat(a)); //Exercise 5 -- Use loop to print the following pattern: for(a=0;a<=10; a++) console.log(`${a} x ${a} = ${a*a}`) //Exercise 6 -- Using loop print the given pattern using console.log(): (See pattern provided in .md file) console.log(`i i^2 i^3`) for(a=0; a<=10;a++) console.log(`${a} ${Math.pow(a,2)} ${Math.pow(a,3)}`) //Exercise 7 -- Use for loop to iterate from 0 to 100 and print only even numbers for(a=0;a<=100;a++) { if(a%2==0) console.log(a); } //Exercise 8 -- Use for loop to iterate from 0 to 100 and print only odd numbers for(a=0;a<=100;a++) { if(!(a%2==0)) console.log(a); } //Exercise 9 -- Use for loop to iterate from 0 to 100 and print only prime numbers // function to check if a given number is prime function isPrime(n) { if(n == 1 || n == 0) // since 0 and 1 is not prime return false. return false; for(var i = 2; i < n; i++) // Run a loop from 2 to n-1 { if(n % i == 0) // if the number is divisible by i, then n is not a prime number. return false; } return true; // otherwise, n is prime number. } for(var i = 1; i <= 100; i++) // check for every number from 1 to N { if(isPrime(i)) // check if current number is prime console.log(`Prime number: ${i}`); } //Exercise 10 -- Use for loop to iterate from 0 to 100 and print the sum of all numbers. //The sum of all numbers from 0 to 100 is 5050. var sum = 0; for(a=0;a<=100;a++) sum = sum + a; console.log(sum); //Exercise 11 -- Use for loop to iterate from 0 to 100 and print the sum of all evens and the sum of all odds. //The sum of all evens from 0 to 100 is 2550. And the sum of all odds from 0 to 100 is 2500. var evenSum = 0; var oddSum = 0; for(a=0;a<=100;a++) { if(a%2==0) evenSum=evenSum+a; else oddSum=oddSum+a; } console.log(`oddSum: ${oddSum}`); console.log(`evenSum: ${evenSum}`); //Exercise 12 -- Use for loop to iterate from 0 to 100 and print the sum of all evens and the sum of all odds. //Print sum of evens and sum of odds as array // [2550, 2500] var evenSum = 0; var oddSum = 0; var myArray = [0,0]; for(a=0;a<=100;a++) { if(a%2==0) myArray[0]=myArray[0]+a; else myArray[1]=myArray[1]+a; } console.log(myArray); //Exercise 13 -- Develop a small script which generate array of 5 random numbers function generateNumArray(numbersToGenerate) { var myArray = []; var looper=1; var num =0; while(looper<=numbersToGenerate) { num = Math.floor(Math.random()*11); myArray.push(num); looper++; } return myArray; } console.log(generateNumArray(5)); //Exercise 14 -- Develop a small script which generate array of 5 random numbers and the numbers must be unique var uniqueArray = []; while(uniqueArray.length<5) { var randomNum = Math.floor(Math.random() * 100) + 1; if(uniqueArray.indexOf(randomNum) === -1) uniqueArray.push(randomNum); } console.log(uniqueArray); //Exercise 15 -- Develop a small script which generate a six characters random id: // 5j2khz function makeRandomID(length) { let resultID = ""; const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; const charLength = characters.length; var counter = 0; while (counter 0) console.log(`${itemsContaininingIA}`); else console.log(`There are no countries ending with ia`); //Exercise 9 -- Using the above countries array, finds the country containing the biggest number of characters and prints the result to console // Ethiopia var longestStringLength = 0; var longestStringIndex = ""; for(a=0;a longestStringLength) { longestStringLength = countriesList[a].length; longestStringIndex = a; } else continue; } console.log(`Longest String in array is: ${countriesList[longestStringIndex]}`) //Exercise 10 -- Using the above countries array, find the country or countries containing only 5 characters. // ['Japan', 'Kenya'] var tempArray = []; for(a=0;a wordCounter) { wordCounter = webTechs[a].length; index = a; } else continue; } console.log(`The longest word in the webTechs array is: ${webTechs[index]}`); //Exercise 12 -- Use the webTechs array to create the following array of arrays: and prints the result to console // [["HTML", 4], ["CSS", 3],["JavaScript", 10],["React", 5],["Redux", 5],["Node", 4],["MongoDB", 7]] console.log(webTechs); var newArray = []; console.log(`test`) for(a=0; a=0;a--) tempFruitArray.push(fruitArray[a]); fruitArray = tempFruitArray; console.log(fruitArray); //Exercise 16 -- Print all the elements of array as shown below. // const fullStack = [ // ['HTML', 'CSS', 'JS', 'React'], // ['Node', 'Express', 'MongoDB'] // ] // HTML // CSS // JS // REACT // NODE // EXPRESS // MONGODB const fullStack = [['HTML', 'CSS', 'JS', 'React'], ['Node', 'Express', 'MongoDB']]; for(a=0;a highestCountOfCharacters) { highestCountOfCharacters = countries[a].length; highestIndex = a; } } console.log(`The country with the highest number of characters in the country array is: ${countries[highestIndex]}`); //Exericse 6 -- Extract all the countries contain the word 'land' from the countries array and print it as array var landArray = []; for(a=0;a