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.
function getIndexToIns(arr, num) {
// Find my place in this sorted array.
arr.sort(function(a,b){ return a-b; });
for (var i = 0; i < arr.length; i++) {
if (num <= arr[i]) return i;
}
return i;
}
function truncateString(str, num) {
// Clear out that junk in your trunk
if ( str.length > num ) {
str = str.slice(-str.length, num);
str +="...";
}
return str;
}
Can anybody help me with code please?
str.slice(-str.length, num)
that removes from the beginning, doesnt it?
function palindrome(str) {
// Good luck!
str = str.replace(/[^A-Za-z]/g, '');
str = str.toLowerCase();
var strArray = [];
for (var i = 0; i < str.length; i++) {
strArray.unshift(str[i]);
}
strArray = strArray.join('');
console.log(strArray === str);
if (strArray !== str) {
return false;
}
return true;
}
palindrome("1 eye for of 1 eye.");
promhize sends brownie points to @sperkajugglite :sparkles: :thumbsup: :sparkles:
:cookie: 305 | @sperkajugglite |http://www.freecodecamp.com/sperkajugglite
Hi Guys .I am not being able to make sense to make "result" below my codes even though after reading full instructions.My code is
function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) {
var result = "";
// Your code below this line
// Your code above this line
return result;
}
// Change the words here to test your function
wordBlanks("cat", "little", "hit", "slowly");
@frontender007
if (num > 3) {
return str.slice(0, num-3) + "...";
} else {
return str.slice(0, num) + "...";
}
inside your if statement
```// Setup
var collection = {
"2548": {
"album": "Slippery When Wet",
"artist": "Bon Jovi",
"tracks": [
"Let It Rock",
"You Give Love a Bad Name"
]
},
"2468": {
"album": "1999",
"artist": "Prince",
"tracks": [
"1999",
"Little Red Corvette"
]
},
"1245": {
"artist": "Robert Palmer",
"tracks": [ ]
},
"5439": {
"album": "ABBA Gold"
}
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));
// Only change code below this line
function updateRecords(id, prop, value) {
return collection;
}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
```
:cookie: 321 | @jarenescueta731 |http://www.freecodecamp.com/jarenescueta731
:warning: promhize already gave sperkajugglite points
promhize sends brownie points to @sperkajugglite and @jarenescueta731 :sparkles: :thumbsup: :sparkles:
```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
simon laughing
code here
"
" code here "
"
promhize sends brownie points to @sperkajugglite :sparkles: :thumbsup: :sparkles:
:warning: promhize already gave sperkajugglite points
before and three
after!
atutus
`
// Setup
var collection = {
"2548": {
"album": "Slippery When Wet",
"artist": "Bon Jovi",
"tracks": [
"Let It Rock",
"You Give Love a Bad Name"
]
},
"2468": {
"album": "1999",
"artist": "Prince",
"tracks": [
"1999",
"Little Red Corvette"
]
},
"1245": {
"artist": "Robert Palmer",
"tracks": [ ]
},
"5439": {
"album": "ABBA Gold"
}
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));
// Only change code below this line
function updateRecords(id, prop, value) {
return collection;
}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");`
code
autut
`// Setup
var collection = {
"2548": {
"album": "Slippery When Wet",
"artist": "Bon Jovi",
"tracks": [
"Let It Rock",
"You Give Love a Bad Name"
]
},
"2468": {
"album": "1999",
"artist": "Prince",
"tracks": [
"1999",
"Little Red Corvette"
]
},
"1245": {
"artist": "Robert Palmer",
"tracks": [ ]
},
"5439": {
"album": "ABBA Gold"
}
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));
// Only change code below this line
function updateRecords(id, prop, value) {
return collection;
}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");`
```// Setup
var collection = {
"2548": {
"album": "Slippery When Wet",
"artist": "Bon Jovi",
"tracks": [
"Let It Rock",
"You Give Love a Bad Name"
]
},
"2468": {
"album": "1999",
"artist": "Prince",
"tracks": [
"1999",
"Little Red Corvette"
]
},
"1245": {
"artist": "Robert Palmer",
"tracks": [ ]
},
"5439": {
"album": "ABBA Gold"
}
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));
// Only change code below this line
function updateRecords(id, prop, value) {
return collection;
}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
```
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 7:
case 8:
case 9:
count =0 ;
break;
case 10:
case 'J':
case 'Q':
case 'K':
case 'A':
count--;
break;
}
if (count < +1)
{return count + "Hold";}
else if (count >1)
{return count + "Bet";}
return count;
// 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('A');
case 2:
case 3:
case 4:
case 5:
case 6:
count++;
break;
case 7:
case 8:
case 9:
count =0 ;
break;
aidenmead sends brownie points to @sperkajugglite :sparkles: :thumbsup: :sparkles:
:cookie: 306 | @sperkajugglite |http://www.freecodecamp.com/sperkajugglite
function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) {
var result = "";
// Your code below this line
// Your code above this line
return result;
}
// Change the words here to test your function
wordBlanks("dog", "big", "ran", "quickly");
http://codepen.io/Sperkajugglite/pen/JKyGJd?editors=0010
can anyone help me :innocent: ?
amran16 sends brownie points to @sperkajugglite :sparkles: :thumbsup: :sparkles:
:cookie: 307 | @sperkajugglite |http://www.freecodecamp.com/sperkajugglite
var result = "My " + my Adjective + myNoun + " would always " + myVerb + myAdverb;
rahin1122 sends brownie points to @hkuz :sparkles: :thumbsup: :sparkles:
:cookie: 419 | @hkuz |http://www.freecodecamp.com/hkuz
function mutation(arr) {
var target = arr[0].toLowerCase();
var test = arr[1].toLowerCase();
for(var i = 0; i<test.length; i++){
if (target.indexOf(test[i]) < 0)
return false;
}
return true;
}
mutation(["Hello", "hey"]);
@rahin1122 hmmmm, what about something like this:
// Your code below this line
var part1 = "My ";
var part2 = " would always ";
result = part1 + myAdjective + myNoun + part2 + myVerb + myAdverb;
// Your code above this line
return result;
I can't remember the exact instructions for that problem
indexOf(test[i])
to see what it outputs?
result = myAdjective + " " + myNoun + " " + ...
:triangular_flag_on_post: Remember to use Read-Search-Ask
if you get stuck. Try to pair program :busts_in_silhouette: and write your own code :pencil:
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, check to see if the given element in the array has the property of "tracks". If the element has the property of "tracks", push the value onto the end of the "tracks" array. If the element does not have the property, create the property and value pair.
If value is blank, delete that prop.
Always return the entire collection object.
:pencil: read more about challenge record collection on the FCC Wiki
labanch sends brownie points to @no-stack-dub-sack :sparkles: :thumbsup: :sparkles:
:cookie: 287 | @no-stack-dub-sack |http://www.freecodecamp.com/no-stack-dub-sack
no-stack-dub-sack sends brownie points to @sperkajugglite :sparkles: :thumbsup: :sparkles:
:cookie: 308 | @sperkajugglite |http://www.freecodecamp.com/sperkajugglite
robinsond7691 sends brownie points to @drez14 :sparkles: :thumbsup: :sparkles:
:warning: could not find receiver for drez14
$(document).ready(function(){
function scroller() {
function aboutAdd() {
var aboutActive = $("#nav-about").addClass(".nav-active");
$("#nav-portfolio").removeClass(".nav-active");
$("#nav-contact").removeClass(".nav-active")
};
function portfolioAdd() {
var aboutActive = $("#nav-portfolio").addClass(".nav-active");
$("#nav-about").removeClass(".nav-active");
$("#nav-contact").removeClass(".nav-active")
};
function contactAdd() {
var aboutActive = $("#nav-contact").addClass(".nav-active");
$("#nav-about").removeClass(".nav-active");
$("#nav-portfolio").removeClass(".nav-active")
};
var aboutPos = $("#about").offset();
var portfolioPos = $("#scroller").offset();
var contactPos = $("#contact").offset();
$(window).on("scroll" , function() {
if (aboutPos > portfolioPos && aboutPos > contactPos {
aboutAdd();
}
else if (portfolioPos > contactPos && portfolioPos < aboutPos){
portfolioAdd();
}
else if(contactPos < portfolioPos && contactPos < aboutPos){
contactAdd();
}
else{
$("#nav-about"),removeClass(".nav-active") ;
$("#nav-portfolio").removeClass(".nav-active");
$("#nav-contact").removeClass(".nav-active");
}
});
};
});
Hi all,
I'm struggling to get 'Comparison with the Greater Than Operator' section to work.
Here my code:
function testGreaterThan(val) {
if (val > 15) { // Change this line
return "Over 100";
}
if (val > 10 ) { // Change this line
return "Over 10";
}
return "10 or Under";
}
// Change this value to test
testGreaterThan(200);
function convertToF(celsius) {
var fahrenheit;
// Only change code below this line
var celcius;
fahrenheit = celcius * 1.8 + 32;
// Only change code above this line
return fahrenheit;
}
// Change the inputs below to test your code
convertToF(30);
sirvanux sends brownie points to @jesushilariohernandez :sparkles: :thumbsup: :sparkles:
:cookie: 175 | @jesushilariohernandez |http://www.freecodecamp.com/jesushilariohernandez
jesushilariohernandez sends brownie points to @sirvanux :sparkles: :thumbsup: :sparkles:
:warning: could not find receiver for sirvanux
jesushilariohernandez sends brownie points to @chrono79 :sparkles: :thumbsup: :sparkles:
:star2: 1541 | @chrono79 |http://www.freecodecamp.com/chrono79
ash717 sends brownie points to @chrono79 :sparkles: :thumbsup: :sparkles:
:star2: 1542 | @chrono79 |http://www.freecodecamp.com/chrono79
ash717 sends brownie points to @chrono79 :sparkles: :thumbsup: :sparkles:
:warning: ash717 already gave chrono79 points
:warning: ash717 already gave chrono79 points
ash717 sends brownie points to @chrono79 :sparkles: :thumbsup: :sparkles:
periman2 sends brownie points to @wo1v3r :sparkles: :thumbsup: :sparkles:
:cookie: 279 | @wo1v3r |http://www.freecodecamp.com/wo1v3r
@sansae
$(document).ready(function() {
$("#getNewQuote").on("click", function(){
//on click, change background color
$("body").css("background-color", "red");
});
});
If you want to use jquery you have to add it, click on javascript panel top right and add it
periman2 sends brownie points to @benalron :sparkles: :thumbsup: :sparkles:
:cookie: 368 | @benalron |http://www.freecodecamp.com/benalron
:triangular_flag_on_post: Remember to use Read-Search-Ask
if you get stuck. Try to pair program :busts_in_silhouette: and write your own code :pencil:
Remove all falsy values from an array.
:pencil: read more about algorithm falsy bouncer on the FCC Wiki
periman2 sends brownie points to @chrono79 :sparkles: :thumbsup: :sparkles:
:star2: 1543 | @chrono79 |http://www.freecodecamp.com/chrono79
return arr.shift(),arr.push(1);
needs to be changed. in particular, the comma is an issue... what should we do? also, what needs to be pushed?
jesushilariohernandez sends brownie points to @periman2 and @ndburrus :sparkles: :thumbsup: :sparkles:
:cookie: 269 | @periman2 |http://www.freecodecamp.com/periman2
:cookie: 980 | @ndburrus |http://www.freecodecamp.com/ndburrus
jesushilariohernandez sends brownie points to @periman2 :sparkles: :thumbsup: :sparkles:
:warning: jesushilariohernandez already gave periman2 points
@periman2 function rot13(str) { // LBH QVQ VG!
var o='';
for (i=0;i<str.length;i++){
if (str.charCodeAt(i)>77){
o+= String.fromCharCode(str.charCodeAt(i)-13);
}
else {
o+= String.fromCharCode(str.charCodeAt(i)+13);
}
}
return o;
}
// Change the inputs below to test
rot13("GUR DHVPX OEBJA QBT WHZCRQ BIRE GUR YNML SBK.");
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 (count<0) {
return "Hold";
} else if (count>0) {
return "Bet";
}
rajdeeproy sends brownie points to @periman2 :sparkles: :thumbsup: :sparkles:
:cookie: 270 | @periman2 |http://www.freecodecamp.com/periman2
if (count<0) {
return count + "Hold";
} else if (count>0) {
return count + "Bet";
}
rajdeeproy sends brownie points to @periman2 :sparkles: :thumbsup: :sparkles:
:warning: rajdeeproy already gave periman2 points
Hi, I'm CamperBot! I can help you in this chatroom :smile:
find TOPIC
find all entries about topic. e.g. find js
wiki TOPIC
show contents of topic pagethanks @username
send brownie points to another userabout @username
shows info on that userAlgorithm BONFIRENAME
info on a Algorithm:triangular_flag_on_post: Remember to use Read-Search-Ask
if you get stuck. Try to pair program :busts_in_silhouette: and write your own code :pencil:
This problem is a bit tricky because you have to familiarize yourself with Arguments, as you will have to work with two or more but on the script you only see two. Many people hardcode this program for three arguments. You will remove any number from the first argument that is the same as any other other arguments.
:pencil: read more about algorithm seek and destroy on the FCC Wiki
"FirstLine\n\\SecondLine\\\rThirdLine"
```
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 (count<=0) {
return count + " Hold";
}else {
return count + " Bet";
}
``` @ChadDean82
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 (count<=0) {
return count +"Hold";
} else {
return count +"Bet";
}
return count;
if (count<=0) {
return count + "Hold";
} else {
return count + "Bet";
}
if (count<=0) {
return count + " Hold";
} else {
return count + " Bet";
}
var superCounter = function (x) {
var charCount = x.length;
var wordCount = x.split(" ").length;
var whiteSpace = wordCount - 1;
var letterCount = charCount - whiteSpace;
var wordArray = x.split(" ");
var avgLen = charCount / wordCount;
console.log(("Letters: " + letterCount), "Character count: " + charCount, "Word count: " + wordCount, "Whitespace count: " + whiteSpace, "Word length average: " + avgLen);
};
superCounter("word counter");
chaddean82 sends brownie points to @bitcross :sparkles: :thumbsup: :sparkles:
:cookie: 208 | @bitcross |http://www.freecodecamp.com/bitcross
// Setup
function phoneticLookup(val) {
var result = undefined;
// Only change code below this line
"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");
// Only change code below this line
var lookup = {
"alpha":"Adams",
"bravo":"Boston",
"charlie":"Chicago",
"delta":"Denver",
"echo":"Easy",
"foxtrot":"Frank"
};
result = lookup[val];
@Bitcross when i run it, it says return not in function
// Setup
function phoneticLookup(val) {
var result = undefined;
// Only change code below this line
var lookup = {
"alpha":"Adams",
"bravo":"Boston",
"charlie":"Chicago",
"delta":"Denver",
"echo":"Easy",
"foxtrot":"Frank"
};
result = lookup[val];
}
// Only change code above this line
return result;
}
// Change this value to test
phoneticLookup("charlie");
// 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"
};
result = lookup[val];
// Only change code above this line
return result;
}
// Change this value to test
phoneticLookup("charlie");
martinronquillo sends brownie points to @bitcross :sparkles: :thumbsup: :sparkles:
:cookie: 213 | @bitcross |http://www.freecodecamp.com/bitcross
function myLocalScope() {
'use strict';
console.log(myVar);
}
myLocalScope();
// Run and check the console
// myVar is not defined outside of myLocalScope
console.log(myVar);
// Now remove the console log line to pass the test
// Setup
var collection = {
"2548": {
"album": "Slippery When Wet",
"artist": "Bon Jovi",
"tracks": [
"Let It Rock",
"You Give Love a Bad Name"
]
},
"2468": {
"album": "1999",
"artist": "Prince",
"tracks": [
"1999",
"Little Red Corvette"
]
},
"1245": {
"artist": "Robert Palmer",
"tracks": [ ]
},
"5439": {
"album": "ABBA Gold"
}
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));
// Only change code below this line
function updateRecords(id, prop, value) {
if (prop!="tracks" && value!=="") {
collection[id][prop] = value;
} else if (prop=="tracks" && id.tracks===undefined) {
collection[id].tracks = [];
collection[id].tracks.push(value);
} else if (prop=="tracks" && value!=="") {
collection[id].tracks.push(value);
} else if (value==="") {
delete collection[id][prop];
}
return collection;
}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
var expression = /\d+/g;
lordyamanouchi sends brownie points to @qualitymanifest :sparkles: :thumbsup: :sparkles:
:star2: 1278 | @qualitymanifest |http://www.freecodecamp.com/qualitymanifest
arr
), in a real array?
arr
(arr.filter
....) and return true if the additional arguments are not in arr
, false otherwise. returning false removes the element
```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 destroyer(arr) {
// Remove all the values
//var iterations = arr[0].length;
var newArr = Array.from(arr);
var finalArr = newArr[0].splice();
function filter(a, b){
return a !== b;
}
for(var i = 1;i<newArr.length;i++){
//finalArr = finalArr.filter(fil(val, newArr[i]));
finalArr.forEach
/*
for(var x = newArr[0].length-1;x >= 0;x--){
if(newArr[x] === newArr[i]){
newArr.splice(x,1);
console.log(newArr);
}
*/
}
/*
for(var x = 0;x<iterations;x++){
if(newArr[x] === arr[i]){
newArr.splice(x,1);
}
}
*/
}
return newArr[0];
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
el
represents each part of arr
https://repl.it/Cc9i
arr
already is a proper array. arguments
will give you an array-like object of all the arguments passed into your function. you're running Array.from()
on the wrong thing
arguments
to a real array. hence the Array.from
fauzankadri sends brownie points to @jarenescueta731 :sparkles: :thumbsup: :sparkles:
:cookie: 322 | @jarenescueta731 |http://www.freecodecamp.com/jarenescueta731
function destroyer(arr) {
// Remove all the values
//var iterations = arr[0].length;
var newArr = arr.slice();
var destroyArr = Array.from(arguments);
for(var i = 0;i<destroyArr.length;i++){
newArr = newArr.filter(function(val){
return val !== newArr[i];
});
}
return newArr;
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
vercaelus sends brownie points to @benalron :sparkles: :thumbsup: :sparkles:
:cookie: 369 | @benalron |http://www.freecodecamp.com/benalron
vercaelus sends brownie points to @qualitymanifest :sparkles: :thumbsup: :sparkles:
:star2: 1279 | @qualitymanifest |http://www.freecodecamp.com/qualitymanifest
function destroyer(arr) {
for (x=0; x<arguments[0].length; x++){
for (y=1; y<arguments.length; y++){
if (arguments[0][x] === arguments[y]){
delete arguments[0][x];
}
}
}
arr = arr.filter(Boolean);
return arr;
}
destroyer([3, 5, 1, 2, 2], 2, 3, 5);
Option1
and Option2
. Option1
eliminates the warning "Don't make function within loop" in Option2
. Are there any other more elegant solutions? function destroyer(arr) {
// Remove all the values
//alert (arguments[1]);
var args = Array.prototype.slice.call(arguments);
args.splice(0,1);
var isNotInList = function (list, val) {
return list.indexOf(val) == -1;
};
var result =[];
/* OPTION 1 */
result = arr.filter (function (elem) {
return isNotInList (args, elem);
});
/* OPTION 2 */
for (var x = 0; x < args.length; x++) {
result = arr.filter (function (elem) { return elem != args[x]; });
arr = result;
}
return result;
}
function destroyer(arr) {
args = Array.prototype.slice.call(arguments,1);
return arr.filter(function(val) {
return args.indexOf(val)===-1;
});
}
kgashok sends brownie points to @masd925 :sparkles: :thumbsup: :sparkles:
:star2: 2128 | @masd925 |http://www.freecodecamp.com/masd925
for
loop, how do I "move" the function out of the loop?
Instructions
Assign the following three lines of text into the single variable myStr using escape sequences.
FirstLine
\SecondLine\
ThirdLine
You will need to use escape sequences to insert special characters correctly. You will also need to follow the spacing as it looks above, with no spaces between escape sequences or words.
Here is the text with the escape sequences written out.
FirstLinenewlinebackslashSecondLinebackslashcarriage-returnThirdLine
function destroyer(arr) {
return arr.filter(function(elem){
for (var i=1;i<this.length;i++) {
if (this[i]===elem) return false;
}
return true;
},arguments);
}
function destroyer(arr) {
var args = arguments;
return arr.filter(function(elem){
for (var i=1;i<args.length;i++) {
if (args[i]===elem) return false;
}
return true;
});
}
ES6y
version!
function destroyer(arr, ...args) {
return arr.filter(elem => !args.includes(elem))
}
ought to work
d=(a,...r)=>a.filter(e=>!r.includes(e))
function destroyer(arr) {
var args = Array.prototype.slice.call(arguments);
return arr.filter(elem => args.indexOf(elem) === -1);
}
function destroyer(arr) {
return arr.filter(function(val) {
return Array.prototype.indexOf.call(this,val,1)===-1;
},arguments);
}
reject
// jshint esversion:6
kgashok sends brownie points to @no-stack-dub-sack and @llamatarianism and @masd925 :sparkles: :thumbsup: :sparkles:
:cookie: 996 | @llamatarianism |http://www.freecodecamp.com/llamatarianism
:cookie: 289 | @no-stack-dub-sack |http://www.freecodecamp.com/no-stack-dub-sack
:warning: kgashok already gave masd925 points
reject
function.
flatMap
or xWhile
functions (e.g. takeWhile
, reduceWhile
)
console.log(("Letters: " + letterCount), "Character count: " + charCount, "Word count: " + wordCount, "Whitespace count: " + whiteSpace, "Word length average: " + avgLen);
};
how can i put all those strings into one object output thank you
function destroyer(arr) {
return arr.filter(function(val) {
return Array.prototype.indexOf.call(this,val,1)===-1;
},arguments);
Array.prototype.indexOf.call(arguments)
arguments
isn't an array, but it's similar enough to one that you can use an array method on it
.call
forEach
?
console.log(data)
bit
.append
1
is doing
this
is equal to arguments
inside that filter function
thisArg
to filter -> documentation
val
is an item in the array
forEach
of what?
no-stack-dub-sack sends brownie points to @llamatarianism :sparkles: :thumbsup: :sparkles:
:cookie: 998 | @llamatarianism |http://www.freecodecamp.com/llamatarianism
checkProp = myObj; // now you're going to be trying to find the property of `myObj` that is equal to itself, which is impossible.
var checkObj = myObj[checkProp] // this would work if you hadn't changed `checkProp`
return "Change Me!"; // you're supposed to change this. change it so it returns `myObj[checkProp]`.
var myStr = 'Firstline\n \\SecondLine\ \rThirdline';
// ^ you didn't escape this one
FirstLine
and ThirdLine
\\
can anyone tell me what is wrong with this
var collection = {
"2548": {
"album": "Slippery When Wet",
"artist": "Bon Jovi",
"tracks": [
"Let It Rock",
"You Give Love a Bad Name"
]
},
"2468": {
"album": "1999",
"artist": "Prince",
"tracks": [
"1999",
"Little Red Corvette"
]
},
"1245": {
"artist": "Robert Palmer",
"tracks": [ ]
},
"5439": {
"album": "ABBA Gold"
}
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));
// Only change code below this line
function updateRecords(id, prop, value) {
if (prop!="tracks" && value!==""){
collection[id][prop]=value;
}
else if (prop ==="tracks" && id.tracks===undefined){
collection[id].tracks=[];
collection[id].tracks.push(value);
}
else if (prop==="tracks" && id.tracks!=="" && value!==""){
collection[id].tracks.push(value);
}
else if (value===""){
delete collection[id][prop];
}
return collection;
}
// Alter values below to test your code
updateRecords(2548, "tracks", "Free");
"FirstLine\n\SecondLine\\rThirdLine"
\S
(which is invalid), a backslash, then an r
['a', 'b', 'c'] convert to
[['a'],['b'],[c']]
"FirstLine\n\\SecondLine\rThirdLine"
\r
)
const arr = [1, 2, 3];
arr.map(x => [x])
// -> [[1], [2], [3]]
jackedwardlyons sends brownie points to @llamatarianism :sparkles: :thumbsup: :sparkles:
:star2: 999 | @llamatarianism |http://www.freecodecamp.com/llamatarianism
id.tracks
is undefined
id
is just a number
"tracks"
property
collection[id].tracks
for a json object
{
"key": "value"
}
and
{
key: "value"
}
produces same result
var a = JSON.parse({key: "value"});
var a = {
"key": "value"
}
console.log(a.key) // => "value"
key
is a variable
{ key: "value" }
and { "key": "value" }
are 100% identical
var a = "foo";
var b = "bar";
var c = {a: b};
console.log(c) // => {a: bar} , I expect {foo: bar}
var c = {};
c[a] = b;
var c = { [a]: b };
[]
makes it evaluate the variable
()
but not []
, I thought ()
is used in evaluation
meetmangukiya sends brownie points to @llamatarianism :sparkles: :thumbsup: :sparkles:
:star2: 1000 | @llamatarianism |http://www.freecodecamp.com/llamatarianism
(+ 1 2)
'(1 2)
'(1 . 2)
`(,@(list 1 2 3))
(+ 1 2)
:keyword
["this" "is" "a" "vector"]
{:hash "map"}
#{:hash :set}
js
?
(define (map fun lst)
(if (null? lst)
'()
(cons (fun (car lst)) (map fun (cdr lst)))))
title
and url
of the current tab somewhere
that example above was in scheme. this is common lisp:
(defun map (fun lst)
(if (null lst)
nil
(cons (funcall fun (first lst)) (map fun (rest lst)))))
this is clojure:
(defn map [fun lst]
(if (empty? lst)
'()
(cons (fun (first lst)) (map fun (rest lst)))))
'
function lookUpProfile(firstName, prop){
// Only change code below this line
var i=0;
while (i<=contacts.length){
for (var j=0;j<=contacts.length;j++){
if (prop!==contacts[i][j]){
console.log("No such property");
}
else if (prop===contacts[i][j]){
console.log(contacts[i][prop]);
}
}
if (firstName===contacts[i][firstName]){
console.log(contacts[i][firstName]);
}
else if (firstName!==contacts[i][firstName]){
console.log("No such contact");
}
i++;
}
// Only change code above this line
}
// Setup
var collection = {
"2548": {
"album": "Slippery When Wet",
"artist": "Bon Jovi",
"tracks": [
"Let It Rock",
"You Give Love a Bad Name"
]
},
"2468": {
"album": "1999",
"artist": "Prince",
"tracks": [
"1999",
"Little Red Corvette"
]
},
"1245": {
"artist": "Robert Palmer",
"tracks": [ ]
},
"5439": {
"album": "ABBA Gold"
}
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));
// Only change code below this line
function updateRecords(id, prop, value) {
if (prop!="tracks" && value!=="") {
collection[id][prop] = value;
} else if (prop=="tracks" && id.tracks===undefined) {
collection[id].tracks = [];
collection[id].tracks.push(value);
} else if (prop=="tracks" && value!=="") {
collection[id].tracks.push(value);
} else {
delete collection[id][prop];
}
return collection;
}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
willfree108 sends brownie points to @llamatarianism :sparkles: :thumbsup: :sparkles:
:star2: 1001 | @llamatarianism |http://www.freecodecamp.com/llamatarianism
localStorage
is an array? Assuming I only know that length is a property of an array
typeof localStorage
returns object
2cool4school sends brownie points to @cal-culator :sparkles: :thumbsup: :sparkles:
:warning: could not find receiver for cal-culator
cal-culator sends brownie points to @2cool4school :sparkles: :thumbsup: :sparkles:
:cookie: 211 | @2cool4school |http://www.freecodecamp.com/2cool4school
meetmangukiya sends brownie points to @rud156 :sparkles: :thumbsup: :sparkles:
:cookie: 270 | @rud156 |http://www.freecodecamp.com/rud156
cal-culator sends brownie points to @dmitrij-schmidt :sparkles: :thumbsup: :sparkles:
:cookie: 323 | @dmitrij-schmidt |http://www.freecodecamp.com/dmitrij-schmidt
@Cal-culator - also try to debug your code in https://repl.it/languages/javascript_web . it's more helpful because it allows to console.log things.
try console.log(contacts[i][j]) in your first if statement ;) it will give you a hint
@Benalron then small gift from me (of course if you don't know it already) - https://regex101.com/#javascript
right down in the middle there's 'substitution' section. really helpful tool
benalron sends brownie points to @dmitrij-schmidt :sparkles: :thumbsup: :sparkles:
:cookie: 324 | @dmitrij-schmidt |http://www.freecodecamp.com/dmitrij-schmidt
Hi, can someone help me?
This is my code:
$(document).ready(function() {
$("#randomBtn").on("click",function(){
var answer="";
var random= Math.floor(Math.random() * (3)+1);
switch(random) {
case 1:
answer=['"La mujer es como la polvora, si no la tratas con cuidado, explota"', "Miguel Moyá"];
break;
case 2:
answer= ['"Y los españoles, muy españoles y mucho españoles!!"', "Rajoy"];
break;
case 3:
answer= ['"El mundo esta dividido en tres grandes grupos, el de los que sabe contar, y el de los que no."',"Homer J. Simpson"];}
$(".message").html(answer[0]);
$(".name").html(answer[1]);
var tweet= "https://twitter.com/intent/tweet?text=+";
answer=answer[0].split(" ");
answer=answer[0].join("%20");
tweet+= answer[0];
tweet+= "%20"+ answer[1];
console.log(answer);
$("#tweetBtn").attr("href",tweet);
});
});
My problem is that $("#tweetBtn").attr("href",tweet); don't work, also I write console.log(answer); for search problems, and console don't print anything
;_(
help please
check if firstName is an actual contact's firstName {
check the given property (prop) is a property of that contact. {
if so, return that prop
if not, return 'no such prop'
}
} if not {
return 'no such contact'
}
```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
@Cal-culator - in your case contacts[i] is an object, and seems like it's properties are not enumerated - that way unreachable by contacts[i][j] where j is a number
try to run in repl.it that code below and read logs
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
}
];
console.log(contacts[0]);
console.log(contacts[0][0]);
console.log(contacts[1]);
console.log(contacts[1][1]);
@Cal-culator as for backticks: you have to put it in chat window like
'''
code
code
code
'''
just replace apostrophes with backticks - the ` marks ;)
:bulb: to format code use backticks! ``` more info
function lookUpProfile(firstName, prop){
// Only change code below this line
var i=0;
CounterLoop:
while (i<=contacts.length-1){
if (contacts[i].hasOwnProperty(prop)===false){
console.log("No such property");
break CounterLoop;
}
else if (contacts[i].hasOwnProperty(prop)===true && contacts[i].firstName===firstName){
console.log(contacts[i][prop]);
break CounterLoop;
}
else if (firstName===contacts[i][firstName]){
console.log(contacts[i][firstName]);
break CounterLoop;
}
else if (firstName!==contacts[i][firstName]){
console.log("No such contact");
break CounterLoop;
}
i++;
}
break;
goto
, dijkstra kills a puppy
// Setup
var collection = {
"2548": {
"album": "Slippery When Wet",
"artist": "Bon Jovi",
"tracks": [
"Let It Rock",
"You Give Love a Bad Name"
]
},
"2468": {
"album": "1999",
"artist": "Prince",
"tracks": [
"1999",
"Little Red Corvette"
]
},
"1245": {
"artist": "Robert Palmer",
"tracks": [ ]
},
"5439": {
"album": "ABBA Gold"
}
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));
// Only change code below this line
function updateRecords(id, prop, value) {
if(prop === "tracks") {
if(collection[id].prop !== "tracks") {
collection[id].prop = tracks[""];
collection[id].prop.push(value);
}
if(value !== "") {
collection[id].prop.push(value);
} else {
delete collection[id].prop;
}
}
return collection;
}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
What is the problem in this?
:triangular_flag_on_post: Remember to use Read-Search-Ask
if you get stuck. Try to pair program :busts_in_silhouette: and write your own code :pencil:
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, check to see if the given element in the array has the property of "tracks". If the element has the property of "tracks", push the value onto the end of the "tracks" array. If the element does not have the property, create the property and value pair.
If value is blank, delete that prop.
Always return the entire collection object.
:pencil: read more about challenge record collection on the FCC Wiki
while(i<=collection.length-1){
while(i<collection.length){
basket = [apple,orange,baby,zucchini]
if(basket has apple)
#do something
break;
check if firstName is an actual contact's firstName {
check the given property (prop) is a property of that contact. {
if so, return that prop
if not, return 'no such prop'
}
} if not {
return 'no such contact'
}
:triangular_flag_on_post: Remember to use Read-Search-Ask
if you get stuck. Try to pair program :busts_in_silhouette: and write your own code :pencil:
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, check to see if the given element in the array has the property of "tracks". If the element has the property of "tracks", push the value onto the end of the "tracks" array. If the element does not have the property, create the property and value pair.
If value is blank, delete that prop.
Always return the entire collection object.
:pencil: read more about challenge record collection on the FCC Wiki
If the prop is "tracks" and value is non-blank, check to see if the given element in the array has the property of "tracks". If the element has the property of "tracks", push the value onto the end of the "tracks" array. If the element does not have the property, create the property and value pair.
var a = 'https://youtube.com';
a.split(/(\/)|(\.)/); // => ["https:", "/", undefined, "", "/", undefined, "youtube", undefined, ".", "com"]
if (contacts.hasOwnProperty("firstName")===firstName){
console.log(firstName);
if (contacts.hasOwnProperty(prop)===true){
console.log(prop);
}
else if (contacts.hasOwnProperty(prop)===false){
console.log("No such Property");
}
}
else if (contacts.hasOwnProperty("firstName")!==firstName){
console.log("No such contact");
}
doesn't output anything
var myObj = {
gift: "pony",
pet: "kitten",
bed: "sleigh"
};
function checkObj(checkProp) {
if (myObj.hasOwnProperty("checkProp")) {
return myObj[checkProp_var];
}
return "Not Found";
}
checkObj("gift");
tomcake15 sends brownie points to @cal-culator :sparkles: :thumbsup: :sparkles:
:warning: could not find receiver for cal-culator
tomcake15 sends brownie points to @cal-culator :sparkles: :thumbsup: :sparkles:
:warning: could not find receiver for cal-culator
hello everyone....
function reverseString(str) {
var j=0;
for(var i=(str.length)-1 ;i>0;i--){
str[j]=str[(str.length)-1];
j++;
}
return str;
}
reverseString("hello");
can anyone please help me out ..
i want to reverse a given string
@Cal-culator - so far i know it's a boolean, so...
if (contacts.hasOwnProperty("firstName") ===firstName){
in case of success is evaluated as
if (true ===firstName){
which is false :)
```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 reverseString(str) {
var str1='';
for(var i=(str.length)-1 ;i>-1;i--){
str1+=str[i];
}
return str1;
}
reverseString("hello");
Instructions
Assign the following three lines of text into the single variable myStr using escape sequences.
FirstLine
\SecondLine\
ThirdLine
You will need to use escape sequences to insert special characters correctly. You will also need to follow the spacing as it looks above, with no spaces between escape sequences or words.
Here is the text with the escape sequences written out.
FirstLinenewlinebackslashSecondLinebackslashcarriage-returnThirdLine
function nextInLine(arr, item) {
// Your code here
return testArr.shift(); // Change this line
}
// Test Setup
var testArr = [1,2,3,4,5];
nextInLine([5,6,7,8,9],1);
// Display Code
console.log("Before: " + JSON.stringify(testArr));
console.log(nextInLine(testArr, 6)); // Modify this line to test
console.log("After: " + JSON.stringify(testArr));
FirstLine
\SecondLine\
ThirdLine
FirstLine\nSecondLine...
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 = count + 1;
break;
case 7:
case 8:
case 9:
count = count + 0;
break;
case 10:
case 'J':
case 'Q':
case 'K':
case 'A':
count = count - 1;
}
return count;
if (count <= 0) {
return count + " " + "Hold";}
}
else {
return count + " " + "Bet";
}
//Add/remove calls to test your function.
// Note: Only the last will display
}// Note: Only the last will display
cc(2); cc('J'); cc(9); cc(2); cc(7);
var contor = 1;
var factorial =0;
function factorialize(num) {
for(var i=1;i<=num;i++)
factorial = contor*=i;
if(num === 0){
factorial = 1;
}
return factorial;
}
factorialize(5);
return count;
@RC31 nope.
1)backslashes are escaped by \ - one for escaping, one for backslash
2) try to get output like
FirstLine
\SecondLine\
ThirdLine
see that backslashes are before and after SecondLine?
function palindrome(str) {
// Good luck!
var splitToString = str.split('');
var reverseString = splitToString.reverse();
if(str === reverseString){
return true;
}
else{
return false;
}
}
palindrome("eye");
```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
iulianchirvasa sends brownie points to @athaman :sparkles: :thumbsup: :sparkles:
:cookie: 584 | @athaman |http://www.freecodecamp.com/athaman
var a[];
and just input values in it using a for loop. is it right ??
var a = [];
indhusethu sends brownie points to @dmitrij-schmidt :sparkles: :thumbsup: :sparkles:
:cookie: 325 | @dmitrij-schmidt |http://www.freecodecamp.com/dmitrij-schmidt
Hello i have this code for the truncate string challenge:
'''
function truncateString(str, num) {
// Clear out that junk in your trunk
//var newstrlength;
if (str > 3 || num > 3) {
var newstrlength = num - 3;
str = str.substring(0, newstrlength || num);
str = str.concat('...');
}
else if (num < 3) {
str = str.substring(0, num);
str = str.concat('...');
}
return str;
}
truncateString("A-tisket a-tasket A green and yellow basket", 11);
'''
but it cannot work with this function call:
'''
truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length)
'''
is the argument syntax correct, because im getting syntax errors when i try to use function call to debug
truncateString("A-tisket a-tasket A green and yellow basket", "A-tisket a-tasket A green and yellow basket".length)
is this argument in this function correct, thats what's on the truncate string challenge