I'm trying to solve the following Kata:
a 2 digit number, if you add the digits together, multiply by 3, add 45 and reverse.
I'm unable to figure out how to return the data from my function so that I can later assign the value to an HTML element.
This is my code.
function daily() {
for(var j = 10; j < 100; j++) {
function teaser(num) {
var x = num;
var y = x.toString().split("");
if(y.length == 2) {
var sum = parseInt(y[0]) + parseInt(y[1]);
if(sum * 3 == x) {
console.log(x + " is equal to 3 times " + sum);
var addFortyFive = x + 45;
console.log("Adding 45 to " + x + " gives " + addFortyFive);
var reversal = parseInt(addFortyFive.toString().split('').reverse().join(''));
console.log("'The 2 digit number " + x + ", is 3 times the sum (" + sum + ") of its digits. If 45 is added to " + x + ", the result is " + addFortyFive + ". If the digits are reversed, the number is... " + reversal + ".");
}
} else {
console.log("Not a 2 digit Number!!");
}
}
teaser(j);
}
}
From your question I'm guessing you need reversal value on function daily for loop.
Would recommend you to take out function teaser from inside for-loop, this will make code much cleaner and easy to understand and you can do like:
function daily() {
for(var j = 10; j < 100; j++) {
var teaser = teaser(j);
// Can now use anything returned from teaser function here
}
}
function teaser(num) {
var x = num;
var y = x.toString().split("");
if(y.length == 2) {
var sum = parseInt(y[0]) + parseInt(y[1]);
if(sum * 3 == x) {
console.log(x + " is equal to 3 times " + sum);
var addFortyFive = x + 45;
console.log("Adding 45 to " + x + " gives " + addFortyFive);
var reversal = parseInt(addFortyFive.toString().split('').reverse().join(''));
console.log("'The 2 digit number " + x + ", is 3 times the sum (" + sum + ") of its digits. If 45 is added to " + x + ", the result is " + addFortyFive + ". If the digits are reversed, the number is... " + reversal + ".");
return reversal;
}
} else {
console.log("Not a 2 digit Number!!");
return false;
}
}
If don't want to take function out then you can do this:
function daily() {
for(var j = 10; j < 100; j++) {
function teaser(num) {
var x = num;
var y = x.toString().split("");
if(y.length == 2) {
var sum = parseInt(y[0]) + parseInt(y[1]);
if(sum * 3 == x) {
console.log(x + " is equal to 3 times " + sum);
var addFortyFive = x + 45;
console.log("Adding 45 to " + x + " gives " + addFortyFive);
var reversal = parseInt(addFortyFive.toString().split('').reverse().join(''));
console.log("'The 2 digit number " + x + ", is 3 times the sum (" + sum + ") of its digits. If 45 is added to " + x + ", the result is " + addFortyFive + ". If the digits are reversed, the number is... " + reversal + ".");
return reversal;
}
} else {
console.log("Not a 2 digit Number!!");
return false;
}
}
var teaser = teaser(j);
// Can now use anything returned from teaser function here
}
}
Returning something from a function is very simple!
Just add the return statement to your function.
function sayHello(name) {
return 'Hello ' + name + '!';
}
console.log(sayHello('David'));
okay, so my issue has been solved! Thanks all of you, especially krillgar, so I had to alter the code you gave me krillgar, a little bit in order to populate the results array with only the numbers (one number in this case) that satisfy the parameters of the daily tease I was asking about. yours was populating with 89 undefined and on number, 27 because it is the only number that works.
One of my problems was that I was expecting the return statement to not only save a value, but also show it on the screen, but what I was not realizing was that I needed a place to store the value. In your code you created a result array to populate with the correct numbers. And also, I needed a variable to store the data for each iteration of the for loop cycling through 10 - 100. Anyways, you gave me what I needed to figure this out and make it do what I wanted it to do, and all is well in the world again.
Anyway, thank you all for your help and input, and I will always remember to make sure I have somewhere to store the answers, and also somewheres to store the value of each loop iteration in order to decide which numbers to push into the results array and save it so it can be displayed and/or manipulated for whatever purpose it may be. I guess I was just so busy thinking about the fact that when I returned num it didn't show the value, instead of thinking about the fact that I needed to store the value. Here is the final code for this problem and thanks again peoples!
function daily() {
var results = [];
for(var j = 10; j < 100; j++) {
function teaser(num) {
var x = num;
var y = x.toString().split("");
if(y.length == 2) {
var sum = parseInt(y[0]) + parseInt(y[1]);
if(sum * 3 == x) {
console.log(x + " is equal to 3 times " + sum);
var addFortyFive = x + 45;
console.log("Adding 45 to " + x + " gives " + addFortyFive);
var reversal = parseInt(addFortyFive.toString().split('').reverse().join(''));
console.log("'The 2 digit number " + x + ", is 3 times the sum (" + sum + ") of its digits. If 45 is added to " + x + ", the result is " + addFortyFive + ". If the digits are reversed, the number is... " + reversal + ".");
return num;
// Here you have one that is correct, so return it:
} else {
console.log(num + " does not fulfill function parameters");
// This is just so you can visualize the numbers
return null;
}
}
}
var answer = teaser(j);
if(answer != null) {
results.push(answer);
}
}
return results;
}
As was said in the comments of the question, because you're going to (most likely) have multiple answers that match your condition, you will need to store those in an array. Your teaser function returns individual results, where daily will check all the numbers in your range.
function daily() {
var results = [];
for(var j = 10; j < 100; j++) {
function teaser(num) {
var x = num;
var y = x.toString().split("");
if(y.length == 2) {
var sum = parseInt(y[0]) + parseInt(y[1]);
if(sum * 3 == x) {
console.log(x + " is equal to 3 times " + sum);
var addFortyFive = x + 45;
console.log("Adding 45 to " + x + " gives " + addFortyFive);
var reversal = parseInt(addFortyFive.toString().split('').reverse().join(''));
console.log("'The 2 digit number " + x + ", is 3 times the sum (" + sum + ") of its digits. If 45 is added to " + x + ", the result is " + addFortyFive + ". If the digits are reversed, the number is... " + reversal + ".");
// Here you have one that is correct, so return it:
return num;
} else {
// Make sure we don't return undefined for when the sum
// times three doesn't equal the number.
return null;
}
} else {
console.log("Not a 2 digit Number!!");
return null;
}
}
var answer = teaser(j);
if (answer !== null) {
results.push(answer);
}
}
return results;
}
Related
I gave an example of using .tofixed() with math, functions, and arrays, to a beginner coder friend who has been reviewing these topics in his class.
const bananaX = 9;
const bananaY = 2.9768;
bananaArray = [bananaX , bananaY];
console.log("X before array = " + bananaX);
console.log("Y before array = " + bananaY + '\n')
console.log("X,Y after array = " + bananaArray + '\n')
console.log("Value of X in array: " + bananaArray[0]+ '\n')
console.log("Value of Y in array: " + bananaArray[1]+ '\n')
function bananaDivision (bananaArray){
console.log("Value of X after function = " + bananaX);
console.log("Value of Y after function = " + bananaY + '\n')
let bananaDivided = Math.abs(bananaX/bananaY );
console.log (`X divided by Y = + ${bananaDivided}` + '\n')
let bananaFixed = bananaDivided.toFixed(2);
console.log("After using .toFixed(2) : " + bananaFixed + '\n');
};
bananaDivision();
They were understanding and following along no problem.
Then they asked me - "What if we put a decimal in the .toFixed ?"
So I ran:
const bananaX = 9;
const bananaY = 2.9768;
bananaArray = [bananaX , bananaY];
console.log("X before array = " + bananaX);
console.log("Y before array = " + bananaY + '\n')
console.log("X,Y after array = " + bananaArray + '\n')
console.log("Value of X in array: " + bananaArray[0]+ '\n')
console.log("Value of Y in array: " + bananaArray[1]+ '\n')
function bananaDivision (bananaArray){
console.log("Value of X after function = " + bananaX);
console.log("Value of Y after function = " + bananaY + '\n')
let bananaDivided = Math.abs(bananaX/bananaY );
console.log (`X divided by Y = + ${bananaDivided}` + '\n')
let bananaFixed = bananaDivided.toFixed(2);
let bananaFixed1 = bananaDivided.toFixed(.69420);
let bananaFixed2 = bananaDivided.toFixed(1.69420);
console.log("After using .toFixed(2) : " + bananaFixed + '\n');
console.log("After using .toFixed(.69420) : " + bananaFixed1 + '\n');
console.log("After using .toFixed(1.69420) : " + bananaFixed2 + '\n');
};
bananaDivision();
I explained it as that .toFixed is looking at the first number within the () and that the decimals are ignored.
Am I correct? For my own curiousity, is there a crazy way to break .toFixed() so that it actually uses decimals? I'm experimenting atm but wanted to know if someone already figured that out.
I explained it as that .toFixed is looking at the first number within the () and that the decimals are ignored.
This would be correct. That is essentially what happens.
For full correctness, the input of toFixed() will be converted to an integer. The specification states that the argument must first be converted to a number - NaN will be converted to a zero. Numbers with a fractional part will be rounded down.
Which means that if you pass any number, you essentially get the integer part of it.
It also means that non-numbers can be used:
const n = 3;
console.log(n.toFixed("1e1")); // 1e1 scientific notation for 10
You're close, since toFixed() expects an integer it will handle converting decimal numbers before doing anything else. It uses toIntegerOrInfinity() to do that, which itself uses floor() so the number is always rounded down.
Most of Javascript handles type conversion implicitly, so it's something you should really understand well if you don't want to run into problems. There's a free book series that explains that concept and a lot of other important Javascript knowledge very well, it's called You Don't Know JS Yet.
just a demo how .tofixed works !!!!!!
function roundFloat(x, digits) {
const arr = x.toString().split(".")
if (arr.length < 2) {
return x
}else if(arr[1] === ""){
return arr[0]
}else if(digits < 1){
return arr[0]
}
const st = parseInt(x.toString().split(".")[1]);
let add = false;
const rudgt = digits
const fX = parseInt(st.toString().split("")[rudgt]);
fX > 5 ? add = true : add = false
nFloat = parseInt(st.toString().split("").slice(0, rudgt).join(""))
if (add) {
nFloat += 1
}
const repeat0 = (() => {
if (rudgt - st.toString().length < 0) {
return 0
}
return rudgt - st.toString().length
})()
const output = x.toString().split(".")[0] + "." + nFloat.toString() + "0".repeat(repeat0);
return output
}
console.log(roundFloat(1.200, 2))
I am currently trying to complete an assignment for an intro2Javascript course. The question basically asks me to return a string of multiples of 2 parameters (num, numMultiple). Each time it increments the value i until i = numMultiple. For example:
5 x 1 = 5\n
5 x 2 = 10\n
5 x 3 = 15\n
5 x 4 = 20\n
This was my attempt:
function showMultiples(num, numMultiples) {
var result;
for (i = 1; i <= numMultiples; i++) {
result = num * i
multiples = "" + num + " x " + i + " = " + result + "\n"
return (multiples)
}
}
...and because the assignment comes with pre-written console logs:
console.log('showMultiples(2,8) returns: ' + showMultiples(2, 8));
console.log('showMultiples(3,2) returns: ' + showMultiples(3, 2));
console.log('showMultiples(5,4) returns: ' + showMultiples(5, 4));
console.log('\n');
This is my output:
showMultiples(2,8) returns: 2 x 1 = 2
Scratchpad/1:59:1
showMultiples(3,2) returns: 3 x 1 = 3
Scratchpad/1:60:1
showMultiples(5,4) returns: 5 x 1 = 5
UPDATE
You were doing two things incorrectly:
1) You were returning after the first iteration through your loop
2) You were assigning to multiples instead of appending to it.
Since you want to gather all the values and then show the final result first, I add all of the values to an array, and then use unshift() to add the final element (the result) to the beginning of the array. Then I use join() to return a string representation of the desired array.
function showMultiples(num, numMultiples) {
var result;
var multiples = [];
for (let i = 1; i <= numMultiples; i++) {
result = num * i
multiples.push("" + num + " x " + i + " = " + result + "\n")
}
multiples.unshift(multiples[multiples.length-1]);
return (multiples.join(''))
}
console.log('showMultiples(2,8) returns: ' + showMultiples(2, 8));
console.log('showMultiples(3,2) returns: ' + showMultiples(3, 2));
console.log('showMultiples(5,4) returns: ' + showMultiples(5, 4));
console.log('\n');
You need to declare all variables, because without you get global variables (beside that it does not work in 'strict mode').
The second point is to use multiples with an empty string for collecting all intermediate results and return that value at the end of the function.
For keeping the last result, you could use another variable and append that value at the end for return.
function showMultiples(num, numMultiples) {
var i,
result,
multiples = "",
temp = '';
for (i = 1; i <= numMultiples; i++) {
result = num * i;
temp = num + " x " + i + " = " + result + "\n";
multiples += temp;
}
return temp + multiples;
}
console.log('showMultiples(2,8) returns: ' + showMultiples(2, 8));
console.log('showMultiples(3,2) returns: ' + showMultiples(3, 2));
console.log('showMultiples(5,4) returns: ' + showMultiples(5, 4));
As other answers say, your problem is in multiple.
You are clearing multiple every iteration and storing the new value, but you do not want that, you want to add the new result, and to do so you use this code:
multiples = multiple + "" + num + " x " + i + " = " + result + "\n"
which can be compressed in what the rest of the people answered:
multiples += "" + num + " x " + i + " = " + result + "\n"
Probably you already know, but to ensure:
a += b ---> a = a + b
a -= b ---> a = a - b
a *= b ---> a = a * b
and there are even more.
function showMultiples(num, numMultiples){
for(i=1; i<=numMultiples; i++){
var multiple = num + " x " + i + " = " + num * i;
}
return multiple;
}
console.log('showMultiples(2,8) returns: ' + showMultiples(2,8));
For this code, what the function should do is, by looking at num and numMultiples variable, it should give you the list of multiplication that is possible with the two numbers. Therefore the console should print out
2x1=2 2x2=4 2x3=6 2x4=8 2x5=10 2x6=12 2x7=14 2x8=16
However, this code prints out 2x8 = 16 any guess to why?
You're assigning the value to multiple then returning it in the end, when your loop has finished, meaning multiple will be 2x8. If you do a console.log(multiple) right under var multiple = num + " x " + i + " = " + num * i; you will see it print out correctly.
EDIT:
function showMultiples(num, numMultiples){
var result = [];
for(i=1; i<=numMultiples; i++){
result.push(num + " x " + i + " = " + num * i);
}
return result.join(' ');
}
Add results to an array and when the function completes, join the values inside the array and return the results.
You only have one print statement, and that is outside of the loop. If you want to print multiple times, you need to put the print statement inside of the loop, something like this:
function showMultiples(num, numMultiples) {
console.log(`showMultiples(${num}, ${numMultiples}) returns:`);
Array.from({length: numMultiples}, (v, k) => k + 1).
forEach(i => console.log(`${num}×${i} = ${num * i}`));
}
showMultiples(2, 8)
// showMultiples(2, 8) returns:
// 2×1 = 2
// 2×2 = 4
// 2×3 = 6
// 2×4 = 8
// 2×5 = 10
// 2×6 = 12
// 2×7 = 14
// 2×8 = 16
However, that is bad design. You shouldn't mix data transformation and I/O. It is much better to separate the two and build the data up first completely, then print it:
function showMultiples(num, numMultiples) {
return Array.from({length: numMultiples}, (v, k) => k + 1).
map(i => `${num}×${i} = ${num * i}`).
join(", ");
}
console.log(`showMultiples(2, 8) returns: ${showMultiples(2, 8)}`);
// showMultiples(2, 8) returns: 2×1 = 2, 2×2 = 4, 2×3 = 6, 2×4 = 8, 2×5 = 10, 2×6 = 12, 2×7 = 14, 2×8 = 16
This is much more idiomatic ECMAScript.
How can I get the number's position by c or Javascript.
It is a ordered number array,like
1 2
3 4
5 6
how can I make a function to get the number 3 is 2-1,and 6 is 3-2
Thank you very much!
You could do something like this in javascript
var data = [
[1, 2],
[3, 4],
[5, 6]
];
var getPos = function(elem, data) {
var result = '';
data.forEach(function(value, index) {
value.forEach(function(v, i) {
if (v == elem) result += (index + 1) + '-' + (i + 1);
});
});
return result;
}
console.log(getPos(5, data));
In C you could write a function that would implement the following pseudo-code:
//...
for(i=0 ; i<ARRAY_SIZE ; i++)
{
if(array[i] == desired_num)
return i;
else
i++;
}
return -1; // return error if desired_num not found
//...
If it's a continuous ordered two-dimensional array you can do something like this to get the indices. (Notice +1 as per your example)
int x = floor(number/2) +1:
int y = (number+1)%2 +1;
If you are using arrays you should be able to jump the the right index without needing to loop like I see other answers suggesting.
array[x][y]
You may do something like this
var table = (new Array (10)).fill(new Array(10).fill("")).map( e=> e.map( f => ~~(Math.random()*1000))),
results = [];
function findInTable(t, q){
var finds = {x:[],y:[]};
t.forEach( (a, i) => {j = a.indexOf(q); ~j && (finds.x.push(j+1), finds.y.push(i+1))});
return finds;
}
results = findInTable(table, 777);
document.write("<pre>" + table[0] + "\n" + table[1] + "\n" + table[2] + "\n" + table[3] + "\n" + table[4] + "\n" + table[5] + "\n" + table[6] + "\n" + table[7] + "\n" + table[8] + "\n" + table[9] + "\n" +"</pre>");
document.write("<pre> Searched for " + 777+ " and found at:\n" + JSON.stringify(results, null, 4) + "</pre>");
I need to execute a if/else conditional inside a JS 'for' loop. Specifically, this is what I'm told to do:
Here is my code
var numSheep = 4;
var monthsToPrint = 12;
for (var monthNumber = 1; monthNumber <= monthsToPrint; monthNumber++) {
if (numSheep < 10000) {
numSheep *= 4;
console.log("There will be " + numSheep + " sheep after " + monthNumber + " month(s)!");
} else {
numSheep *= 4;
var number = numSheep / 2;
var newNum = numSheep - number;
console.log("Removing " + number + " sheep from the population. Phew!");
console.log("There will be " + newNum + " sheep after " + monthNumber + " month(s)!");
}
}
Everytime I submit this code I get this error from codeschool, "You're calling console.log the correct number of times, but not logging the correct messages. Are you diving the numSheep by 2 whenever there are more than 10000."
P.S: It's challenge no. 5 of JavaScript Road Trip Part 2 on codeschool. If you need to log into codeschool and see for yourself, this link should give a 48 hour access: http://go.codeschool.com/eO1V6A
If I got everything correct
First of all as #eosterberg mentioned in a comment the text says "for any month the population is above 10,000"
Secondly you need to save the number of sheep you have left; numSheep = numSheep/2, the sheep you sent away is right now the same but store that in a variable, sentSheep.
Thirdly the text says; "The rate at which the staying population is grows, however, will stay the same(x4)" this means that the population that stays only grows(at least what we know) => you should put numSheep *= 4; after the calculations and before the console.log's
And the actual code would look like;
var numSheep = 4;
var monthsToPrint = 12;
for (var monthNumber = 1; monthNumber <= monthsToPrint; monthNumber++) {
if (numSheep <= 10000) {
numSheep *= 4;
console.log("There will be " + numSheep + " sheep after " + monthNumber + " month(s)!");
} else {
numSheep = numSheep / 2;
var sentSheep = numSheep;
numSheep *= 4;
console.log("Removing " + sentSheep + " sheep from the population. Phew!");
console.log("There will be " + numSheep + " sheep after " + monthNumber + " month(s)!");
}
}