Jump to content

kayut

Members
  • Content count

    17
  • Joined

  • Last visited

Community Reputation

0 Neutral

About kayut

  • Rank
    Newbie
  1. Hey, Can some one please explain to me why this works: let myNames = ['apple', 'orange', 'lemon']; let output = myNames.map(function(item){ return item; }); console.log(output); But the same code with forEach doesn't work? let myNames = ['apple', 'orange', 'lemon']; let output = myNames.forEach(function(item){ return item; }); console.log(output); Isn't it that myNames is an array and forEach is a built-in method of Array? Why is that the forEach is not working? Thanks
  2. Thanks, it makes sense. But I still don't get this code: outerFunction().kir(); I know that you can access the method kir of an object with myObject.kir(); But I have never seen myObject().kir(); Could you guys please explain a bit about it?
  3. Hey, If I use a closure inside an IIFE like this: var outerFunction = (function() { // this is an IIFE const outer = `I'm the outer function!`; var innerFunction = function() { const inner = `I'm the inner function!`; console.log(outer); // I'm a variable from the outer function! } return { kir:innerFunction } })(); It seems to me that the IIFE returns an object called outerFunction and add the method kir to it. So that now, I can access the kir method of the object outerFunction with the following code: outerFunction.kir(); But if I don't use an IIFE for the same example: function outerFunction () { const outer = `I'm the outer function!`; var innerFunction = function() { const inner = `I'm the inner function!`; console.log(outer); // I'm the variable from the outer function! } return { kir:innerFunction } } I can access the method kir only if I run this: outerFunction().kir(); Why it doesn't work if I run this?: outerFunction(); outerFunction.kir(); It seems to me that in the first example, when the IIFE runs automatically, it returns the object outerFunction and add the method kir to it. But when I run outerFunction manually, it's different and I can't access the method kir. I'm a bit confused! Can some one please explain to me why I can't access kir method in the second example by running this?: outerFunction(); outerFunction.kir(); I guess this is more a question about understanding IIFE rather than the closure. Thanks
  4. @iwato, I think you didn't read my question properly. Your answer has nothing to do with my question. First, this is a forum for JavaScript and not for jQuery. Second, my question is about forEach and your solution has nothing to do with forEach!
  5. Hey, I have two divs with class demo and want to change their border. I can do this as follow: <div class="demo">first div</div> <div class="demo">second div</div> let myClass = document.getElementsByClassName('demo'); console.log(myClass); // is an array for (i = 0; i < myClass.length; i++) { myClass[i].style.border = '1px solid red'; } I know that myClass is an array. So, I should be able to iterate through it with forEach method, isn't it? But how? Thanks
  6. Hey, There are 3 different ways to create a new object: let myObject = {}; var myObject = { name: “Engage", days: 30 } var myObject = new Object(); Now my question is about the 2 different ways of instantiating a new object. You can either use an existing object as prototype of your new object, like this: var person = { name: "David", age: 20, }; // Create theDude object and use person object as its prototype var theDude = Object.create(person); Or you can use a Constructor function to create your new object like this: function Person (name) { this.name = name; this.age = 20; this.getInfo = function getInfo() { return this.name + ' is ' + this.age + ' years old.'; }; } // Instantiating a new object using the new keyword and a constructor function var theDude = new Person("David"); My question is: What is the difference between the last 2 methods of instantiating a new object? Which method is more recommended? In the real world, which one is used more? Thanks
  7. Hi, Looking at this Pen: Animation I like to achieve that when the animation of the second element starts only after the animation of the first element is finished. When the animation of the second element is finished, the animation of the first element should start and so on (infinitive). I know that I used JavaScript in this (not working) example but I'm more than happy to have a working jQuery solution. Thanks
  8. Hi, I'm just learning the JavaScript touch event. For this I created this example: Example on Codepen In the above example, you can grab the element and move it to the right or left. It works. However I want to limit the horizontal movement of the element (for example only 100px to the left and 100px to the right). How can I do this? Thanks
  9. Hi, In the following script, each time you click the button it shows 4 more elements: Load 4 more elements I need to change the script so that when all elements are visible and you click again the button, all elements get hidden except of the first 4 elements. I tried for hours but couldn't find any solution. Could you please help me? Thanks
  10. Hi, I need to create a carousel (slider) which should like something like this: Which jQuery (or any other) carousel can I use for it? Thanks
  11. kayut

    JSON: Why does each property show twice!

    Thanks for your help. I found and fixed the issue now.
  12. Hi, looking at this example of Plunker: http://plnkr.co/edit/z0bnxjV2XkBZC0yJxL7Z?p=preview can some one please tell my why does each answer and each question coming from the JSON file show twice? Thanks
  13. kayut

    A tricky question about margin

    I just want to understand it. I mean if the first <p> element 50px margin-bottom and the second <p> element 50px margin-top has then the distance between two paragraphs should be 100px. Why is it still 50px ????
  14. kayut

    A tricky question about margin

    Hi, Consider a web page with two adjacent paragraphs: <p> first paragraph</p><p> second paragraph</p> With this CSS code applied to a web page: p { background-color: red; margin-top: 50px; margin-bottom: 50px;} What will be the distance between the two red rectangles displayed for the background color of these paragraphs? Answer: I thought it should be 100px but NO, the distance between two paragraphs would be only 50px. Can some one please explain WHY?? Tahnks
  15. kayut

    jQuery: include and exclude

    This is what I have to do:
×