FizzBuzz

Given a number n, for each integer i in the range from 1 to n inclusive, print one value per line as follows:

  • If i is a multiple of both 3 and 5, print FizzBuzz.
  • If i is a multiple of 3 (but not 5), print Fizz.
  • If i is a multiple of 5 (but not 3), print Buzz.
  • If i is not a multiple of 3 or 5, print the value of i.

Function Description

Complete the function fizzBuzz in the editor below.

fizzBuzz has the following parameter(s):

int n: upper limit of values to test (inclusive)

Returns: NONE

Prints:

The function must print the appropriate response for each value i in the set {1, 2, … n} in ascending order, each on a separate line.

Constraints

  • 0 < n < 2 × 105

Input Format for Custom Testing

Input from stdin will be processed as follows and passed to the function.

The single integer n, the limit of the range to test: [1, 2, …n].

Sample Case 0

Sample Input

STDIN    Function
-----    --------
15    →  n = 15

Sample Output

1     
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz

Explanation

The numbers 3, 6, 9, and 12 are multiples of 3 (but not 5), so print Fizz on those lines.

The numbers 5 and 10 are multiples of 5 (but not 3), so print Buzz on those lines.

The number 15 is a multiple of both 3 and 5, so print FizzBuzz on that line.

None of the other values is a multiple of either 3 or 5, so print the value of i on those lines.

function fizzBuzz(n) {
	var i, res = '';
	for( i=1; i <= n; i++ ) {
		res = '';
		if( i % 3 == 0 ) {
			res += 'Fizz';
		}
		if( i % 5 == 0 ) {
			res += 'Buzz';
		}
		if( res == '' ) {
			res = i;
		}
		console.log(res);
	}
}
fizzBuzz(100);

React Like button

import cx from 'classnames';
import { Component } from 'react';

export default class LikeButton extends Component {
    state = {
        count: 100,
        liked: false
    }
    likeMe = () => {
        let newCount;
        if(this.state.liked) {
            newCount = this.state.count - 1;
        } else {
            newCount = this.state.count + 1;
        }

        this.setState({
          count: newCount,
          liked: !this.state.liked
        })
    }
    
    render() {
        return (
            <div>
                <div>
                    <h1 
                    className={`like-button ${this.state.liked ? "liked" : ""}`}
                    onClick={this.likeMe}
                    >Like | <span className={`likes-counter`}>{this.state.count}</span></h1>
                </div>
                <style>{`
                    .like-button {
                        font-weight: normal;
                        cursor: pointer;
                        font-family: Helvetica, Arial, Sans-Serif;
                        font-size: 1rem;
                        padding: 5px 10px;
                        color: #585858;
                        border: 1px solid #585858;
                        display: inline-block;
                    }
                   .liked {
                        font-weight: bold;
                        color: #1565c0;
                        border-color: #1565c0;
                   }
                `}</style>
            </div>
        );
    }
}

Maximum words counter in the sentence

You would like to find the sentence containing the largest number of words in some given text. The text is specified as a string S consisting of N characters: letters, spaces, dots, question marks and exclamation marks.

The text can be divided into sentences by splitting it at dots, question marks and exclamation marks. A sentence can be divided into words by splitting it at spaces. A sentence without words is valid, but a valid word must contain at least one letter.

For example, given S = “We test coders. Give us a try?“, there are three sentences: “We test coders“, “ Give us a try” and ““. The first sentence contains three words: “We“, “test” and “coders“. The second sentence contains four words: “Give“, “us“, “a” and “try“. The third sentence is empty.

Write a function that, given a string S consisting of N characters, returns the maximum number of words in a sentence.

For example, given S = “We test coders. Give us a try?“, the function should return 4, as explained above.

Given S = “Forget CVs..Save time . x x“, the function should return 2, as there are four sentences: “Forget CVs” (2 words), “” (0 words), “Save time ” (2 words) and “ x x” (2 words).

Assume that:

  • the length of S is within the range [1..100];
  • string S consists only of letters (a-z, A-Z), spaces, dots (.), question marks (?) and exclamation marks (!).

In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.

function maxWordsCounter(S) {
    var sentencesList = [];
    var wordsList = [];
    var lenList = [];
    var i, j, wordCounter = 0;
    var updS = S;

    updS = updS.replace(/\.|\!|\?/g, '|'); // replace '.!?' with '|' (pipe symbol)
    sentencesList = updS.split('|');
    for (var i in sentencesList) {
        wordsList = [];
        wordsList = sentencesList[i].split(' ');
        for (var j in wordsList) {
            if(wordsList[j].trim() !== '') {
                wordCounter++;
            }
        }
        lenList.push(wordCounter);
        wordCounter = 0;
    }
    return Math.max(...lenList);
}

Unique integers that sum up to 0

Write a function that, given an integer N (1 ≤ N ≤ 100), returns an array containing N unique integers that sum up to 0. The function can return any such array.

For example, given N = 4, the function could return [1, 0, −3, 2] or [−2, 1, −4, 5]. The answer [1, −1, 1, 3] would be incorrect (because value 1 occurs twice). For N = 3 one of the possible answers is [−1, 0, 1] (but there are many more correct answers).

function uniqueIntegers(N) {
    result = [];
    var x = Math.floor(N / 2);
    var i = 0;
    if (N % 2 === 0) { // even
        i = 0;
    } else { // odd
        result[0] = 0;
        i = 1;
    }
    while (i < N) {
        result[i] = x;
        result[i + 1] = -1 * x;
        i += 2;
        x--;
    }
    return result;
}

Maximum depth of nested ol/ul

You are given a DOM tree and have to analyze the ul and ol list tags within it. Your task is to find the maximum depth of nested ul/ol list tags. A single ul/ol list is nested one level deep. Each ul/ol list inside another ul/ol list is nested one level deeper. If there are no ul or ol lists at all in the DOM tree, the depth of nesting is 0.

Note that ul/ol lists can be nested directly or indirectly; that is, a ul list inside a table inside an ol list is nested two levels deep.

For example, given an HTML document with the following contents within the body tag:

<ul>
  <li>Item:
     <ol>
       <li>Point:
         <div>
           <ul>
             <li>elem1</li>
           </ul>
         </div>
       </li>
     </ol>
  </li>
  <li>elem2</li>
</ul>
<ul>
  <li>simple list1</li>
</ul>

there is a ul list nested three levels deep. Namely, “elem1” is in a ul list which is inside an ol list containing “Point”, while this ol list is inside another ul list containing “Item”.

Write a function that, given a DOM tree, returns the maximum depth of nested ul/ol lists. For example, given the DOM tree of the document shown above, the function should return 3, as explained above.

Given the following content:

<ol>
  <li>
    <ul>
      <li></li>
    </ol>
  </li>
</ol>

the function should return 2.

Assume that:

  • the DOM tree represents a valid HTML5 document;
  • length of the HTML document does not exceed 4KB;
  • jQuery is supported.
function maxDepth() {
    var maxDepth = 0,
    tempDepth,
    parents,
    targetSelector = 'ul, ol';

    $(targetSelector).each( function() {
        parents = $(this).parents(targetSelector);
        tempDepth = 1;
        if (parents) {
            tempDepth = parents.length + 1;
        }
        if (tempDepth > maxDepth) {
            maxDepth = tempDepth;
        }
    });
    return maxDepth;
}

Alternating characters

Write a function that returns a string of length Num consisting of alternating characters: “a” and “b”, starting with an “a” character.
For example, giver Num=5, your function should return “ababa”.

function altChars(Num) {
    let result = '';
    for (i = 0; i < Num; i++) {
        if (i % 2 == 0) {
            result += 'a';
        } else {
            result += 'b';
        }
    }
    return result;
}

Days of the week

Days of the week are represented as three-letter strings (“Mon”, “Tue”, “Wed”. “Thu”, “Fri”, “Sat”. “Sun”).
Write a function solution that, given a string S representing the day of the week and an integer K (between 0 and 500, inclusive), returns the day of the week that is K days later.
For example, given S = “Wed” and K = 2, the function should return “Fri”.
Given S = “Sat” and K = 23, the function should return “Mon”.

function weekDay(S, K) {
  var weekDays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
  
  // get the index of the day
  var dayIndex = weekDays.indexOf(S); 
  
  // get the offset index
  var moduloIndex = (dayIndex + K) % weekDays.length;
  return weekDays[moduloIndex];
}

Buildings and volume of water stuck

Given buildings of width 1 and variable height, write a function returning the volume of water which remains stuck between them when it rains. This function has to be written in JavaScript.
The program will receive the list of the building heights as input array. Your program should return volume of water stuck between the buildings.
For example input: [2,1,3] result should be: 1
For example input: [3,1,2,4] result should be: 3

function result(Arr) {
    var sum = 0;
    var lArr = [];
    var rArr = [];
    var leftMax = 0;
    var tmpMax = 0;
    var rightMax = 0;
    for (Ai = 0; Ai < Arr.length; Ai++) {
      // get maximum to the right of the current element
      lArr = Arr.slice(0, Ai);
      if(lArr.length > 0) {
        tmpMax = Math.max(...lArr);
      } else {
        tmpMax = 0;
      }
      leftMax = tmpMax;
      
      // get maximum to the right of the current element
      rArr = Arr.slice(Ai+1);
      if(rArr.length > 0) {
        tmpMax = Math.max(...rArr);
      } else {
        tmpMax = 0;
      }
      rightMax = tmpMax;
      
      // get amount of water based on the left and right max elements
      if(Arr[Ai] < leftMax && Arr[Ai] < rightMax) {
        sum += Math.min(leftMax, rightMax) - Arr[Ai];
      }
    }
    return sum;
}