HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div class="container-fluid" id="page-container">
<div id="content-wrap">
<h1 class="text-center">Basic Algorithm Scripting</h1>
<h2 class="text-muted text-center">An Interactive View</h2>
<div class="row">
<div class="col-md-6">
<div id="test-describe">
<h4 id="descTitle" class="text-center">Choose a script from the dropdown →</h4>
<p id="descText">The script description will be displayed here telling you a little about it. Tests will follow showing sample input with expected output. You can input these values or a (valid) value of your own choosing.</p>
<h5 id="testsTitle">Test cases</h5>
<ul id="descTests">
<li>test 1</li>
<li>test 2</li>
<li>test n</li>
</ul>
</div>
</div>
<div class="col-md-6">
<div class="user-interact">
<form id="form" onsubmit="userInput(); return false" class="js-selector">
<select name="script" id="dropdown" class="dropdown">
<option value=" " selected>Select one</option>
<option value="cToF">Convert Celsius to Fahrenheit</option>
<option value="fToC">Convert Fahrenheit to Celsius</option>
<option value="revString">Reverse a String</option>
<option value="factorial">Factorialize a Number</option>
<option value="longest">Find Longest Word in a String</option>
<option value="ending">Confirm the Ending</option>
<option value="repeat">Repeat a String</option>
<option value="truncate">Truncate a String</option>
<option value="titleCase">Title Case a Sentence</option>
</select>
<div class="form-group column">
<input class="col-md-12 input-area" name="input" type="text" id="textbox" placeholder="" autocomplete="off">
<input class="col-md-6 input-area2" name="input" type="text" id="number" placeholder="" autocomplete="off">
</div>
<div class="submit">
<button id="submit" type="submit" class="btn btn-primary">Show Me</button>
</div>
</form>
</div>
<div id="output-area">
<p class="js-output"></p>
</div>
</div>
</div>
</div>
<div class="text-center" id="footer">
<a class="btn btn-info" href="https://www.github.com/sjroma/fccJavaScript" target="_blank"><i class="fab fa-github" aria-hidden="true"></i> GitHub</a>
<p class="copy">© 2019 SJRoma</p>
</div>
</div>
<script src="scripts/basicAS.js"></script>
* {
margin: 0;
padding: 0;
}
body {
background-color: #f9f9f9;
font-family: 'Open Sans', sans-serif;
}
#page-container {
position: relative;
min-height: 100vh;
}
#content-wrap {
padding-bottom: 5rem; /*footer height*/
}
.col-md-6 {
padding-top: 1.5em;
}
.dropdown {
border: 1px solid #333;
font-size: inherit;
height: 2em;
margin-bottom: 0.5em;
width: 16em;
}
.input-area {
border: 1px solid #333;
border-radius: 0.4em;
height: 2em;
margin-bottom: 0.25em;
padding-left: 0.5em;
}
.input-area2 {
border: 1px solid #333;
border-radius: 0.4em;
height: 0em;
padding-left: 0.5em;
padding-bottom: 1.4em;
visibility: hidden;
}
.btn-primary {
width: 6em;
}
.btn:hover {
background-color: #1d6fa3;
cursor: pointer;
transition: background-color 0.5s;
}
#output-area {
background-color: #fff;
border: 1px solid #333;
border-radius: 0.4em;
height: 2em;
margin-top: 0.5em;
padding-left: 0.5em;
}
#descTests {
padding-left: 1.5em;
}
#footer {
position: absolute;
bottom: 0;
height: 5rem; /*footer height*/
}
//repaint page based on user choice from dropdown selector
function dropdownChoice() {
"use strict";
document.getElementById("textbox").value = "";
document.getElementById("number").value = "";
document.getElementById("output-area").innerHTML = "";
document.getElementById("number").style.visibility = "hidden";
var script = document.getElementById("dropdown").value;
switch (script) {
case "cToF":
document.getElementById("descTitle").innerHTML = "Convert Celsius to Fahrenheit";
document.getElementById("descText").innerHTML = "The formula to convert from Celsius to Fahrenheit is; <code>F = (C x (9/5)) + 32</code>";
document.getElementById("descTests").innerHTML = "<li><var>-30</var> should return a value of <var>-22</var></li><li><var>-10</var> should return a value of <var>14</var></li><li><var>0</var> should return a value of <var>32</var></li><li><var>20</var> should return a value of <var>68</var></li><li><var>30</var> should return a value of <var>86</var></li>";
document.getElementById("textbox").placeholder = "-40";
break;
case "fToC":
document.getElementById("descTitle").innerHTML = "Convert Fahrenheit to Celsius";
document.getElementById("descText").innerHTML = "The formula to convert from Fahrenheit to Celsius is; <code>C = (F-32) * (5/9)</code><br> This wasn't a script in the course but being in the U.S. I have more reason to use this conversion.<br> One day I'll write a toggle so you don't have to choose from the dropdown.";
document.getElementById("descTests").innerHTML = "<li><i>-40</i> should return a value of <i>-40</i> (it's where the two scales cross)</li><li><i>32</i> should return a value of <i>0</i></li><li><i>68</i> should return a value of <i>20</i></li><li><i>212</i> (boiling point of water) should return a value of <i>100</i></li><li><i>451</i> (ignition temp of paper) should return a value of <i></i>233</li>";
document.getElementById("textbox").placeholder = "72";
break;
case "revString":
document.getElementById("descTitle").innerHTML = "Reverse a String";
document.getElementById("descText").innerHTML = "Reverse the provided string";
document.getElementById("descTests").innerHTML = "<li>\"hello\" should become \"olleh\"</li><li>\"Howdy\" should become \"ydwoH\"</li><li>\"Greetings from Earth\" should become \"htraE morf sgniteerG\"</li>";
document.getElementById("textbox").placeholder = "stressed";
break;
case "factorial":
document.getElementById("descTitle").innerHTML = "Factorialize a Number";
document.getElementById("descText").innerHTML = "Return the factorial of the provided integer. If the integer is represented with the letter n, a factorial is the product of all positive integers less than or equal to n. Factorials are often represented with the shorthand notation n!<br> For example: 5! = 1 x 2 x 3 x 4 x 5 = 120<br> Only integers greater than or equal to zero will be supplied to the function.";
document.getElementById("descTests").innerHTML = "<li><var>5</var> should return <var>120</var><li><var>10</var> should return <var>3628800</var><li><var>20</var> should return <var>2432902008176640000</var><li><var>0</var> should return <var>1</var>";
document.getElementById("textbox").placeholder = "11";
break;
case "longest":
document.getElementById("descTitle").innerHTML = "Find the Longest Word in a String";
document.getElementById("descText").innerHTML = "Return the length of the longest word in the provided sentence.<br> The response should be a number";
document.getElementById("descTests").innerHTML = "<li>\"The quick brown fox jumped over the lazy sleeping dog\" should return 8</li><li>\"May the force be with you\" should return 5</li><li>\"Google do a barrel roll\" should return 6</li><li>\"What is the average airspeed velocity of an unladen swallow\" should return 8</li><li>\"What if we try a super-long word such as otorhinolaryngology\" should return 19</li>";
document.getElementById("textbox").placeholder = "May the Fourth is Star Wars day";
break;
case "ending":
document.getElementById("descTitle").innerHTML = "Confirm the Ending";
document.getElementById("descText").innerHTML = "- Check if a string (first argument, <code>str</code>) ends with the given target string (second argument, <code>target</code>).<br> - This challenge <em>can</em> be solved with the <code>.endsWith()</code> method, which was introduced in ES2015. But for the purpose of this challenge, we would like you to use one of the JavaScript substring methods instead.";
document.getElementById("descTests").innerHTML = "<li>\"Bastian\", \"n\" should return true</li><li>\"Congratulation\", \"on\" should return true</li><li>\"Connor\", \"n\" should return false</li><li>\"Walking on water and developing software from a specification are easy if both are frozen\", \"specification\" should return false</li><li>\"He has to give me a new name\", \"name\" should return true</li><li>\"Open sesame\", \"same\" should return true</li><li>\"Open sesame\", \"pen\" should return false</li><li>\"Open sesame\", \"game\" should return false</li><li>\"If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing\", \"mountain\" should return false</li><li>\"Abstraction\", \"action\" should return true</li>";
document.getElementById("textbox").placeholder = "bicycle";
document.getElementById("number").style.visibility = "visible";
document.getElementById("number").placeholder = "cycle";
break;
case "repeat":
document.getElementById("descTitle").innerHTML = "Repeat a String Repeat a String";
document.getElementById("descText").innerHTML = "Repeat a given string <code>str</code> (first argument) for <code>num</code> times (second argument). Return an empty string if <code>num</code> is not a positive number.<br> The built-in <code>repeat()</code> method should not be used";
document.getElementById("descTests").innerHTML = "<li>\"*\", 3 should return \"***\"</li><li>\"abc\", 3 should return \"abcabcabc\"</li><li>\"abc\", 4 should return \"abcabcabcabc\"</li><li>\"abc\", 1 should return \"abc\"</li><li>\"*\", 8 should return \"********\"</li><li>\"abc\", -2 should return \" \"</li>";
document.getElementById("textbox").placeholder = "tick-tock";
document.getElementById("number").style.visibility = "visible";
document.getElementById("number").placeholder = "2";
break;
case "truncate":
document.getElementById("descTitle").innerHTML = "Truncate a String";
document.getElementById("descText").innerHTML = "Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a <code>...</code> ending.";
document.getElementById("descTests").innerHTML = "<li>\"A-tisket a-tasket A green and yellow basket\", 8 should return \"A-tisket...\"</li><li>\"Peter Piper picked a peck of pickled peppers\", 11 should return \"Peter Piper...\"</li><li><s>\"A-tisket a-tasket A green and yellow basket\", \"A-tisket a-tasket A green and yellow basket\".length should return \"A-tisket a-tasket A green and yellow basket\"</s> Don't have a way to test this...yet</li><li><s>\"A-tisket a-tasket A green and yellow basket\", \"A-tisket a-tasket A green and yellow basket\".length + 2 should return \"A-tisket a-tasket A green and yellow basket\"</s> Don't have a way to test this...yet</li><li>\"A-\", 1 should return \"A...\"</li><li>\"Absolutely Longer\", 2 should return \"Ab...\"</li>";
document.getElementById("textbox").placeholder = "GitHub cheat sheet";
document.getElementById("number").style.visibility = "visible";
document.getElementById("number").placeholder = "6";
break;
case "titleCase":
document.getElementById("descTitle").innerHTML = "Title Case a Sentence";
document.getElementById("descText").innerHTML = "Return the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lower case.<br> For the purpose of this exercise, you should also capitalize connecting words like 'the' and 'of'.";
document.getElementById("descTests").innerHTML = "<li>\"I'm a little tea pot\" should return a string</li><li>\"I'm a little tea pot\" should return \"I'm A Little Tea Pot\"</li><li>\"sHoRt AnD sToUt\" should return \"Short And Stout\"</li><li>\"HERE IS MY HANDLE HERE IS MY SPOUT\" should return \"Here Is My Handle Here Is My Spout\"</li>";
document.getElementById("textbox").placeholder = "wHo TyPeS LiKe ThIs?";
break;
default:
//do nothing;
}
}
//end page setup
//Basic Algorithm Scripts
//convert celsius to fahrenheit
function convertToF(celsius) {
var fahrenheit;
const tempFahrenheit = (celsius * (9 / 5) + 32);
fahrenheit = tempFahrenheit.toFixed(1);
return fahrenheit;
}
//convert from fahrenheit to celsius
function convertToC(fahrenheit) {
var celsius;
const tempCelsius = ((fahrenheit - 32) * (5/9));
celsius = tempCelsius.toFixed(1);
return celsius;
}
//reverse a string
function reverseString(str) {
var splitString = str.split("");
var reverseArray = splitString.reverse();
var joinArray = reverseArray.join("");
return joinArray;
}
//factorialize a positive integer
function factorialize(num) {
var answer = 0;
if (num == 0) {
answer = 1;
} else {
answer = num * factorialize(num - 1);
}
return answer;
}
//find the longest word in a string
function findLongestWordLength(str) {
var eachWord = str.split(" ");
var longestWord = 0;
// console.log("Split words are ",eachWord);
for (var i = 0; i < eachWord.length; i++) {
if (eachWord[i].length > longestWord) {
longestWord = eachWord[i].length;
}
}
return longestWord;
}
//confirm the ending is the same
function confirmEnding(str, target) {
var doesIt = "";
doesIt = str.indexOf(target, str.length - target.length) !== -1;
return doesIt;
}
//repeat a string n number of times
function repeatStringNumTimes(str, num) {
if (num <= 0) {
return "";
} else if (num === 1) {
return str;
} else {
str = str + repeatStringNumTimes(str, num - 1);
}
return str;
}
//truncate a string
function truncateString(str, num) {
if (str.length > num) {
return str.substring(0, num) + '...';
} else {
return str;
}
}
//uppercase the first letter of each word in a sentence, all other letters lowercase
function titleCase(str) {
let words = str.toLowerCase().split(' ');
let capitalWords = [];
for (let i = 0; i < words.length; i++) {
capitalWords.push(words[i].charAt(0).toUpperCase() + words[i].slice(1));
}
return capitalWords.join(' ');
}
//End scripts
//populate the output area
function userInput(script, str) {
"use strict";
script = document.getElementById("dropdown").value;
let userInput = document.getElementById("textbox").value;
let userInput2 = document.getElementById("number").value;
var output = '';
switch (script) {
case ("cToF"):
var faren = convertToF(userInput);
output = `${userInput} °C equals ${faren} °F`;
console.log('output=', output);
break;
case ("fToC"):
var celsius = convertToC(userInput);
output = `${userInput} °F equals ${celsius} °C`;
console.log('output=', output);
break;
case ("revString"):
output = reverseString(userInput);
console.log('output=', output);
break;
case ("factorial"):
if (userInput <0) {
output = "Input must be 0 or greater";
} else {
var factorialNum = factorialize(userInput);
output = `${userInput}! = ${factorialNum}`;
console.log('output=', output);
}
break;
case ("longest"):
var longestLength = findLongestWordLength(userInput);
output = `The longest word is ${longestLength} characters`;
console.log('output=', output);
break;
case ("ending"):
output = confirmEnding(userInput, userInput2);
console.log('output=', output);
break;
case ("repeat"):
if (isNaN(userInput2)) {
output = "The second argument must be an integer";
} else {
output = repeatStringNumTimes(userInput, userInput2);
console.log('output=', output);
}
break;
case ("truncate"):
output = truncateString(userInput, userInput2);
console.log('output=', output);
break;
case ("titleCase"):
output = titleCase(userInput);
console.log('output=', output);
break;
default:
//do nothing;
}
document.getElementById("output-area").innerHTML = output;
}
document.getElementById("dropdown").onchange = function() {dropdownChoice()};
Also see: Tab Triggers