001 - JS Scratch

In this example below you will see how to do a 001 - JS Scratch with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by jmanhart, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright jmanhart ©
  • HTML
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>001 - JS Scratch</title>
  
  
  
  
  
</head>

<body>

  
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/jmanhart/001-js-scratch-OvjoEe */
// Infinite While Loop

let flag = true;
let counter = 0;
while(flag === true){
  console.log(counter);
  counter+=1;
  if (counter === 2){
    break;
  }
}

// While

let cards = ['Diamond', 'Spade', 'Heart', 'Club'];

let currentCard = 'Heart'

while (currentCard != 'Spade') {
  console.log(currentCard);
  currentCard = cards[Math.floor(Math.random() * 4)];     
}

console.log(currentCard)


// For Each

let groceries = ['whole wheat flour', 'brown sugar', 'salt', 'cranberries', 'walnuts']; 

groceries.forEach(function(groceryItem) {
  console.log(' - ' + groceryItem);
});

let fruits = ['mango', 'papaya', 'pineapple', 'apple'];

fruits.forEach(function(fruitWant){
  console.log('I want to eat a ' + fruitWant)
});

fruits.forEach(fruitWant =>
   console.log('I want to eat a ' + fruitWant)            
);


// .map
// In the previous exercise, we called the .forEach() method and learned that it returns undefined. It also does not change the array it is called upon. What if we do want to change the contents of the array? We can use .map()!
// The syntax for .map() is almost the same as the syntax for .forEach(), with one important change. Notice that directly before the function call, the code reads, let bigNumbers =. This is because .map() returns a new array with elements that have been modified by the code in its block. bigNumbers is the new array in which the method will save the values.

// .map() can also be written with arrow function syntax.

let numbers = [1, 2, 3, 4, 5]; 

let bigNumbers = numbers.map(function(number) {
  return number * 10;
});

// .filter()
// Another useful iterator method is .filter(). Like .map(), .filter() returns a new array. However, .filter() returns certain elements from the original array that evaluate to truthy based on conditions written in the block of the method.

let words = ['chair', 'music', 'pillow', 'brick', 'pen', 'door']; 

let shortWords = words.filter(function(word) {
  return word.length < 6;
});

let shortWords = words.filter(word => word.length < 6);



// Review: Iterators
// You have learned a number of useful methods in this lesson as well as how to use the JavaScript documentation from the Mozilla Developer Network to discover and understand additional methods. Let's review!

// .forEach() is used to execute the same code on every element in an array but does not change the array and returns undefined.
// .map() executes the same code on every element in an array and returns a new array with the updated elements.
// .filter() checks every element in an array to see if it meets certain criteria and returns a new array with the elements that return truthy for the criteria.
// All iterator methods can be written using arrow function syntax. In fact, given the succinctness and the implicit return of arrow function syntax, this is quickly becoming the preferred way to write these types of method calls.
// You can visit the Mozilla Developer Network to learn more about iterator methods (and all other parts of JavaScript!).
// Additional iterator methods such as .some(), .every(), .reduce() perform different functions.

Comments