# 09/07/21 Evchuk Dayana ## 1 [HTML dynamic color string generation](https://www.codewars.com/kata/56f1c6034d0c330e4a001059) 6 kyu Generate a valid randomly generated hexadecimal color string. Assume all of them always have 6 digits. ### JS ``` var generateColor = function() { let chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']; let hashcode = '#'; for (let i = 0; i < 6; i++) { hashcode += chars[Math.floor(Math.random() * chars.length)]; } return hashcode; }; ``` ### Java ``` import java.util.Random; public class GenerateColorRGB { public static final String symbols = "0123456789abcdef"; public static String generateColor(Random r) { String rgb = "#"; for(int i=0; i<6; i++){ rgb+=symbols.charAt(r.nextInt(symbols.length())); } return rgb; } } ``` ## 2 [field chained HTML formatting](https://www.codewars.com/kata/5e98a87ce8255200011ea60f) 5 kyu We want to create an object with methods for various HTML elements: div, p, span and h1 for the sake of this Kata. These methods will wrap the passed-in string in the tag associated with each. ### JS ``` class Format extends Function { static get div() {return new Format("<div>", "</div>")} static get p() {return new Format("<p>", "</p>")} static get span() {return new Format("<span>", "</span>")} static get h1() {return new Format("<h1>", "</h1>")} constructor(open="", close="") { super(); this.open = open; this.close = close; function print(...texts){return open + texts.join("") + close}; Object.setPrototypeOf(print, this); return print; } get div() {return new Format(this.open + "<div>", "</div>" + this.close)} get p(){return new Format(this.open + "<p>", "</p>" + this.close)} get span() {return new Format(this.open + "<span>", "</span>" + this.close)} get h1(){return new Format(this.open + "<h1>", "</h1>" + this.close)} } ``` ### Java ``` import java.util.Random; public class Format extends Function { static get div() {return new Format("<div>", "</div>")} ; static get p() {return new Format("<p>", "</p>")} ; static get span() {return new Format("<span>", "</span>")} ; static get h1() {return new Format("<h1>", "</h1>")} ; constructor(open="", close="") { super(); this.open = open; this.close = close; function print(...texts){return open + texts.join("") + close}; Object.setPrototypeOf(print, this); return print; } get div() {return new Format(this.open + "<div>", "</div>" + this.close)} ; get p(){return new Format(this.open + "<p>", "</p>" + this.close)} ; get span() {return new Format(this.open + "<span>", "</span>" + this.close)} ; get h1(){return new Format(this.open + "<h1>", "</h1>" + this.close)} ; } ``` ## 3 [HTML Generator] (https://www.codewars.com/kata/54eecc187f9142cc4600119e) 7 Kyu To organize your code, make of all your functions methods of a class called HTMLGen. Tag functions should be named after the tag of the element they create. Each function will take one argument, a string, which is the inner HTML of the element to be created. The functions will return the string for the appropriate HTML element. ### JS ``` function HTMLGen() {} ['a', 'b', 'p', 'body', 'div', 'span', 'title'].forEach(function(tag) { HTMLGen.prototype[tag] = function(content) { return '<' + tag + '>' + content + '</' + tag + '>' } }) HTMLGen.prototype.comment = function(content) { return '<!--' + content + '-->' } ``` ### Java ``` import java.util.Random; public class HTMLGen() {} ['a', 'b', 'p', 'body', 'div', 'span', 'title'].forEach(function(tag) { HTMLGen.prototype[tag] = function(content) { return '<' + tag + '>' + content + '</' + tag + '>' ; } }) HTMLGen.prototype.comment = function(content) { return '<!--' + content + '-->' } ``` ### 4 Generate HTML links https://www.codewars.com/kata/56896f078dcf3e886c000067 7 kyu ### JS ``` function generateMenu (menuItems) { if (menuItems.length == 0) return ""; else { var result = ""; for (var i = 0; i < menuItems.length; i++) { result += "<a href=\"" + menuItems[i].url + "\">" + menuItems[i].text + "</a>"; } return result; } } ``` ### Java ``` import java.util.Random; public class generateMenu (menuItems) { if (menuItems.length == 0) return ""; else { int result = ""; for (int i = 0; i < menuItems.length; i++) { result += "<a href=\"" + menuItems[i].url + "\">" + menuItems[i].text + "</a>"; } return result; } } ``` ### 5 [htmlify my text](https://www.codewars.com/kata/55efee33bd91ae75ac00003c) 7kyu ### JS ``` function htmlify(string){ return '<p>' + string .replace(/–/g, '&ndash;') .replace(/& &/g, '&amp; &amp;') .replace(/’/g, '&rsquo;') .replace(/\s&\s/g, ' &amp; ') .replace(/“/g, '&ldquo;') .replace(/”/g, '&rdquo;') + '</p>' } ``` ### Java ``` import java.util.Random; public class htmlify(string){ return '<p>' + string .replace(/–/g, '&ndash;') ; .replace(/& &/g, '&amp; &amp;') ; .replace(/’/g, '&rsquo;') ; .replace(/\s&\s/g, ' &amp; ') ; .replace(/“/g, '&ldquo;') ; .replace(/”/g, '&rdquo;') ; + '</p>' ; } ```