JavaScript Countdown program - javascript

im trying to write a program in JavaScript that fulfils this prompt
Write a JavaScript function
countDown(i)
that takes an integer argument and returns a \countdown" from i to 0, with a space appearing between
each number.
For example, countDown(5) should return the string "5 4 3 2 1 0". As for the rst problem, you may
want to test your solution on the computer.
sofar I have this
var i= "";
function countdown(i)
{
while( i > 0)
{
console.log(integer);
i--;
}
}
countdown();
can someone please help me im very new to programing

Hopefully this makes enough sense:
function countdown(i) {
//initialize the variable to be returned with the initial i
var ret = i;
//in each iteration, assigns i to i-1 then checks if i >= 0
while (--i >= 0) {
//concatenates a space and the current i value to the return string
ret += ' ' + i;
}
//returns the string
return ret;
}
Fiddle

I hope you read the comments I've put in the code and learn.
// you write comments in JavaScript with two forward slashes
// i is the integer parameter of your countdown function
// i is passed to countdown when called, i.e. countdown(9)
function countdown(i)
{
// this is an ret string variable that is private to the countdown function
// you can't access ret from outside of this function
var ret = "";
// your loop should include 0 according to your requirements
while( i >= 0)
{
// here you are appending i to your ret string which you'll return at the end of this function
ret += i;// += is a short hand form of saying ret = ret + i
// you want to append an empty space for every i except the last one (0)
if(i > 0) {
ret += " ";
}
i--; // here you are decrementing i
}
return ret;
}
// here you are making the actual call to the function with integer 5
// you are assigning the returned value of your function call to result variable
var result = countdown(5);
// here you are printing your result string variable to the log
console.log(result);
Here another solution (bit more advanced) using recursion, alternative to for/while loops where a function calls itself:
// here is an implementation using recursion
function countdown(i)
{
if(i<=0)
return i;
return i + " " + countdown(--i);
}

Here is the answer:
function countdown(i) {
answer = '';
while( i >= 0) {
answer = answer + i.toString() + ' ';
i--;
}
return answer;
}
countdown(5);

Here's a way to do it using recursion:
//define countdown function
var countdown = function countdown(i) {
//loop while the passed in parameter "i" is >= 0
while (i >= 0) {
//return a space concatenated with the value of i
//and call the countdown function again (by
//concatenating the result) to continue counting down
return ' ' + i + countdown(i -= 1);
}
return ''; //out of loop, return an empty string
};
console.log(countdown(5));

You want to include 0 in your countdown, so you want
while (i >= 0)
as opposed to while (i > 0), which excludes 0 at the end.
Also, as some of the comments mentioned, var i = "" defines i to be a string, so you cannot perform operations like i--. You should define i to be an integer, e.g. var i = 5.

recursive way ;)
​var countdown=function(i) {
    console.log(i);
    i>0 && countdown(i-1);
}
    countdown(10);

Related

Undefined in Split String

i have a function to split string into 2 part, front and back. Then reverse it to back and front. Here is my code
function reverseString(string) {
let splitString = ""
let firstString = ""
for(i = 0; i <= string.length/2 - 1; i++) {
firstString += string[i]
}
for(i = string.length/2; i <= string.length; i++) {
splitString += string[i]
}
return splitString + firstString
}
Sorry for bad explanation, this is test case and expected result (first one is expected result, the second one is my result)
console.log(reverseString("aaabccc")); // "cccbaaa" "undefinedundefinedundefinedundefinedaaa"
console.log(reverseString("aab")); // "baa" "undefinedundefineda"
console.log(reverseString("aaaacccc")); // "ccccaaaa" "ccccundefinedaaa"
console.log(reverseString("abcdefghabcdef")); // "habcdefabcdefg" "habcdefundefinedabcdefg"
could you help me, whats wrong with it. Thank you
You could try another approach and use the slice function
function reverseString(string)
{
if (string.length < 2) { return string; }
let stringHalfLength = string.length / 2;
let isLengthOdd = stringHalfLength % 1 !== 0;
if (isLengthOdd) {
return string.slice(Math.ceil(stringHalfLength), string.length + 1) + string[Math.floor(stringHalfLength)] + string.slice(0, Math.floor(stringHalfLength));
}
return string.slice(stringHalfLength, string.length + 1) + string.slice(0, stringHalfLength);
}
console.log(reverseString("aaabccc") === "cccbaaa");
console.log(reverseString("aab") === "baa");
console.log(reverseString("aaaacccc") === "ccccaaaa");
console.log(reverseString("abcdefghabcdef") === "habcdefabcdefg");
A more efficient way to reverse the string would be to split the string, then use the built-in reverse javascript function (which reverses the elements of the split string), and then re-join the elements using the join function.. No need to re-invent the wheel?
You can concatenate the functions in shorthand (.split.reverse.join etc...) so your function would look something like this:
function reverseString(string) {
return string.split("").reverse().join("");
}
Try it out!
function reverseString(string) {
return string.split("").reverse().join("");
}
console.log(reverseString("hello"));
console.log(reverseString("aaabbbccc"));
If there's a particular reason you're opting not to use the in-built functions (i.e. if I've missed something?) , feel free to comment.
The short version of what you need:
function reverseString(string) {
const splitPosition = Math.ceil(string.length / 2);
return string.substring(splitPosition) + string.substring(0, splitPosition);
}
The key to your question is the middle element. To accomplish that, you probably want to use Math.floor that round under.
console.log(reverseString("aaabccc")); // "cccbaaa"
console.log(reverseString("abcdefghabcdef")); // "habcdefabcdefg"
function reverseString (str) {
if (str.length<2) {
return str
}
var half = Math.floor(str.length / 2);
return (str.slice(-half) + (str.length%2?str[half]:"") + str.slice(0,half));
}
reverseString('')
> ""
reverseString('1')
> "1"
reverseString('12')
> "21"
reverseString('123')
> "321"
reverseString('1234')
> "3412"
reverseString('12345')
> "45312"
reverseString("aaabccc")
> "cccbaaa"
reverseString("abcdefghabcdef")
> "habcdefabcdefg"
So basically your problem is not to grab 2 parts of the string and rearrange, it is to grab 3 parts.
1 part: str.slice(0,half)
2 part: str.length%2 ? str[half] : ""
3 part: str.slice(-half)
The second part is empty if the string length is even and the middle character if is odd.
So the code version in long self explanatory code:
function reverseString (str) {
if (str.length<2) {
return str
}
var half = Math.floor(str.length / 2);
var firstPart = str.slice(0,half);
var midlePart = str.length % 2 ? str[half] : ""; // we could expand also this
var endPart = str.slice(-half);
return endPart + midlePart + firstPart;
}
And also, notice the precondition, so I don't have to deal with the easy cases.
Also, in your code, you got undefined because you access in the last loop to:
string[string.length] you need to change <= by <

How should I fix this asynchronicity problem in my "Josephus Problem" code?

Background
I'm new to JavaScript and am solving various formulations of the Josephus Problem to better understand the syntax. Using a circularLinkedList implementation*, I've solved the classic formulation: Wikipedia||Numberphile. I've also solved the problem for any fixed number of fighters and a fixed number of skips between eliminations (e.g., if skipping two fighters between eliminations, 1 eliminates 4, 5 eliminates 8, etc). I am now trying to solve the problem given any function that indicates the number of skips at a given moment.
Problem
I can't access the return value of my skip function. I understand from 1, 2, 3 that my issue involves asynchronicity, but am having trouble isolating takeaways from the long responses involving AJAX and jQuery, which I'm unfamiliar with. Could I get an ELI5 solution? I apologize for my lack of understanding.
Code
function winnerStepFunc(num, func) {
let cll = new circularLinkedList(); //Initializing list with participants
for (let a = 0; a < num; a++) {
cll.append(a);
}
function next(funcSteps) { //Generating string indicating #steps from function's output
let toEvaluate = "start";
for (let i = 0; i < funcSteps; i++) {
toEvaluate += ".next"
}
return toEvaluate;
}
let start = cll.getHead(); //Selecting first eliminator
while (cll.size() > 1) {
let toCheck = func(); // PROBLEM LINE
console.log("toCheck = " + toCheck); //toCheck = undefined
let str = next(toCheck);
while (eval(str) !== start && cll.size() > 1) { //
let locCurrent = cll.indexOf(start.element);
start = eval(str).next;
cll.removeAt(((locCurrent + toCheck)% cll.size()));
}
cll.delete(eval(str).next.element);
start = start.next;
}
console.log(start.element + 1);
}
function callFunction(name, args) { // builds function string to be evaluated
let result = name + "(";
for (let i = 0; i < args.length -1; i++) {
result += args[i] + ", ";
}
result += args[args.length-1] + ")";
return result;
}
function callFunction(name) {
let result = `${name}()`;
return result;
}
function addOne() { //<--The first basic example I'm trying:
return ++globalTimes; //Make the step increase by one for each elimination
}
var globalTimes = 0;
winnerStepFunc(12, function(){eval(callFunction("addOne"))});
*CLL Implementation
You don't return in your function. I would remove all the eval stuff and just call the function directly.
winnerStepFunc(12, addOne);

How do I run a function over and over by using parameters?

My case:
function randomLetter(){
var random = letter[Math.floor(Math.random()*26)];
return random;
}
function randomWord(wordLength){
var random = randomLetter() + randomLetter() + randomLetter();
return random;
}
How do I write a code that run the randomLetter() function x times using parametes.
Example: I write 3 in the parameter, and the function will give me three random letters.
So instead of writing randomLetter() + randomLetter() + randomLetter(), I will just write randomWord(3), and I will get three random letters.
Another approach, which buffers each letter into an array and returns the joined array.
function randomWord(wordLength){
var letters = [];
for (var i = 0; i < wordLength; i++) {
letters.push(randomLetter());
}
return letters.join("");
}
Or recursion:
var letters = "abcdefghijklmnopqrstuvwxyz"
function randomLetter() {
return letters.charAt(Math.floor((Math.random() * 100)) % letters.length)
}
function getLetters(count) {
if (count-- < 1) return "";
return getLetters(count) + randomLetter() + ","
}
document.getElementById("output").innerText = getLetters(4)
<div id="output" />
For this you could use a for loop like:
function randomWord(wordLength){
var random =''
for (var i = 0, i<wordLength, i++) {
random += randomLetter();
}
return random;
}
the first parameter in the parentheses after the 'for' keyword initializes the i variable with 0. The next value i<wordLength is the stop condition, which will test at the beginning of each run if the condition is still true, otherwise it will stop looping. The third i++ is what runs every time a loop finishes, in this case it increments i by one, which is identical to i = i + 1.
here is some more information: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration
You can use a for-loop:
function randomWord(x){
var random = [];
for(var a = 0; a < x; a++){
random[a] = randomLetter();
}
return random.join("");
}
Yet another recursive solution:
function randomLetter() {
return ('qwertyuiopasdfghjklzxcvbnm')[Math.floor(Math.random()*26)];
}
function randomWord(wordLength) {
return (wordLength > 0) ? (randomWord(wordLength - 1) + randomLetter()) : '';
}
console.log( randomWord(10) );

How do I finish this loop? Concatenate two previous characters

I'm making a function that when you give it a string, it will return the uppercase of the even numbered chars and the lower case of the odd numbered ones. So if you give it "HELLO" it will give you "HeLlO". This is obviously wrong since it only works for the first two characters. How do I complete the loop so that it doesn't keep concatenating the strings together?
function evenOddChange(source)
{
var i;
var result;
i = 0;
result = "";
while ( i < (source.length))
{
result = result + source.toUpperCase().charAt(i) + source.toLowerCase().charAt(i + 1);
i = i + 1;
}
return result;
}
You can usemodulo to check if its even or Odd.
function evenOddChange(source)
{
var i;
var result;
i = 0;
result = "";
while ( i < (source.length))
{
if(i%2==0){
result = result + source.toUpperCase().charAt(i);
}else{
result = result + source.toLowerCase().charAt(i);
}
i = i + 1;
}
return result;
}
Just use modulus to find if the index is odd or even and then use bracket notation.
function evenOddChange(source) {
var i = 0, result = "";
while (i < source.length)
result += source[i][i++ % 2 == 0 ? "toUpperCase" : "toLowerCase"]();
return result;
}
Note that when we are doing i++ % 2, the modulus operator operates on the value of i before we increment it. The increment effect will be felt only in the next iteration.
Increment by 2, not 1:
i = i + 2;
Not you are changing i and i+1, incrementing by only one will overwrite the i+1 change. This simple change will fix your problem.

JQuery/JavaScript increment number

I am trying to increment a number by a given value each second and retain the formatting using JavaScript or JQuery
I am struggling to do it.
Say I have a number like so:
1412015
the number which this can be incremented by each second is variable it could be anything beween 0.1 and 2.
Is it possible, if the value which it has to be incremented by each second is 0.54 to incremenet the number and have the following output:
1,412,016
1,412,017
1,412,018
Thanks
Eef
I'm not quite sure I understand your incrementation case and what you want to show.
However, I decided to chime in on a solution to format a number.
I've got two versions of a number format routine, one which parses an array, and one which formats with a regular expression. I'll admit they aren't the easiest to read, but I had fun coming up with the approach.
I've tried to describe the lines with comments in case you're curious
Array parsing version:
function formatNum(num) {
//Convert a formatted number to a normal number and split off any
//decimal places if they exist
var parts = String( num ).replace(/[^\d.]-/g,'').split('.');
//turn the string into a character array and reverse
var arr = parts[0].split('').reverse();
//initialize the return value
var str = '';
//As long as the array still has data to process (arr.length is
//anything but 0)
//Use a for loop so that it keeps count of the characters for me
for( var i = 0; arr.length; i++ ) {
//every 4th character that isn't a minus sign add a comma before
//we add the character
if( i && i%3 == 0 && arr[0] != '-' ) {
str = ',' + str ;
}
//add the character to the result
str = arr.shift() + str ;
}
//return the final result appending the previously split decimal place
//if necessary
return str + ( parts[1] ? '.'+parts[1] : '' );
}
Regular Expression version:
function formatNum(num) {
//Turn a formatted number into a normal number and separate the
//decimal places
var parts = String( num ).replace(/[^\d.]-/g,'').split('.');
//reverse the string
var str = parts[0].split('').reverse().join('');
//initialize the return value
var retVal = '';
//This gets complicated. As long as the previous result of the regular
//expression replace is NOT the same as the current replacement,
//keep replacing and adding commas.
while( retVal != (str = str.replace(/(\d{3})(\d{1,3})/,'$1,$2')) ) {
retVal = str;
}
//If there were decimal points return them back with the reversed string
if( parts[1] ) {
return retVal.split('').reverse().join('') + '.' + parts[1];
}
//return the reversed string
return retVal.split('').reverse().join('');
}
Assuming you want to output a formatted number every second incremented by 0.54 you could use an interval to do your incrementation and outputting.
Super Short Firefox with Firebug only example:
var num = 1412015;
setInterval(function(){
//Your 0.54 value... why? I don't know... but I'll run with it.
num += 0.54;
console.log( formatNum( num ) );
},1000);
You can see it all in action here: http://jsbin.com/opoze
To increment a value on every second use this structure:
var number = 0; // put your initial value here
function incrementNumber () {
number += 1; // you can increment by anything you like here
}
// this will run incrementNumber() every second (interval is in ms)
setInterval(incrementNumber, 1000);
This will format numbers for you:
function formatNumber(num) {
num = String(num);
if (num.length <= 3) {
return num;
} else {
var last3nums = num.substring(num.length - 3, num.length);
var remindingPart = num.substring(0, num.length - 3);
return formatNumber(remindingPart) + ',' + last3nums;
}
}
function rounded_inc(x, n) {
return x + Math.ceil(n);
}
var x = 1412015;
x = rounded_inc(x, 0.54);

Categories

Resources