JSFiddle - React, Tailwind, and code Playground

by Saeed Abbaspour

HTML

<h2> Excercise 1 </h2>

<p>Write a function that prints the numbers from 1 to 100. But for multiples of three print “Foo” instead of the number and for the multiples of five print “Bar”. For numbers which are multiples of both three and five print “FooBar”.</p>

<h3>Answer</h3>

<p id="e1"></p>
<hr>

<h2> Excercise 2 </h2>

<p>Write a function that determines the number of even numbers that appear in a range of integers 0..n, where n is supplied as the sole argument to your function.</p>

<h3>Answer</h3>

<p>How many even numbers appear between 0 and <span id="entry"></span>?</p>
<p id="e2"></p>
<hr>

<h2> Excercise 3 </h2>

<p>Given the following pseudocode, determine the range of possible values for “a” in your language of choice:</p>
<p>x = random_int(1,6)</p>
<p>y = random_int(1,6)</p>
<p>z = random_int(1,6)</p>
<p>a = x + y + z</p>

<h3>Answer</h3>

<p id="e3"></p>
<hr>

<h2> Excercise 4 </h2>

<p>Given:</p>
<p>words = ['one', 'one', 'two', 'three', 'three', 'two']</p>
<p>Remove the duplicates.</p>
 <h3>Answer</h3>

<p id="e4"></p>
<hr>

<h2> Excercise 5 </h2>

<p>Print the following output using only one loop:</p>
<p>1 1 1 1 1</p>
<p>2 2 2 2 2</p>
<p>3 3 3 3 3</p>
<p>4 4 4 4 4</p>
<p>5 5 5 5 5</p>

<h3>Answer</h3>

<p id="e5"></p>

CSS

body {
    font: 13px sans-serif;
    padding: 1%;
}

JavaScript

// Exercise No. 1
var temp;
for (i = 1; i < 101; i++) {
    if (i % 3 !== 0 && i % 5 !== 0) {
        temp = e1.innerHTML;
        e1.innerHTML = temp + i;
    } else {
        if (i % 3 === 0) {
            temp = e1.innerHTML;
            e1.innerHTML = temp + "Foo";
        }
        if (i % 5 === 0) {
            temp = e1.innerHTML;
            e1.innerHTML = temp + "Bar";
        }
    }
    temp = e1.innerHTML;
    e1.innerHTML = temp + ", ";
}

// Exercise No. 2
var counter = 0;
var n = prompt("Please Enter a positive number:");
for (i = 0; i < n; i++) {
    if (i % 2 === 0) {
        counter += 1;
    }
}
entry.innerHTML = n;
e2.innerHTML = counter;

// Exercise No. 3 
var x, y, z, a;
var range = [];
for (var i = 0; i < 500; i++) {
    x = Math.floor(Math.random() * 6) + 1;
    y = Math.floor(Math.random() * 6) + 1;
    z = Math.floor(Math.random() * 6) + 1;
    a = x + y + z;
    range.push(a);
}
var min = Math.min.apply(null, range),
    max = Math.max.apply(null, range);
e3.innerHTML = "Possible values for a are between " + min + " and " + max;

// Exercise No. 4
var words = ['one', 'one', 'one', 'two', 'three', 'three', 'two'];
var sorted_words = words.sort();
var lenW = words.length;
var results = [];
for (var i = 0; i < lenW; i++) {
    if (sorted_words[i + 1] !== sorted_words[i]) {
        results.push(sorted_words[i]);
    }
}
e4.innerHTML = results;

// Excercise No. 5
var arr = [];
for (var i = 0; i < 25; i++) {
    arr.push(( i % 5 ) + 1);
}
e5.innerHTML = arr.sort();