(Ik Its a stupid question) with the "do - while" the code runs multiple times at the same time (i putted a console.log('while') at the beginning, so it print 'while' when the code is executed) but I want to run it one time at a time. Here's an example:
Code:
let x = 100
let y = 100
do {
console.log('while')
/*
here there's a piece of code that let the user choose what want he to do
*/
let random = Math.floor(//random number between 1 and 2)
if (random == 1) {
y = y - 50
} else {
x = x - 50
} while (y >= 0 && x >= 0 )
Obviously the console prints 'while' until the process ends.
I tried to use for (let i; i < 100; i++) and in the for i putted a if ( y <= 0 && x <= 0) but I can't start-over the loop (I dont know how to use continue).
The main problem with your code is the brackets - your formatting is incorrect:
let x = 100
let y = 100
do {
//...
if (random == 1) {
y = y - 50
} else {
x = x - 50
} while (y >= 0 && x >= 0 );
You don't close off your "do" loop - you just go right into the while after the else. In order for your code to work properly, you need to insert a bracket after the else:
let x = 100
let y = 100
do {
//...
if (random == 1) {
y = y - 50
} else {
x = x - 50
}
} while (y >= 0 && x >= 0 );
Thinking it would be like
for number from range x,y
if number in ones or number in hundreds
print
else
??? I don't know what command to do the if statement.
You may try using the modulus operator here:
for (i=100; i <= 200; ++i) {
if (i % 10 == 2 || Math.floor(i / 10) % 10 == 3) {
console.log(i);
}
else {
// turned this off for demo purposes
// console.log("???");
}
}
for (let i = 100; i <= 200; i++) {
if (i % 10 === 2 || (i / 10) % 10 === 3) {
// do something
} else {
console.log('???')
}
}
You could store the tensDigit and onesDigit in variables and then check them in the if statement for better readabilty:
for (let num = 100; num <= 200; num++) {
const tensDigit = Math.floor((num % 100) / 10);
const onesDigit = num % 10;
if (tensDigit === 3 || onesDigit === 2) {
console.log(num);
}
}
your question is super ambiguous, please be more specific. That being said this function should do what you are asking.
function printIfDigitIsInPlace(INPUT, DIGIT, PLACE) {
const arr = Array.from(INPUT.toString());
if (arr[PLACE] === DIGIT.toString()) console.log(INPUT);
}
the code should be able to handle any amount up to 20000, For example, suppose the Entered amount is 2600 when the balance in the card is 3000. Will output following :
New Balance - 400
Notes:
2000 * 1
500 * 1
100 * 1
(only three banknotes 2000, 500, 100) and the cash limit is 20000
I am new in the javascript world, and I am not able to write the code, could anyone help me out??? please!
var h = 5;
var f = 2;
var t = 1;
var ifAmtLessThn2000 = ifAmtLessThn2000(n) {
var temp;
if (n < 500) {
h += (n / 100);
return {
h
}
} else if (n >= 500 && n < 2000) {
f += n / 500;
h += (n - 500) / 100;
return {
h,
f
}
} else {
temp = n - 1500;
if (temp < 500) {
h += (temp / 100);
return {
h
}
console.log('hundred : ' + h);
} else {
f += 1;
h += (temp - 500) / 100;
console.log('five hundred : ' + f);
console.log('hundred : ' + h);
return {
f,
h
}
}
}
}
var ifAmtGreaterthan2000 = (n) => {
var h = 0;
var f = 0;
var t = 0;
var tt = 0;
var temp;
if (n < 2000) {
tt += (n / 2000);
}
else if (n >= 2000 && n < 10000) {
f += n / 500;
h += (n - 500) / 100;
}
else {
temp = n - 1500;
if (temp < 500) {
h += (temp / 100);
}
else {
f += 1;
h += (temp - 500) / 100;
}
}
}
var checkAmt = (n) => {
if (n < 100 || (n % 100) > 0) {
console.log("Invalid Amount : less than 100 ");
} else {
if (n > 20000) {
console.log("ATM Cash Limit exceeds.");
} else {
if (n <= 2500) {
ifAmtLessThn2500(n);
console.log(h + " x 100");
console.log(f + " x 500");
} else {
temp = n - 2500;
t += temp / 1000;
if (temp > 500)
temp = temp - (1000 * (t - 1));
ifAmtLessThn2500(temp);
console.log(h + " x 100");
console.log(f + " x 500");
console.log(t + " x 1000");
}
}
}
}
checkAmt(2500);
Sorry for a dumb program, but I need help please can anyone give me a solution in typeScript code, returning the req denomination in array!!
const withdraw = (amount) => {
let hundredNotes = 0;
let fiftyNotes = 0;
let twentyNotes = 0;
while (amount >= 20) {
if (
amount >= 100 &&
((amount % 100) % 50 === 0 || (amount % 100) % 20 === 0)
) {
amount -= 100;
hundredNotes++;
} else if (
amount >= 50 &&
((amount % 50) % 100 === 0 || (amount % 50) % 20 === 0)
) {
amount -= 50;
fiftyNotes++;
} else {
amount -= 20;
twentyNotes++;
}
}
return [hundredNotes, fiftyNotes, twentyNotes];
};
console.log(withdraw(230));
console.log(withdraw(250));
amtArray = [2000, 500, 100]; // the denomination you want to find.
for (let i = 0; i < this.amtArray.length; i++) {
this.resultArray.push(Math.floor(total / this.amtArray[i]));
// Get the new total
total = total % this.amtArray[i];
}
var twothousands_notes = this.resultArray[0];
var fivehundred_notes = this.resultArray[1];
var hundred_notes = this.resultArray[2];
console.log('calculated amt : ' + '100 : ' +
hundred_notes + ' 500 : ' +
fivehundred_notes + ' 2000 : ' +
twothousands_notes);
Based on the amount you can adjust the logic.
Hope this helps.. :)
this would cover all your cases
function dispenseCase (inputAmount) {
var notes = [];
var balance = 3000;
if(inputAmount !== 0 && inputAmount % 100 == 0 && inputAmount <= balance) {
var notes2000 = Math.round(inputAmount / 2000);
var notes500 = Math.round((inputAmount - (notes2000 * 2000)) / 500 );
var notes100 = Math.round((inputAmount - ((notes2000 * 2000) + (notes500 * 500))) / 100);
notes.push(notes2000);
notes.push(notes500);
notes.push(notes100);
console.log("balance in you account = ", balance - inputAmount);
console.log(notes);
}
else if (inputAmount > balance) {
console.log("Insufficient balance in your account");
}
else if ( inputAmount % 100 != 0 || inputAmount < 100 ) {
console.log( "Invalid amount entered, amount should be multiples of 100");
}
}
dispenseCase(2600);
ATM denomination program in Javascript.
Here, It'll find the minimum number of notes of different denominations that sum the entered amount. Starting from the highest denomination note to the lowest notes.
function countCurrency(amount) {
var notes = [2000, 500, 200, 100];
var noteCounter = [0, 0, 0, 0];
for (var i = 0; i < 4; i++) {
if (amount >= notes[i]) {
noteCounter[i] = Math.floor(amount / notes[i]);
amount = amount - noteCounter[i] * notes[i];
}
}
// Print notes denomination
console.log("Denomination Count:");
for (var j = 0; j < 4; j++) {
if (noteCounter[j] !== 0) {
console.log(notes[j] + " : " + noteCounter[j]);
}
}
}
countCurrency(3300);
Here is the working example
https://codesandbox.io/s/atm-denomination-javascript-o0wb4?file=/src/index.js
this would print the number of notes in a 2000, 500, 100 order for the amount you enter
function dispenseCase (inputAmount) {
var notes = [];
if(inputAmount !== 0) {
var notes2000 = Math.round(inputAmount / 2000);
var notes500 = Math.round((inputAmount - (notes2000 * 2000)) / 500 );
var notes100 = Math.round((inputAmount - ((notes2000 * 2000) + (notes500 * 500))) / 100);
notes.push(notes2000);
notes.push(notes500);
notes.push(notes100);
console.log(notes);
}
}
dispenseCase(2600);
hope this helps
//ATM Cash Denominations //Cash Input Value Already been Provided in this method // You may use a input stream method to input a user input value
public class Denominations
{
public static void main(String args[])//throws IOException
{
int notes[]={5000,2000,1000,500,100}; //storing all the denominations in an array
int amount = 27000;
int copy=amount; //Making a copy of the amount
int totalNotes=0,count=0;
System.out.println("\nATM CASH DENOMINATIONS: \n");
for(int i=0;i<5;i++) //Since there are 5 different types of notes, hence we check for each note.
{
count=amount/notes[i]; // counting number of notes[i] notes
if(count!=0) //printing that denomination if the count is not zero
{
System.out.println(notes[i]+"\tx\t"+count+"\t= "+notes[i]*count);
}
totalNotes=totalNotes+count; //finding the total number of notes
amount=amount%notes[i]; //finding the remaining amount whose denomination is to be found
}
System.out.println("--------------------------------");
System.out.println("TOTAL\t\t\t= "+copy); //printing the total amount
System.out.println("--------------------------------");
System.out.println("Total Number of Notes\t= "+totalNotes); //printing the total number of notes
}
}
let sumToDenominate=Math.floor(Math.random() * 100);
let billsValues = [100, 50, 20, 10, 5,1];
let restAfterDenomination = [];
let billsNumber = [];
function denomination(sum, billsValues) {
printInitialValue( sumToDenominate, billsValues);
initializeArray( sumToDenominate, billsValues);
for (let i = 1; i <= billsValues.length; i++) {
if (restAfterDenomination[i - 1] > 0 || restAfterDenomination < billsNumber[i]) {
billsNumber.push(Math.floor(restAfterDenomination[i - 1] / billsValues[i]));
console.log(`bill's number of `, billsValues[i], "=>", billsNumber[i]);
restAfterDenomination.push(restAfterDenomination[i - 1] - (billsNumber[i] * billsValues[i]));
} else {
console.log(`rest is less than smallest bill or equal to 0`);
billsNumber.push(0);
// break;
}
}
}
function printInitialValue(amount, billsValue) {
console.log("Denomination sumToDenominate: ", amount);
console.log("____________");
for (const logEntry of billsValue) {
console.log(logEntry);
}
console.log("__________");
}
function initializeArray(amount, billsValues) {
billsNumber.push(Math.floor(amount / billsValues[0]));
console.log(`bill's number of`, billsValues[0], "=>", billsNumber[0]);
restAfterDenomination.push(amount - (billsNumber[0] * billsValues[0]));
denomination(sumToDenominate,billsValues);
added a while loop, and trying to end the loop by entering finish inside the loop. The game is still running after it the game is completed.
var number = Math.floor(Math.random() * 100) + 1; // generate random #
var guess;
var guessCount = 0;
var correctGuess = false;
var diff;
var correct = false;
while (!correct) {
correct = guessFunction();
var finish = false;
}
function guessFunction(){
guess = prompt('I am thinking of a number between 1 and 100. What is it?');
guessCount += 1;
var guessInt = parseInt(guess); //turn guesses into interger
if (guess == number){ //which number is bigger, guess or number
alert('Correct!' + 'That was a total of ' + guessCount + ' guesses.');
correctGuess = true;
finish = true;
}
else if (guessInt > number) {
diff = guessInt - number;
}
else if (guessInt < number) {
diff = number - guessInt;
}
if (diff >= 1 && diff <= 10 && !correctGuess) {
alert('Very Hot');
guessFunction();
}
else if (diff < 10 && diff <= 20 && !correctGuess){
alert('Hot');
guessFunction();
}
else if (diff < 20 && diff <= 30 && !correctGuess){
alert('Warm');
guessFunction();
}
else if (diff < 30 && diff <= 50 && !correctGuess){
alert('Cold');
guessFunction();
}
else if (diff > 50 && !correctGuess){
alert('Ice Cold');
guessFunction();
}
}
guessFunction();
Trying to get this code to run but it only allows for 2 alert windows when guessing the random number. Im not sure how to get this to run, perhaps the guessFunction is not running?
var number = Math.floor(Math.random() * 100) + 1; // generate random #
var guess;
var guessCount = 0;
var correctGuess = false;
var diff;
function guessFunction(){
guess = prompt('I am thinking of a number between 1 and 100. What is it?');
guessCount += 1;
var guessInt = parseInt(guess); //turn guesses into interger
if (guess == number){ //which number is bigger, guess or number
alert('Correct!');
}
else if (guessInt > number) {
diff = guessInt - number;
}
else if (guessInt < number) {
diff = number - guessInt;
}
}
guessFunction();
if (diff >= 1 && diff <= 10) {
alert('Very Hot');
guessFunction();
}
else if (diff < 10 && diff <= 20){
alert('Hot');
guessFunction();
}
else if (diff < 20 && diff <= 30){
alert('Warm');
guessFunction();
}
else if (diff < 30 && diff <= 50){
alert('Cold');
guessFunction();
}
else if ( diff > 50){
alert('Ice Cold');
guessFunction();
}
The script stops executing because you only call your function twice. If you want this to run until the user guesses the right number, you probably want a while loop:
var correct = false;
while (!correct) {
// guessFunction could return true if they get it right
correct = guessFunction();
}
Yes, your script is only executing twice before exiting. You've got your nesting wrong:
var number = Math.floor(Math.random() * 100) + 1; // generate random #
var guess;
var guessCount = 0;
var correctGuess = false;
var diff;
function guessFunction(){
guess = prompt('I am thinking of a number between 1 and 100. What is it?');
guessCount += 1;
var guessInt = parseInt(guess); //turn guesses into interger
if (guess == number){ //which number is bigger, guess or number
alert('Correct!');
correctGuess = true;
}
else if (guessInt > number) {
diff = guessInt - number;
}
else if (guessInt < number) {
diff = number - guessInt;
}
if (diff >= 1 && diff <= 10 && !correctGuess) {
alert('Very Hot');
guessFunction();
}
else if (diff < 10 && diff <= 20 && !correctGuess){
alert('Hot');
guessFunction();
}
else if (diff < 20 && diff <= 30 && !correctGuess){
alert('Warm');
guessFunction();
}
else if (diff < 30 && diff <= 50 && !correctGuess){
alert('Cold');
guessFunction();
}
else if (diff > 50 && !correctGuess){
alert('Ice Cold');
guessFunction();
}
}
guessFunction();
EDIT:
My answer is more verbose than it needs to be so that it relates directly to your question. The next step for you is to implement the better solution of a while loop mentioned in Menello's answer in your script.
I need to organize an unknown amount of elements into columns which read vertically. The code below works great for organizing the elements based on a set amount per column, but I won't know the amount of elements so I need to divide all the elements into 4 groups (possibly 5 or 6 cloumns though).
Column 1 = 1st 25% of elements
Column 2 = 2nd 25% of elements
Column 3 = 3rd 25% of elements
Column 4 = 4th 25% of elements
$(window).load(function(){
$('.equalChildHeights').setEqualHeights();
var linkNum = 1;
$('.organizeLinks').each(function(){
if (linkNum <= 10) {
$(this).addClass('linkCol1');
} else if (linkNum > 10 && linkNum <= 20) {
$(this).addClass('linkCol2');
} else if (linkNum > 20 && linkNum <= 30) {
$(this).addClass('linkCol3');
} else if (linkNum > 30 && linkNum <= 40) {
$(this).addClass('linkCol4');
} else if (linkNum > 40 && linkNum <= 50) {
$(this).addClass('linkCol4');
};
linkNum++;
});
$('.linkCol1').each(function(){
$(this).appendTo('.column-1');
});
$('.linkCol2').each(function(){
$(this).appendTo('.column-2');
});
$('.linkCol3').each(function(){
$(this).appendTo('.column-3');
});
$('.linkCol4').each(function(){
$(this).appendTo('.column-4');
});
Here is a working example:
Counts all elements and sets a var with the percentage.
var linkNum = 1;
var totoalLinksCol = $('.organizeLinks').length;
var linksPerCol = Math.ceil(totoalLinksCol/4);
$('.organizeLinks').each(function(){
if (linkNum <= linksPerCol) {
$(this).addClass('linkCol1');
} else if (linkNum > linksPerCol && linkNum <= linksPerCol*2) {
$(this).addClass('linkCol2');
} else if (linkNum > linksPerCol*2 && linkNum <= linksPerCol*3) {
$(this).addClass('linkCol3');
} else if (linkNum > linksPerCol*3 && linkNum <= linksPerCol*4) {
$(this).addClass('linkCol4');
};
linkNum++;
});
$('.linkCol1').each(function(){
$(this).appendTo('.column-1');
});
$('.linkCol2').each(function(){
$(this).appendTo('.column-2');
});
$('.linkCol3').each(function(){
$(this).appendTo('.column-3');
});
$('.linkCol4').each(function(){
$(this).appendTo('.column-4');
});
If you want to divide them into 5 columns, just change the 4 to a 5.
var linksPerCol = Math.ceil(totoalLinksCol/4);
You are looking for the modulus operator