Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Enter "hello" ( hello )

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Enter "hello" ( hello )

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Enter "hello" ( hello )

Have an account? Sign In Now

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the question so it can be answered easily.

Please choose the appropriate section so the question can be searched easily.

Please choose suitable Keywords Ex: question, poll.

Browse
Type the description thoroughly and in details.

Choose from here the video type.

Put Video ID here: https://www.youtube.com/watch?v=sdUUx5FdySs Ex: "sdUUx5FdySs".

Enter "hello" ( hello )

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

IT Community and Answers

IT Community and Answers Logo IT Community and Answers Logo

IT Community and Answers Navigation

Search
Ask A Question

Mobile menu

Close
Ask a Question
You can use WP menu builder to build menus
Home/ Questions/Q 4456
Next
john_t
john_tEnlightened
Asked: September 1, 20212021-09-01T22:29:34+00:00 2021-09-01T22:29:34+00:00In: Codewars solutions and discussions

Do something “n.times” (Simplifying “for” loops).js

/*

Description:

Do something “n.times” (Simplifying “for” loops)

Overview

In computer programming, we have a very basic but key principle called the DRY principle. DRY stands for “Don’t Repeat Yourself” which basically means that if you have multiple identical (or very similar) blocks of code you should probably simplify it. This is very important because it usually makes the code more readable and understandable to fellow developers and because it always improves performance.

For example, if we wanted to print “Hello World” to the console 100 times, we could write something like this:

// UGH! Disgusting!

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

console.log(“Hello World”);

However, the problem is, the code is barely readable and looks very ugly. How many times exactly is the string “Hello World” printed to the console? Maybe it is only printed 99 times? Maybe 1000? Also, if there is an error in the statement/code (console.log), the same bug will have to be fixed 100 times and I doubt anyone would enjoy doing that.

Luckily, in just about every modern high-level programming language, there exists a for loop that makes the code much more readable and DRY:

// Much better 😀

for (var i = 0; i < 100; i++) { console.log("Hello World"); } By executing the same action 100 times by using a single for loop instead of copying and pasting the same code 100 times, the code becomes much more readable and easy to debug. For example, if I misspelt "Hello World" I only need to fix it in one place to eliminate the bug as opposed to fixing it in every console.log statement. Task However, I myself find the for loop very complex and unreadable. Think about it - would you know what the for loop does if you've never learned computer programming? For example, if you've never coded before, would you have any idea what this thing in the for loop: i = 0; i < number; i++ means? Therefore, I would like you to define a method callable on integers, Number.prototype.times, that effectively replaces the for loop. It should work like this: // Prints "Hello World" to the console 100 times // Isn't this much more readable? At least a non-programmer // knows that something is being executed 100 times! (100).times(_ => {

console.log(“Hello World”);

});

Sometimes, we also want to loop through an array. For example, if we have an array example = [1,2,3,4], we could use our newly defined method to loop over the array like this:

example.length.times(_ => {

// Code to be executed

});

However, here lies the problem – since the function does not accept any arguments, how can we use this method to loop over every element in the array? Therefore, your Number.prototype.times method should supply the iteration variable i to the anonymous function being executed to support this feature of looping through every element in an array, like such:

example.length.times(i => {

console.log(example[i]);

});

/* Prints the following to the console:

1

2

3

4

*/

The iteration variable supplied to the anonymous function could also be used like this:

(100).times(i => {

console.log(i);

});

/* Prints all integers from 0 (inclusive) to 100 (exclusive) */

Task Summary

In case you got lost, here’s precisely what you have to do: define a method Number.prototype.times that accepts a function f as an argument and executes it as many times as the integer it is called on (e.g. (100).times would execute something 100 times). The iteration variable i should be supplied to the anonymous function being executed in order to support looping through array elements.

FUNDAMENTALS

*/

Number.prototype.times = function (f) {

for (let i = 0; i < this; i++) f(i) }

Related Questions and Answers:

  • Job Matching #1 codewars js 8 kyu
  • What can the Product List Performance report be used for? (select three)
  • You decide to run a landing page experiment to test a new carousel which highlights your…
  • Which reflects a best practice for designing your report?
  • Fix the Bugs (Syntax) - My First Kata.js
  • 0
  • 0 0 Answers
  • 44 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse
Enter "hello" ( hello )

Sidebar

Ask A Question

Stats

  • Questions 778
  • Answers 10
  • Best Answers 8
  • Users 2
  • Popular
  • Answers
  • Anonymous

    Logical calculator Codewars JS 8 kyu

    • 2 Answers
  • Anonymous

    Reversed sequence javascript codewars 8 kyu

    • 2 Answers
  • john_t

    Are arrow functions odd?.js

    • 1 Answer
  • john_t
    john_t added an answer function odds(values){ return values.filter(values => values % 2 != 0);… September 12, 2021 at 6:29 pm
  • john_t
    john_t added an answer function toCsvText(array) { return array.join('\n'); } https://www.codewars.com/kata/5a34af40e1ce0eb1f5000036/train/javascript September 12, 2021 at 6:20 pm
  • john_t
    john_t added an answer function solution(a, b){ return a.length < b.length ? a +… September 7, 2021 at 8:04 pm

Related Questions

  • Anonymous

    Short Long Short codewars js

    • 1 Answer
  • john_t

    Fuel Calculator.js

    • 0 Answers
  • john_t

    Freudian translator.js

    • 0 Answers

Top Members

john_t

john_t

  • 774 Questions
  • 55k Points
Enlightened
Jacob

Jacob

  • 1 Question
  • 21 Points
Begginer

Trending Tags

codewars codewars js 8 kyu codewars kata codewars kata solution codewars solutions data studio ga individual qualification exam answers google ads Google Analytics Individual Qualification google analytics individual qualification exam answers javascript js Segment Conversion Type Name

Explore

  • Questions
    • New Questions

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.