JSFiddle - React, Tailwind, and code Playground
by dashk
HTML
<div id="result"></div>
JavaScript
/**
* Print out the series 1 through N replacing numbers divisible by 'A' by F,
* numbers divisible by 'B' by B and numbers divisible by both as 'FB'.
*
* @param {String} line Input - It may consist multiple lines
* @return {String} Output per specification - One "set" per line, separated
* by a new line character
**/
function codeEvalExecute(line)
{
// Check if given input has nothing
if (!line || line.length < 0) return "";
// Array to store the output
var output = [];
// Breakup input by lines
var lines = line.split("\n");
var tokenizedLine, a, b, n, currentLineOutput,
currentOutput;
// Loop through each line
for (var i = 0; i < lines.length; ++i) {
// Split line up by space to extract the numbers out
// Per question statement, input are expected to be well formed.
tokenizedLine = lines[i].split(" ");
a = parseInt(tokenizedLine[0]);
b = parseInt(tokenizedLine[1]);
n = parseInt(tokenizedLine[2]);
// Array to store current line's output
currentLineOutput = [];
// Count from 1 to N
for (var j = 1; j <= n; ++j) {
currentOutput = "";
// Check if current number is divisible by a
if (j % a == 0) {
currentOutput += "F";
}
// Check if current number is divisible by b
if (j % b == 0) {
currentOutput += "B";
}
// If current output is not divisible by either a or b, print
// original number.
if (!currentOutput) {
currentOutput = j;
}
// Add current "count" to current line's output
currentLineOutput.push(currentOutput);
}
// Add current line's output to final output
output.push(currentLineOutput.join(" "));
}
//...