Get help on our basic JavaScript and Algorithms Challenges. If you are posting code that is large use Gist - https://gist.github.com/ paste the link here.
if (something) {
} else {
}
I think is enough for me today, I let the current code in case someone can give me a hint on what I am doing wrong.
function add() {
var sum = 0;
function sumfirstand(x){
return function(y){
if(typeof y === "number"){
return x + y;
} else {return undefined;}
};
}
if(arguments.length == 1 && typeof arguments[0] == "number"){
sum = sumfirstand(arguments[0]);
return sum;
}else{
for(var i = 0; i < arguments.length; i++){
var type = typeof arguments[i];
if(type !== "number"){ return undefined;
} else { sum += arguments[i]; }
}
return sum;
}
}
add(2)(3);
Tomorrow more, nn!
function add() {
var sum = 0;
function sumfirstand(x) {
return function (y) {
if (typeof y === "number") {
return x + y;
}
};
}
if (arguments.length === 1 && typeof arguments[0] == "number") {
sum = sumfirstand(arguments[0]);
return sum;
}
else {
for (var i = 0; i < arguments.length; i++) {
var type = typeof arguments[i];
if (type !== "number") {
return undefined;
}
else {
sum += arguments[i];
}
}
return sum;
}
}
function Person(firstAndLast) {
var nameArr = firstAndLast.split(" ");
return {
getFirstName : function() {
return nameArr[0];
},
getLastName : function() {
return nameArr[1];
},
getFullName : function() {
return nameArr[0] + " " + nameArr[1];
},
setFirstName : function(first) {
nameArr[0] = first;
},
setLastName : function(last) {
nameArr[1] = last;
},
setFullName : function(firstAndLast) {
nameArr = firstAndLast.split(" ");
}
};
};
var bob = new Person('Bob Ross');
bob.getFullName();
this.getFirstName = function() { ...
this.name = nameArr[0]
while(blah<blue){
while(cat=green){
var blah = 35;
}
}
instanceOf()
instead
savrov sends brownie points to @tylermoeller :sparkles: :thumbsup: :sparkles:
:star: 399 | @tylermoeller | http://www.freecodecamp.com/tylermoeller
this.name = function() {}
inside your constructor function. Or extend Person
's prototype, but you'd fail the property count test
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["Javascript", "Gaming", "Foxes"]
}
];
console.log(contacts.hasOwnProperty("number"))
contacts
is an ARRAY
console.log(contacts[0].hasOwnProperty('number'));
rnwebdk sends brownie points to @dcnr and @tylermoeller :sparkles: :thumbsup: :sparkles:
:star: 400 | @tylermoeller | http://www.freecodecamp.com/tylermoeller
:star: 754 | @dcnr | http://www.freecodecamp.com/dcnr
sreeves89 sends brownie points to @dcnr and @pjconsidine :sparkles: :thumbsup: :sparkles:
:star: 394 | @pjconsidine | http://www.freecodecamp.com/pjconsidine
:star: 755 | @dcnr | http://www.freecodecamp.com/dcnr
rnwebdk sends brownie points to @tylermoeller and @iheartkode :sparkles: :thumbsup: :sparkles:
:warning: rnwebdk already gave tylermoeller points
:star: 552 | @iheartkode | http://www.freecodecamp.com/iheartkode
rnwebdk sends brownie points to @iheartkode :sparkles: :thumbsup: :sparkles:
:warning: rnwebdk already gave iheartkode points
working on loops "Iterate Through an Array with a For Loop” specifically.
can someone explain to how the loop is doing its math?
var myArr = [ 2, 3, 4, 5, 6];
var total = 0;
for (var i = 0; i < myArr.length; i++){
total += myArr[i];
}
adding the total its 20. If I were to do myArr[4] it equals to 30 myArr[3] = 25, myArr2 = 20, myArr1 = 15, myArr[0] = 10.
I want to know how its processing it.
var arr = [1,2,3,4,5,6];
for(var i = 0; i < arr.length; i++) {
console.log(i);
}
0
1
2
3
4
5
@vtli94 You can always add console.log()
to get more detail:
var myArr = [2, 3, 4, 5, 6];
var total = 0;
for (var i = 0; i < myArr.length; i++) {
console.log('iteration: ' + i + ' = ' + 'total + myArr[i]' + ' = ' + total + ' + ' + myArr[i] + ' = ' + (total + myArr[i]));
total += myArr[i];
}
Output is:
iteration: 0 = total + myArr[i] = 0 + 2 = 2
iteration: 1 = total + myArr[i] = 2 + 3 = 5
iteration: 2 = total + myArr[i] = 5 + 4 = 9
iteration: 3 = total + myArr[i] = 9 + 5 = 14
iteration: 4 = total + myArr[i] = 14 + 6 = 20
iheartkode sends brownie points to @tylermoeller :sparkles: :thumbsup: :sparkles:
:star: 402 | @tylermoeller | http://www.freecodecamp.com/tylermoeller
vtli94 sends brownie points to @iheartkode and @tylermoeller :sparkles: :thumbsup: :sparkles:
:star: 553 | @iheartkode | http://www.freecodecamp.com/iheartkode
:star: 403 | @tylermoeller | http://www.freecodecamp.com/tylermoeller
function largestOfFour(arr) {
// You can do this!
var result = [];
for (var i = 0; i < arr.length; i++){
var largestNumber = 0;
for(var j=0; j < arr.length; j++){
if(largestNumber<arr[i][j]){
result=arr[i][j];
}
}
result.push(largestNumber);
}
return result;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
return "undefined"
; now I feel silly.
function largestOfFour(arr) {
// You can do this!
var result = [];
for (var i = 0; i < arr.length; i++){
var largestNumber = 0;
for(var j=0; j < arr.length; j++){
if(largestNumber<arr[i][j]){
result=arr[i][j];
}
}
result.push(largestNumber);
}
return result;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
// Setup
function phoneticLookup(val) {
var result = "";
// Only change code below this line
var lookup = {
"alpha": "Adams",
"bravo": "Boston",
"charlie": "Chicago",
"delta": "Denver",
"echo": "Easy",
"foxtrot": "Frank",
};
// Only change code above this line
return result;
}
// Change this value to test
phoneticLookup("charlie");
function myTest(val) {
// Only change code below this line
if (val > 10 || val < 20) {
return "Outside";
}
// Only change code above this line
return "Inside";
}
// Change this value to test
myTest(15);
Why is this not returning inside?
// Setup
function phoneticLookup(val) {
var result = "";
// Only change code below this line
var lookup = {
alpha: "Adams",
bravo: "Boston",
charlie: "Chicago",
delta: "Denver",
echo: "Easy",
foxtrot: "Frank",
};
var result = lookup.val;
// Only change code above this line
return result;
}
// Change this value to test
phoneticLookup("charlie");
sethnejame sends brownie points to @gvik02 :sparkles: :thumbsup: :sparkles:
:star: 280 | @gvik02 | http://www.freecodecamp.com/gvik02
```
bitgrower sends brownie points to @username :sparkles: :thumbsup: :sparkles:
:warning: could not find receiver for username
```
ar contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["Javascript", "Gaming", "Foxes"]
}
];
function lookUp(firstName, prop){
// Only change code below this line
for(var i = 0; i < contacts.length; i++){
if(contacts[i].firstName === firstName &&
contacts[i].hasOwnProperty(prop) === true)
{
return contacts[i][prop];
}
else if (contacts[i].firstName !== firstName){
return "No such contact";
}
return "No such property";
}
// Only change code above this line
}
// Change these values to test your function
lookUp("Akira", "address");
```
Why is this failing?
Encode the following sequence, separated by spaces:
backslash tab tab carriage-return new-line and assign it to myStr
var myStr = "\b \t \t \r \n"; // Change this line
```js ⇦ Type 3 backticks and then press [shift + enter ⏎]
(type js or html or css)
<paste your code here>,
then press [shift + enter ⏎]
``` ⇦ Type 3 backticks, then press [enter ⏎]
This an inline `<paste code here>
` code formatting with a single backtick(`) at start and end around the code
.
See also: ☛ How to type Backticks | ☯ Compose Mode | ❄ Gitter Formatting Basics
so @cfj2
if ( a <= num) <--- 1 < 5 ... yes
val = a val; <--- 11 = 1
function multiplyAll(arr) {
var product = 1;
// Only change code below this line
for (var i=0; i < multiplyAll.length; i++)
{
for (var j=0; j < multiplyAll[i].length; j++)
{
console.log(multiplyAll[i]*[j]);
}
}
// Only change code above this line
return product;
}
// Modify values below to test your code
multiplyAll([[1,2],[3,4],[5,6,7]]);
@grrttmrtn -- are you trying to ? :)
first you might want to use the length of arr instead of the length of multiplyAll (the function)
bitgrower sends brownie points to @camperbot :sparkles: :thumbsup: :sparkles:
:star: 1307 | @camperbot | http://www.freecodecamp.com/camperbot
```js ⇦ Type 3 backticks and then press [shift + enter ⏎]
(type js or html or css)
<paste your code here>,
then press [shift + enter ⏎]
``` ⇦ Type 3 backticks, then press [enter ⏎]
This an inline `<paste code here>
` code formatting with a single backtick(`) at start and end around the code
.
See also: ☛ How to type Backticks | ☯ Compose Mode | ❄ Gitter Formatting Basics
'function multiplyAll(arr) {
var product = 1;
// Only change code below this line
for (var i=0; i < product.length; i++)
{
for (var j=0; j < product[i].length; j++)
{
console.log(product[i]*[j]);
}
}
// Only change code above this line
return product;
}
// Modify values below to test your code
multiplyAll([[1,2],[3,4],[5,6,7]]);'
function multiplyAll(arr) {
var product = 1;
// Only change code below this line
for (var i=0; i < product.length; i++)
{
for (var j=0; j < product[i].length; j++)
{
console.log(product[i]*[j]);
}
}
// Only change code above this line
return product;
}
// Modify values below to test your code
multiplyAll([[1,2],[3,4],[5,6,7]]);
:bulb: to format code use backticks! ``` more info
function multiplyAll(arr) {
var product = 1;
// Only change code below this line
for (var i=0; i < arr.length; i++)
{
for (var j=0; j < arr[i].length; j++)
{
console.log(arr[i]*[j]);
}
}
// Only change code above this line
return product;
}
// Modify values below to test your code
multiplyAll([[1,2],[3,4],[5,6,7]]);
asaki444 sends brownie points to @bitgrower :sparkles: :thumbsup: :sparkles:
:star: 706 | @bitgrower | http://www.freecodecamp.com/bitgrower
now ... what if I had
myArray = [ [1,2,3], [2,3,4],[5,6,7]];
what would myArray[2] be equal to ?
now ... do you remember this syntax:
myNum += 1;
so ... if I have a loop like this ...
var num = 3;
j = 0;
for (i = 0; i < num; i++) {
j += 1;
}
what is j going to equal at the end ?
what if I said
j *= i;
instead ?
with profile lookup
function truncate(str, num) {
// Clear out that junk in your trunk
var trunk = '';
if (str.length > num) {
trunk = str.slice(0, num - 3) + '...';
return trunk;
}
return str;
}
@grrttmrtn -- what if I changed the inner loop to do this instead, @grrttmrtn ...
j *= i;
davidweedmark sends brownie points to @grrttmrtn :sparkles: :thumbsup: :sparkles:
:star: 258 | @grrttmrtn | http://www.freecodecamp.com/grrttmrtn
this.removeEventListener("click");
davidweedmark sends brownie points to @bitgrower :sparkles: :thumbsup: :sparkles:
:star: 707 | @bitgrower | http://www.freecodecamp.com/bitgrower
so ... if I have a loop like this ...
var num = 3;
j = 2;
for (i = 0; i < num; i++) {
j *= 1;
}
now what would j be equal do ...
var num = 3;
j = 2;
for (i = 0; i < num; i++) {
j *= i;
}
@grrttmrtn -- you have a couple of different pieces I'm building up, @grrttmrtn ...
oh nuts let me try that again ...actually, it would be zero
var num = 3;
j = 2;
for (i = 1; i < num; i++) {
j *= i;
}
`so ... first time thru the loop it will be j = j * 1
so j will be 2
next time thru the loop it will be j = j * 2
grrttmrtn sends brownie points to @bitgrower :sparkles: :thumbsup: :sparkles:
:star: 708 | @bitgrower | http://www.freecodecamp.com/bitgrower
```js ⇦ Type 3 backticks and then press [shift + enter ⏎]
(type js or html or css)
<paste your code here>,
then press [shift + enter ⏎]
``` ⇦ Type 3 backticks, then press [enter ⏎]
This an inline `<paste code here>
` code formatting with a single backtick(`) at start and end around the code
.
See also: ☛ How to type Backticks | ☯ Compose Mode | ❄ Gitter Formatting Basics
contacts[i][prop]
?
//Setup
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["Javascript", "Gaming", "Foxes"]
}
];
function lookUp(firstName, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
if (firstName == contacts.firstName && prop == contacts.i) {
return contacts[prop];
} else if (firstName !== contacts.firstName) {
return "No such contact";
} else if (prop !== contacts.prop) {
return "No such property";
}
}
// Only change code above this line
}
// Change these values to test your function
lookUp("Akira", "likes");
@glenohumeral13 ... well ... I think the first thing you need to do is look for a firstname, no ?
And how did you use contacts[i][prop] ?
//Setup
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["Javascript", "Gaming", "Foxes"]
}
];
function lookUp(firstName, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
if (firstName == contacts.firstName && prop == contacts[i][prop]) {
return contacts[prop];
} else if (firstName !== contacts.firstName) {
return "No such contact";
} else if (prop !== contacts.prop) {
return "No such property";
}
}
// Only change code above this line
}
// Change these values to test your function
lookUp("Akira", "likes");
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
}
arr[1]
ok so i have this which isnt working ```
//Setup
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["Javascript", "Gaming", "Foxes"]
}
];
function lookUp(firstName, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
if (firstName == contacts.firstName && prop == contacts[i].prop) {
return contacts[i].prop;
} else if (firstName !== contacts.firstName) {
return "No such contact";
} else if (prop !== contacts[i].prop) {
return "No such property";
}
}
// Only change code above this line
}
// Change these values to test your function
lookUp("Akira", "likes");
```
//Setup
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["Javascript", "Gaming", "Foxes"]
}
];
function lookUp(firstName, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
if (firstName == contacts.firstName && prop == contacts[i].prop) {
return contacts[i].prop;
} else if (firstName !== contacts.firstName) {
return "No such contact";
} else if (prop !== contacts[i].prop) {
return "No such property";
}
}
// Only change code above this line
}
// Change these values to test your function
lookUp("Akira", "likes");
if (firstName == contacts.firstName && prop == contacts[i].prop) {
if I have
retArr = [];
myArr = [1,2,3];
for (var i = 0; i < myArr.length; i++) {
retArr.push(myArr[i]);
}
do you know what this does?
[1, 2, 3]
The explanation for this problem is very simple. You will generate a list of prime numbers up to the number you are given as a parameter. Then you need to add them all up and return that value. The tricky part is on generating the list of prime numbers. I suggest you find a code or a good math algorithm that you can turn into code.
:pencil: read more about algorithm sum all primes on the FCC Wiki
needed with exact change challenge - its late im tired maybe i am overlooking something obviousi am getting the right answers for the change but not as 2 decimal places ie - dime 02 quarter 05 one 1 etc - instead of dime 020 quarter 050 one 100 i can use tofixed2 to force 2 decimals but then it converts to a string and that is not accepted as the right answer either converting back to a number loses the decimal places again i am stumped how do i force a number to be 2 decimal places without converting it to a string
0.5 === 0.50
so no worries
@glenohumeral13 -- if you have an array ... assume each element of the array is a room ... the room can contain anything ... but in order to get to anything within the room, you still have to access it by the array index ...
...so, if the element is an object ... then you need to use the array indexing followed by your object addressing ...
your object address can be either dot or bracket ...
if you know the name, and the name is appropriate ... you can use the dot notation ...
contacts[i].firstname
Instructions
You are given a JSON object representing (a small part of) your record collection. Each album is identified by a unique id number and has several properties. Not all albums have complete information.
Write a function which takes an id, a property (prop), and a value.
For the given id in collection:
If value is non-blank (value !== ""), then update or set the value for the prop.
If the prop is "tracks" and value is non-blank, push the value onto the end of the tracks array.
If value is blank, delete that prop.
Always return the entire collection object.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
:pencil: read more about checkpoint record collection on the FCC Wiki
or if it's a property named in a variable, then you will use bracket notation
contacts[i][prop]
You will need to gather all the Fibonacci numbers and then check for the odd ones. Once you get the odd ones then you will add them all. The last number should be the number given as a parameter if it actually happens to be an off Fibonacci number.
:pencil: read more about algorithm sum all odd fibonacci numbers on the FCC Wiki
//Setup
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["Javascript", "Gaming", "Foxes"]
}
];
function lookUp(firstName, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
if (firstName == contacts[i].firstName && prop == contacts[i][prop]) {
return contacts[i].prop;
} else if (firstName !== contacts[i].firstName) {
return "No such contact";
} else if (prop !== contacts[i][prop]) {
return "No such property";
}
}
// Only change code above this line
}
// Change these values to test your function
lookUp("Akira", "likes");
This problem seems simple but you need to make sure to flatten any array, regardless of the level which is what adds a bit of difficulty to the problem.
:pencil: read more about algorithm steamroller on the FCC Wiki
anyway .. let's take a look ...
firstName is "Akira" ... so you look at contacts[i].firstName ... and yes, it's Akira ... but then we have to see if this particular object has a property "likes" ... it doesn't ... so it goes to your ELSE statement ...
well ... we already know that firstName === contacts[i].firstName, so it skips that ...
however ... it looks to see if prop !== contacts[i][prop] (which we already determined before, btw)
...and it doesn't ... so it returns "No such property"
The smallest common multiple between two numbers is the smallest number that
both numbers can divide into. This concept can be extended to more than two
numbers as well.
We can first start with just finding the smallest common multiple between two
numbers. Naively, you can start writing out multiple of each number until you
write a multiple that exists from both numbers.
An example would be the numbers 3
and 4
. The multiples of 3
are 3, 6, 9,
12, 15, 18, ...
and the multiples of 4
are 4, 8, 12, 16, 20, ...
. The
first smallest number we run into in both lists is 12
so this is the smallest
common multiple between 3
and 4
.
This problem can be confusing because most people look for the smallest common
multiple of just the two numbers but forget the keyword range. However, this
means that if you are given [1,5]
, then you have to check for the smallest
common multiple for all the numbers [1,2,3,4,5]
that is evenly divisible by
all of them.
:pencil: read more about algorithm smallest common multiple on the FCC Wiki
it's important ... what @gloriousjob said ... you need to understand what the code is doing ...
you can put your code in here: pythontutor.com/javascript.html ... and see a cool visualization of what is happening in your code ...
[FreeCodeCamp/Help]
[FreeCodeCamp/HelpJavaScript]
[FreeCodeCamp/HelpFrontEnd]
[FreeCodeCamp/HelpDataViz]
[FreeCodeCamp/HelpBackEnd]
[FreeCodeCamp/Python]
[FreeCodeCamp/Java]
[FreeCodeCamp/Go]
[FreeCodeCamp/Ruby]
[FreeCodeCamp/PHP]
[FreeCodeCamp/Elixir]
[FreeCodeCamp/Linux]
[FreeCodeCamp/SQL]
[FreeCodeCamp/NonprofitProjects]
[FreeCodeCamp/CodeReview]
[FreeCodeCamp/YouCanDoThis]
function queue(arr, item) {
// Your code here
return arr; // Change this line
}
// Test Setup
var testArr = [1,2,3,4,5];
// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(queue(testArr1, 6)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));
rexchip sends brownie points to @bitgrower :sparkles: :thumbsup: :sparkles:
:star: 709 | @bitgrower | http://www.freecodecamp.com/bitgrower
Instructions
You are given a JSON object representing (a small part of) your record collection. Each album is identified by a unique id number and has several properties. Not all albums have complete information.
Write a function which takes an id, a property (prop), and a value.
For the given id in collection:
If value is non-blank (value !== ""), then update or set the value for the prop.
If the prop is "tracks" and value is non-blank, push the value onto the end of the tracks array.
If value is blank, delete that prop.
Always return the entire collection object.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
:pencil: read more about checkpoint record collection on the FCC Wiki
```js ⇦ Type 3 backticks and then press [shift + enter ⏎]
(type js or html or css)
<paste your code here>,
then press [shift + enter ⏎]
``` ⇦ Type 3 backticks, then press [enter ⏎]
This an inline `<paste code here>
` code formatting with a single backtick(`) at start and end around the code
.
See also: ☛ How to type Backticks | ☯ Compose Mode | ❄ Gitter Formatting Basics
I am wondering why the two codes below giving different result:
First code:
function convert(num) {
var str = "";
var tmp;
tmp = Math.floor(num/1000);
for(i=0;i<tmp;i++)
str += "M";
tmp = Math.floor((num%1000)/100);
if(tmp === 9)
str += "CM";
else if(tmp >= 5) {
str += "D";
for(i=0;i<tmp-5;i++)
str += "C";
}
else if(tmp === 4)
str += "CD";
else if(tmp < 4) {
for(i=0;i<tmp;i++)
str += "C";
}
tmp = Math.floor((num%100)/10);
if(tmp === 9)
str += "XC";
else if(tmp >= 5) {
str += "L";
for(i=0;i<tmp-5;i++)
str += "X";
}
else if(tmp === 4)
str += "XL";
else if(tmp < 4) {
for(i=0;i<tmp;i++)
str += "X";
}
tmp = num%10;
if(tmp === 9)
str += "IX";
else if(tmp >= 5) {
str += "V";
for(i=0;i<tmp-5;i++)
str += "I";
}
else if(tmp === 4)
str += "IV";
else if(tmp < 4) {
for(i=0;i<tmp;i++)
str += "I";
}
return str;
}
convert(3999);
function convert(num) {
var str = "";
var tmp;
tmp = Math.floor(num/1000);
for(i=0;i<tmp;i++)
str += "M";
tmp = Math.floor((num%1000)/100);
if(tmp === 9)
str += "CM";
else if(tmp >= 5) {
str += "D";
for(i=0;i<tmp-5;i++)
str += "C";
}
else if(tmp === 4)
str += "CD";
else if(tmp < 4)
for(i=0;i<tmp;i++)
str += "C";
tmp = Math.floor((num%100)/10);
if(tmp === 9)
str += "XC";
else if(tmp >= 5) {
str += "L";
for(i=0;i<tmp-5;i++)
str += "X";
}
else if(tmp === 4)
str += "XL";
else if(tmp < 4) {
for(i=0;i<tmp;i++)
str += "X";
}
tmp = num%10;
if(tmp === 9)
str += "IX";
else if(tmp >= 5) {
str += "V";
for(i=0;i<tmp-5;i++)
str += "I";
}
else if(tmp === 4)
str += "IV";
else if(tmp < 4) {
for(i=0;i<tmp;i++)
str += "I";
}
return str;
}
convert(3999);
else if(tmp < 4)
for(i=0;i<tmp;i++)
str += "C";
FreeCodeCamp/CodeReview
for Code Review after completion
When a return
statement is reached, the execution of the current function stops and control returns to the calling location.
function myFun() {
console.log("Hello");
return "World";
console.log("byebye")
}
myFun();
The above outputs "Hello" to the console, returns "World", but "byebye"
is never output, because the function exits at the return
statement.
:pencil: read more about challenge return early pattern for functions on the FCC Wiki
mukhtar2t sends brownie points to @davidweedmark :sparkles: :thumbsup: :sparkles:
:star: 372 | @davidweedmark | http://www.freecodecamp.com/davidweedmark
mukhtar2t sends brownie points to @abhisekp :sparkles: :thumbsup: :sparkles:
:star: 2244 | @abhisekp | http://www.freecodecamp.com/abhisekp
return value !== args[i];
function destroyer(arr) {
// Remove all the values
var args = Array.prototype.slice.call(arguments);
var filtered;
filtered = arr.filter(function destroy(value) {
for (var i = 1; i < args.length; i++) {
return value !== args[i];
}
});
return filtered;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
cyberistic sends brownie points to @davidweedmark :sparkles: :thumbsup: :sparkles:
:star: 373 | @davidweedmark | http://www.freecodecamp.com/davidweedmark
function drop(arr, func) {
// Drop them elements.
for (var i = 0; i <= arr.length; i++) {
if (func(arr[0])) {
break;
} else {
arr.shift();
}
}
return arr;
}
drop([1, 2, 3, 4], function(n) {return n > 5; });
Booleans can only hold the value of either true or false. They are basically little on-off switches.
:pencil: read more about challenge understand boolean values on the FCC Wiki
About queues
In Computer Science a queue is an abstract Data Structure where items are kept in order. New items can be added at the back of the queue and old items are taken off from the front of the queue.
Instructions
Write a function queue which takes an "array" and an "item" as arguments.
Add the item onto the end of the array, then remove the first element of the array.
The queue function should return the element that was removed.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
:pencil: read more about checkpoint stand in line on the FCC Wiki
function golfScore(par, strokes) {
// Only change code below this line
if (1) {
return "Hole-in-one!";
} else if (2) {
return "Eagle";
} else if (3) {
return "Birdie";
} else if (4) {
return "Par";
} else if (5) {
return "Bogey";
} else if (6) {
return "Double Bogey";
} else if (strokes >= 7) {
return "Go Home!";
}
// Only change code above this line
}
// Change these values to test
golfScore(4, 2);
Basically while the second argument is not true, you will have to remove the first element from the left of the array that was passed as the first argument.
if (strokes == 1) {
return "Hole-in-one!";
}
challenge:Word Blanks
code:
function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) {
var result = "";
// Your code below this line
result ="dog" + "big" + "ran" + "quickly";
// Your code above this line
return result;
}
// Change the words here to test your function
wordBlanks("dog", "big", "ran", "quickly");
so i don't know how to solve it.
doudeman sends brownie points to @gloriousjob :sparkles: :thumbsup: :sparkles:
:star: 306 | @gloriousjob | http://www.freecodecamp.com/gloriousjob
function golfScore(par, strokes) {
// Only change code below this line
if (strokes == 1) {
return "Hole-in-one!";
} else if (strokes <=par-2) {
return "Eagle";
} else if (strokes == par-1) {
return "Birdie";
} else if (par) {
return "Par";
} else if (strokes == par+1) {
return "Bogey";
} else if (strokes == par+2) {
return "Double Bogey";
} else if (strokes >= par+3) {
return "Go Home!";
}
// Only change code above this line
}
// Change these values to test
golfScore(4, 2);
moose1551 sends brownie points to @cyberistic and @davidweedmark :sparkles: :thumbsup: :sparkles:
:star: 374 | @davidweedmark | http://www.freecodecamp.com/davidweedmark
:star: 240 | @cyberistic | http://www.freecodecamp.com/cyberistic
function golfScore(par, strokes) {
// Only change code below this line
if (strokes == 1) {
return "Hole-in-one!";
} else if (strokes <=par-2) {
return "Eagle";
} else if (strokes == par-1) {
return "Birdie";
} else if (strokes == 4 && par == 4) {
return "Par";
} else if (strokes == par+1) {
return "Bogey";
} else if (strokes == par+2) {
return "Double Bogey";
} else if (strokes >= par+3) {
return "Go Home!";
}
// Only change code above this line
}
// Change these values to test
golfScore(4, 2);
else if (strokes == 4 && par == 4) {
return "Par";
else if (strokes == 4 && par == 4) {
return "Par";
rpark4 sends brownie points to @masd925 :sparkles: :thumbsup: :sparkles:
:star: 1129 | @masd925 | http://www.freecodecamp.com/masd925
var count = 0;
function cc(card) {
// Only change code below this line
switch (card) {
case 2:
case 3:
case 4:
case 5:
case 6:
count++;
break;
case 10:
case "J":
case "Q":
case "K":
case "A":
count--;
break;
}
if (card <= 6 && card >= 2){
return count + " Bet";
}else if (card >= 7 && card <= 9 ){
return count + " Hold";
}else if (card == 10 || card === 'J' || card === 'Q' || card === 'K' || card === 'A'){
return count + " Hold";
}
// Only change code above this line
}
// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc(3); cc(7); cc('K'); cc(2);
function golfScore(par, strokes) {
// Only change code below this line
if (strokes == 1 && par == 4) {
return "Hole-in-one!";
} else if (strokes <=par-2) {
return "Eagle";
} else if (strokes == par-1) {
return "Birdie";
} else if (par) {
return "Par";
} else if (strokes == par+1) {
return "Bogey";
} else if (strokes == par+2) {
return "Double Bogey";
} else if (strokes >= par+3) {
return "Go Home!";
}
// Only change code above this line
}
// Change these values to test
golfScore(4, 2);
@Moose1551
else if (strokes == par) {
return "Par";
Is what you need to do
moose1551 sends brownie points to @abhishekkumar1 and @alfonsotech and @cyberistic and @rpark4 :sparkles: :thumbsup: :sparkles:
:warning: moose1551 already gave cyberistic points
:star: 353 | @alfonsotech | http://www.freecodecamp.com/alfonsotech
:star: 226 | @rpark4 | http://www.freecodecamp.com/rpark4
:star: 370 | @abhishekkumar1 | http://www.freecodecamp.com/abhishekkumar1
@Cyberistic
if (card <= 6 && card >= 2){
return count + " Bet";
}else if (card >= 7 && card <= 9 ){
return count + " Hold";
}else if (card == 10 || card === 'J' || card === 'Q' || card === 'K' || card === 'A'){
return count + " Hold";
}
This is the problem. maybe you've never sat at the blackjack table and counted cards, aye?
cyberistic sends brownie points to @alfonsotech and @rpark4 :sparkles: :thumbsup: :sparkles:
:star: 354 | @alfonsotech | http://www.freecodecamp.com/alfonsotech
:star: 227 | @rpark4 | http://www.freecodecamp.com/rpark4
if (strokes == 1) {
return "Hole-in-one!";
} else if (strokes <=par-2) {
return "Eagle";
} else if (strokes == par-1) {
return "Birdie";
} else if (strokes == par) {
return "Par";
} else if (strokes == par+1) {
return "Bogey";
} else if (strokes == par+2) {
return "Double Bogey";
} else if (strokes >= par+3) {
return "Go Home!";
}
The function should check if firstName is an actual contact's firstName and the given property (prop) is a property of that contact.
If both are true, then return the "value" of that property.
If firstName does not correspond to any contacts then return "No such contact"
If prop does not correspond to any valid properties then return "No such property"
function lookUp(firstName, prop){
// Only change code below this line
if (firstName === contacts.firstName && prop === contacts.prop) {
return contacts.prop;
} else if (firstName !== contacts.firstName) {
return "No such contact";
} else if (firstName !== contacts.firstName && prop !== contacts.hasOwnProperty(prop)) {
return "No such property";
}
rpark4 sends brownie points to @qualitymanifest :sparkles: :thumbsup: :sparkles:
:star: 1053 | @qualitymanifest | http://www.freecodecamp.com/qualitymanifest
qualitymanifest sends brownie points to @rpark4 :sparkles: :thumbsup: :sparkles:
:star: 228 | @rpark4 | http://www.freecodecamp.com/rpark4
" :thumbsup: "
return "Eagle";
What I did wrong?
if(strokes=1){
return "Tiny";
}else if (strokes<=par-2){
return "Eagle";
}else if(par-1){
return "Birdie";
}else if(par){
return "Par";
}else if(Par+1){
return "Bogey";
}else if(par+2){
return "Double Bogey";
}else if(strokes>=par+3){
return "Go Home!";
}else {
return "Change Me";
}
if(strokes=1){
return "Hole-in-one";
}else if (strokes<=par-2){
return "Eagle";
}else if(par-1){
return "Birdie";
}else if(par){
return "Par";
}else if(Par+1){
return "Bogey";
}else if(par+2){
return "Double Bogey";
}else if(strokes>=par+3){
return "Go Home!";
}else {
return "Change Me";
}
:bulb: to format code use backticks! ``` more info
''' function myTest(val) {
var answer = "";
// Only change code below this line
switch (val) {
case 1:
answer = "alpha";
break;
case 2:
answer = "beta";
break;
case 3:
answer = "gamma";
break;
case 4:
answer = "delta";
// Only change code above this line
return answer;
}
}
// Change this value to test
myTest(1);
'''
:bulb: to format code use backticks! ``` more info
function myTest(val) {
var answer = "";
// Only change code below this line
switch (val) {
case 1:
answer = "alpha";
break;
case 2:
answer = "beta";
break;
case 3:
answer = "gamma";
break;
case 4:
answer = "delta";
// Only change code above this line
return answer;
}
}
// Change this value to test
myTest(1);
```js ⇦ Type 3 backticks and then press [shift + enter ⏎]
(type js or html or css)
<paste your code here>,
then press [shift + enter ⏎]
``` ⇦ Type 3 backticks, then press [enter ⏎]
This an inline `<paste code here>
` code formatting with a single backtick(`) at start and end around the code
.
See also: ☛ How to type Backticks | ☯ Compose Mode | ❄ Gitter Formatting Basics
collection[id][prop]
accesses the property value, so you assign to it with: collection[id][prop]=value;
rpark4 sends brownie points to @masd925 and @alfonsotech and @abhishekkumar1 :sparkles: :thumbsup: :sparkles:
:warning: rpark4 already gave masd925 points
:star: 356 | @alfonsotech | http://www.freecodecamp.com/alfonsotech
:star: 371 | @abhishekkumar1 | http://www.freecodecamp.com/abhishekkumar1
arunkumrv sends brownie points to @codernoob :sparkles: :thumbsup: :sparkles:
:star: 345 | @codernoob | http://www.freecodecamp.com/codernoob
else if(par){
return "Par";
@chandra90
chandra90 sends brownie points to @moose1551 :sparkles: :thumbsup: :sparkles:
:star: 201 | @moose1551 | http://www.freecodecamp.com/moose1551
// Only change code below this line
function update(id, prop, value) {
if (value !== "" && prop !== "tracks"){
collection[id][prop]=value;
}
else if (value !== "" && prop === "tracks") {
collection[id][prop].push(value);
}
else if (value === "")
function telephoneCheck(str) {
// Good luck!
if(str.match(/(\({1})\d{3}(\){1})/g)){
if(str.match(/^1?[\s-]?\(?(\d{3})\)?[\s-]?(\d{3})[\s-]?\d{4}$/g)){
return true;
}
else
return false;
}
else if(str.match(/^1?[\s-]?\d{3}[\s-]?\d{3}[\s-]?\d{4}$/g))
return true;
else
return false;
}
function convert(celsius) {
// Only change code below this line
// Only change code above this line
if ( typeof fahrenheit !== celsius * (9/5) + 32 ) {
return fahrenheit;
} else {
return fahrenheit = celsius * (9/5) +32
;
}
}
// Change the inputs below to test your code
convert(30);
function destroyer(arr) {
// Remove all the values
return arr;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.
more info:
bf details
|bf links
|hint
function destroyer(arr, num1, num2) {
...
$(document).ready(function() {
...
});
mstettner sends brownie points to @rpark4 :sparkles: :thumbsup: :sparkles:
:star: 229 | @rpark4 | http://www.freecodecamp.com/rpark4
arguments
on this one
timgrossmann sends brownie points to @masd925 :sparkles: :thumbsup: :sparkles:
:star: 1130 | @masd925 | http://www.freecodecamp.com/masd925
@dcnr Yeap! it work.. I had to erase the checking function add(2); jeje
Thanks for all the support, It help me a lot!
abel1987 sends brownie points to @dcnr :sparkles: :thumbsup: :sparkles:
:star: 756 | @dcnr | http://www.freecodecamp.com/dcnr
rpark4 sends brownie points to @codernoob :sparkles: :thumbsup: :sparkles:
:star: 346 | @codernoob | http://www.freecodecamp.com/codernoob
rpark4 sends brownie points to @codernoob :sparkles: :thumbsup: :sparkles:
:warning: rpark4 already gave codernoob points
sorry mstettner, you can't send brownie points to yourself! :sparkles: :sparkles:
mstettner sends brownie points to @camperbot :sparkles: :thumbsup: :sparkles:
:star: 1308 | @camperbot | http://www.freecodecamp.com/camperbot
var myStr
you just need to get rid of the period
var myStr
and get rid of the period
Sorry, can't find a bonfire called was just the name for what are now referred to as algorithms these challenges youre doing used to be called waypoints. [ Check the map? ]
mstettner sends brownie points to @qualitymanifest and @codernoob :sparkles: :thumbsup: :sparkles:
:star: 1054 | @qualitymanifest | http://www.freecodecamp.com/qualitymanifest
:star: 347 | @codernoob | http://www.freecodecamp.com/codernoob
function destroyer(arr) {
arguments[0].filter(function(val){
return val != arguments[i];
});
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
i
in there?
function destroyer(arr) {
var theArguments = arguments;
arguments[0].filter(function(val){
for(var i = 1; i <= arr.length; i++) {
return val != theArguments[i];
}
});
}
mstettner sends brownie points to @kirbyedy :sparkles: :thumbsup: :sparkles:
:star: 592 | @kirbyedy | http://www.freecodecamp.com/kirbyedy
function destroyer(arr) {
var theArguments = arguments;
arguments[0].filter(function(val){
for(var i = 1; i <= theArguments.length; i++) {
if(val === theArguments[i]) {
return false;
}
} return true;
});
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
mstettner sends brownie points to @qualitymanifest :sparkles: :thumbsup: :sparkles:
:warning: mstettner already gave qualitymanifest points
var myStr = \ \ \t \t \r \n;
function destroyer(arr) {
var theArguments = arguments;
return arr.filter(function(val){
for(var i = 1; i <= theArguments.length; i++) {
if(val === theArguments[i]) {
return false;
}
}
return true;
});
}
\ \
mstettner sends brownie points to @urobert and @qualitymanifest :sparkles: :thumbsup: :sparkles:
:warning: mstettner already gave qualitymanifest points
:star: 657 | @urobert | http://www.freecodecamp.com/urobert
qualitymanifest sends brownie points to @mstettner :sparkles: :thumbsup: :sparkles:
:star: 157 | @mstettner | http://www.freecodecamp.com/mstettner
mikenaza sends brownie points to @masd925 :sparkles: :thumbsup: :sparkles:
:star: 1131 | @masd925 | http://www.freecodecamp.com/masd925
// Setup
function phoneticLookup(val) {
var result = "undefined";
// Only change code below this line
var obj = {
"alpha": "Adams",
"bravo": "Boston",
"charlie": "Chicago",
"delta": "Denver",
"echo": "Easy",
"foxtrot": "Frank";
}
result = obj[val];
// Only change code above this line
return result;
}
// Change this value to test
phoneticLookup("charlie");
(shift+enter for line break)
var code = "formatted";
var myStr = "This is the first sentence. ";
ourStr += "This is the second sentence.";