Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Type JavaScript here and click "Run Code" or press Ctrl + s
- console.log('Hello, world!');
- // Challenge 1
- function addTwo(num) {
- return num + 2;
- }
- // To check if you've completed it, uncomment these console.logs!
- console.log(addTwo(3));
- console.log(addTwo(10));
- // Challenge 2
- function addS(word) {
- return word + "s";
- }
- // uncomment these to check your work
- console.log(addS('pizza'));
- console.log(addS('bagel'));
- // Challenge 3
- function map(array, callback) {
- var someArray = [];
- for (i=0; i<array.length; i++) {
- someArray.push(callback(array[i]));
- }
- return someArray;
- }
- console.log(map([1, 2, 3], addTwo));
- // Challenge 4
- function forEach(array, callback) {
- for (i=0; i<array.length; i++) {
- callback(array[i]);
- }
- }
- var alphabet = '';
- var letters = ['a', 'b', 'c', 'd'];
- forEach(letters, function(char) {
- alphabet += char;
- });
- console.log(alphabet);
- // see for yourself if your forEach works!
- //--------------------------------------------------
- // Extension
- //--------------------------------------------------
- //Extension 1
- function mapWith(array, callback) {
- var someArray = []
- forEach(array, function(item){
- someArray.push(callback(item));
- });
- return someArray;
- }
- var blah = mapWith([1, 2, 3], addTwo);
- console.log("blah");
- console.log(blah);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement