Advertisement
makispaiktis

Basic Examples

Aug 14th, 2020 (edited)
1,546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function square(arr){
  2.     result = []
  3.     for(let i=0; i<arr.length; i++){
  4.         result.push(arr[i] * arr[i]);
  5.     }
  6.     return result
  7. }
  8.  
  9. console.log()
  10. console.log("******** 1. FOR-LOOP ********")
  11. console.log(square([1,2,3,4,5,6,7,8,9,10]));
  12. console.log();
  13. console.log("******** 2. STRING METHODS ********")
  14. str1 = "Hello World";
  15. str2 = str1.replace("Hello", "Goodbye");
  16. str3 = str2.toUpperCase();
  17. console.log("str1 = " + str1);
  18. console.log("str2 = " + str2);
  19. console.log("str3 = " + str3);
  20. console.log("Index of 'H' and 'l' in str1: (" + str1.indexOf('H') + ", " + str1.indexOf('l') + ")")
  21. console.log();
  22. console.log("******** 3. CAPITALIZING THE FIRST LETTERS OF WORDS and SUBSTR METHOD ********");
  23. sentence = "You ARe vErY goOd at programming bUT noT LIKE me!";
  24. words = sentence.split(" ");
  25. console.log("Sentence = " + sentence);
  26. // words[0] = "You", words[1] = "are", ....
  27. for(let i=0; i<words.length; i++){
  28.     words[i] = words[i].substr(0,1).toUpperCase() + words[i].substr(1).toLowerCase();
  29. }
  30. console.log("words = " + words);
  31. console.log("With join: " + words.join(" "));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement